* @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
| 756 | * @endcode |
| 757 | */ |
| 758 | bool 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. |