|
@@ -303,11 +303,8 @@ bool String::equals_ignoring_case(const StringView& other) const
|
|
return StringUtils::equals_ignoring_case(view(), other);
|
|
return StringUtils::equals_ignoring_case(view(), other);
|
|
}
|
|
}
|
|
|
|
|
|
-int String::replace(const String& needle, const String& replacement, bool all_occurrences)
|
|
|
|
|
|
+Vector<size_t> String::find_all(const String& needle) const
|
|
{
|
|
{
|
|
- if (is_empty())
|
|
|
|
- return 0;
|
|
|
|
-
|
|
|
|
Vector<size_t> positions;
|
|
Vector<size_t> positions;
|
|
size_t start = 0, pos;
|
|
size_t start = 0, pos;
|
|
for (;;) {
|
|
for (;;) {
|
|
@@ -317,11 +314,26 @@ int String::replace(const String& needle, const String& replacement, bool all_oc
|
|
|
|
|
|
pos = ptr - characters();
|
|
pos = ptr - characters();
|
|
positions.append(pos);
|
|
positions.append(pos);
|
|
- if (!all_occurrences)
|
|
|
|
- break;
|
|
|
|
|
|
|
|
start = pos + 1;
|
|
start = pos + 1;
|
|
}
|
|
}
|
|
|
|
+ return positions;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int String::replace(const String& needle, const String& replacement, bool all_occurrences)
|
|
|
|
+{
|
|
|
|
+ if (is_empty())
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ Vector<size_t> positions;
|
|
|
|
+ if (all_occurrences) {
|
|
|
|
+ positions = find_all(needle);
|
|
|
|
+ } else {
|
|
|
|
+ auto pos = find(needle);
|
|
|
|
+ if (!pos.has_value())
|
|
|
|
+ return 0;
|
|
|
|
+ positions.append(pos.value());
|
|
|
|
+ }
|
|
|
|
|
|
if (!positions.size())
|
|
if (!positions.size())
|
|
return 0;
|
|
return 0;
|
|
@@ -338,6 +350,22 @@ int String::replace(const String& needle, const String& replacement, bool all_oc
|
|
return positions.size();
|
|
return positions.size();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+size_t String::count(const String& needle) const
|
|
|
|
+{
|
|
|
|
+ size_t count = 0;
|
|
|
|
+ size_t start = 0, pos;
|
|
|
|
+ for (;;) {
|
|
|
|
+ const char* ptr = strstr(characters() + start, needle.characters());
|
|
|
|
+ if (!ptr)
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ pos = ptr - characters();
|
|
|
|
+ count++;
|
|
|
|
+ start = pos + 1;
|
|
|
|
+ }
|
|
|
|
+ return count;
|
|
|
|
+}
|
|
|
|
+
|
|
String String::reverse() const
|
|
String String::reverse() const
|
|
{
|
|
{
|
|
StringBuilder reversed_string;
|
|
StringBuilder reversed_string;
|