MCPcopy Create free account
hub / github.com/avast/retdec / isComposedOnlyOfStrings

Function isComposedOnlyOfStrings

src/utils/string.cpp:758–788  ·  view source on GitHub ↗

* @brief Returns @c true if @a str is composed solely of strings @a ss, @c * false otherwise. * * Examples: * @code * isComposedOnlyOfStrings("abcd", "abcd") -> true * isComposedOnlyOfStrings("ababab", "ab") -> true * isComposedOnlyOfStrings("aaaaa", "aa") -> true * isComposedOnlyOfStrings("", "") -> true * isComposedOnlyOfStrings("ababab", "ba") -> false * isComposedOnly

Source from the content-addressed store, hash-verified

756* @endcode
757*/
758bool isComposedOnlyOfStrings(const std::string &str, const std::string &ss) {
759 if (str.empty()) {
760 // The empty string can be composed only of the empty string.
761 return ss.empty();
762 }
763
764 if (ss.empty()) {
765 // No non-empty string can be composed only of the empty string.
766 return false;
767 }
768
769 if (ss.size() > str.size()) {
770 // A string cannot be formed only by strings of a greater length than
771 // the string itself.
772 return false;
773 }
774
775 if (str.size() % ss.size() == 0) {
776 // It suffices to check that str is of the form ss ss ss (without the
777 // spaces)...
778 for (std::string::size_type i = 0, e = str.size(); i < e; i += ss.size()) {
779 if (str.substr(i, ss.size()) != ss) {
780 return false;
781 }
782 }
783 return true;
784 }
785
786 // Both str and ss have to be composed only of a single character.
787 return isComposedOnlyOfChars(str, ss[0]) && isComposedOnlyOfChars(ss, str[0]);
788}
789
790/**
791* @brief Strips all directories from the given path.

Callers 2

isMoreReadableInHexaMethod · 0.85
TEST_FFunction · 0.85

Calls 3

isComposedOnlyOfCharsFunction · 0.85
emptyMethod · 0.45
sizeMethod · 0.45

Tested by 1

TEST_FFunction · 0.68