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

AK: Use an enum instead of a bool for String::replace(all_occurences)

This commit has no behavior changes.

In particular, this does not fix any of the wrong uses of the previous
default parameter (which used to be 'false', meaning "only replace the
first occurence in the string"). It simply replaces the default uses by
String::replace(..., ReplaceMode::FirstOnly), leaving them incorrect.
This commit is contained in:
DexesTTP 2022-07-05 22:33:15 +02:00 committed by Linus Groh
parent b2454888e8
commit 7ceeb74535
47 changed files with 108 additions and 102 deletions

View file

@ -291,7 +291,7 @@ public:
return { characters(), length() };
}
[[nodiscard]] String replace(StringView needle, StringView replacement, bool all_occurrences = false) const { return StringUtils::replace(*this, needle, replacement, all_occurrences); }
[[nodiscard]] String replace(StringView needle, StringView replacement, ReplaceMode replace_mode) const { return StringUtils::replace(*this, needle, replacement, replace_mode); }
[[nodiscard]] size_t count(StringView needle) const { return StringUtils::count(*this, needle); }
[[nodiscard]] String reverse() const;

View file

@ -476,13 +476,13 @@ String invert_case(StringView str)
return builder.to_string();
}
String replace(StringView str, StringView needle, StringView replacement, bool all_occurrences)
String replace(StringView str, StringView needle, StringView replacement, ReplaceMode replace_mode)
{
if (str.is_empty())
return str;
Vector<size_t> positions;
if (all_occurrences) {
if (replace_mode == ReplaceMode::All) {
positions = str.find_all(needle);
if (!positions.size())
return str;

View file

@ -22,6 +22,11 @@ enum class CaseSensitivity {
CaseSensitive,
};
enum class ReplaceMode {
All,
FirstOnly,
};
enum class TrimMode {
Left,
Right,
@ -80,7 +85,7 @@ String to_snakecase(StringView);
String to_titlecase(StringView);
String invert_case(StringView);
String replace(StringView, StringView needle, StringView replacement, bool all_occurrences = false);
String replace(StringView, StringView needle, StringView replacement, ReplaceMode);
size_t count(StringView, StringView needle);
}
@ -88,5 +93,6 @@ size_t count(StringView, StringView needle);
}
using AK::CaseSensitivity;
using AK::ReplaceMode;
using AK::TrimMode;
using AK::TrimWhitespace;

View file

@ -228,9 +228,9 @@ bool StringView::operator==(String const& string) const
String StringView::to_string() const { return String { *this }; }
String StringView::replace(StringView needle, StringView replacement, bool all_occurrences) const
String StringView::replace(StringView needle, StringView replacement, ReplaceMode replace_mode) const
{
return StringUtils::replace(*this, needle, replacement, all_occurrences);
return StringUtils::replace(*this, needle, replacement, replace_mode);
}
#endif

View file

@ -268,7 +268,7 @@ public:
}
#ifndef KERNEL
[[nodiscard]] String replace(StringView needle, StringView replacement, bool all_occurrences = false) const;
[[nodiscard]] String replace(StringView needle, StringView replacement, ReplaceMode) const;
#endif
[[nodiscard]] size_t count(StringView needle) const
{

View file

@ -245,7 +245,7 @@ URL URLParser::parse(StringView raw_input, URL const* base_url, Optional<URL> ur
// NOTE: This replaces all tab and newline characters with nothing.
if (processed_input.contains("\t") || processed_input.contains("\n")) {
report_validation_error();
processed_input = processed_input.replace("\t", "", true).replace("\n", "", true);
processed_input = processed_input.replace("\t", "", ReplaceMode::All).replace("\n", "", ReplaceMode::All);
}
State state = state_override.value_or(State::SchemeStart);