1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:07:35 +00:00

AK+Everywhere: Consolidate String::index_of() and String::find()

We had two functions for doing mostly the same thing. Combine both
of them into String::find() and use that everywhere.

Also add some tests to cover basic behavior.
This commit is contained in:
Andreas Kling 2021-05-24 11:50:46 +02:00
parent 875a2cbb71
commit de395a3df2
12 changed files with 36 additions and 30 deletions

View file

@ -82,7 +82,7 @@ HunkLocation parse_hunk_location(const String& location_line)
size_t length { 0 };
};
auto parse_start_and_length_pair = [](const String& raw) {
auto index_of_separator = raw.index_of(",").value();
auto index_of_separator = raw.find(',').value();
auto start = raw.substring(0, index_of_separator);
auto length = raw.substring(index_of_separator + 1, raw.length() - index_of_separator - 1);
auto res = StartAndLength { start.to_uint().value() - 1, length.to_uint().value() - 1 };

View file

@ -297,7 +297,7 @@ Optional<Image::Symbol> Image::find_demangled_function(const String& name) const
if (symbol.is_undefined())
return IterationDecision::Continue;
auto demangled = demangle(symbol.name());
auto index_of_paren = demangled.index_of("(");
auto index_of_paren = demangled.find('(');
if (index_of_paren.has_value()) {
demangled = demangled.substring(0, index_of_paren.value());
}

View file

@ -240,7 +240,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::index_of)
auto needle = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
return Value((i32)string.index_of(needle).value_or(-1));
return Value((i32)string.find(needle).value_or(-1));
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_lowercase)
@ -692,7 +692,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace)
auto search_string = search_value.to_string(global_object);
if (vm.exception())
return {};
Optional<size_t> position = string.index_of(search_string);
Optional<size_t> position = string.find(search_string);
if (!position.has_value())
return js_string(vm, string);

View file

@ -389,7 +389,7 @@ public:
{
if (const auto start_pos = pseudo_name.find('('); start_pos.has_value()) {
const auto start = start_pos.value() + 1;
if (const auto end_pos = pseudo_name.index_of(")", start); end_pos.has_value()) {
if (const auto end_pos = pseudo_name.find(')', start); end_pos.has_value()) {
return pseudo_name.substring_view(start, end_pos.value() - start).trim_whitespace();
}
}

View file

@ -43,7 +43,7 @@ void Resource::for_each_client(Function<void(ResourceClient&)> callback)
static Optional<String> encoding_from_content_type(const String& content_type)
{
auto offset = content_type.index_of("charset=");
auto offset = content_type.find("charset="sv);
if (offset.has_value()) {
auto encoding = content_type.substring(offset.value() + 8, content_type.length() - offset.value() - 8).to_lowercase();
if (encoding.length() >= 2 && encoding.starts_with('"') && encoding.ends_with('"'))
@ -58,7 +58,7 @@ static Optional<String> encoding_from_content_type(const String& content_type)
static String mime_type_from_content_type(const String& content_type)
{
auto offset = content_type.index_of(";");
auto offset = content_type.find(';');
if (offset.has_value())
return content_type.substring(0, offset.value()).to_lowercase();