mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:07:35 +00:00
AK: Add String::find_all() and String::count()
This commit is contained in:
parent
e9898a6031
commit
80077cea86
3 changed files with 56 additions and 6 deletions
|
@ -303,11 +303,8 @@ bool String::equals_ignoring_case(const StringView& other) const
|
|||
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;
|
||||
size_t start = 0, pos;
|
||||
for (;;) {
|
||||
|
@ -317,11 +314,26 @@ int String::replace(const String& needle, const String& replacement, bool all_oc
|
|||
|
||||
pos = ptr - characters();
|
||||
positions.append(pos);
|
||||
if (!all_occurrences)
|
||||
break;
|
||||
|
||||
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())
|
||||
return 0;
|
||||
|
@ -338,6 +350,22 @@ int String::replace(const String& needle, const String& replacement, bool all_oc
|
|||
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
|
||||
{
|
||||
StringBuilder reversed_string;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue