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

Everywhere: Add sv suffix to strings relying on StringView(char const*)

Each of these strings would previously rely on StringView's char const*
constructor overload, which would call __builtin_strlen on the string.
Since we now have operator ""sv, we can replace these with much simpler
versions. This opens the door to being able to remove
StringView(char const*).

No functional changes.
This commit is contained in:
sin-ack 2022-07-11 17:32:29 +00:00 committed by Andreas Kling
parent e5f09ea170
commit 3f3f45580a
762 changed files with 8315 additions and 8316 deletions

View file

@ -12,29 +12,29 @@
namespace AK {
static constexpr Array<StringView, 7> long_day_names = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
"Sunday"sv, "Monday"sv, "Tuesday"sv, "Wednesday"sv, "Thursday"sv, "Friday"sv, "Saturday"sv
};
static constexpr Array<StringView, 7> short_day_names = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
"Sun"sv, "Mon"sv, "Tue"sv, "Wed"sv, "Thu"sv, "Fri"sv, "Sat"sv
};
static constexpr Array<StringView, 7> mini_day_names = {
"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"
"Su"sv, "Mo"sv, "Tu"sv, "We"sv, "Th"sv, "Fr"sv, "Sa"sv
};
static constexpr Array<StringView, 7> micro_day_names = {
"S", "M", "T", "W", "T", "F", "S"
"S"sv, "M"sv, "T"sv, "W"sv, "T"sv, "F"sv, "S"sv
};
static constexpr Array<StringView, 12> long_month_names = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
"January"sv, "February"sv, "March"sv, "April"sv, "May"sv, "June"sv,
"July"sv, "August"sv, "September"sv, "October"sv, "November"sv, "December"sv
};
static constexpr Array<StringView, 12> short_month_names = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
"Jan"sv, "Feb"sv, "Mar"sv, "Apr"sv, "May"sv, "Jun"sv,
"Jul"sv, "Aug"sv, "Sep"sv, "Oct"sv, "Nov"sv, "Dec"sv
};
}

View file

@ -114,7 +114,7 @@ StringView FormatParser::consume_literal()
if (consume_specific("}}"))
continue;
if (next_is(is_any_of("{}")))
if (next_is(is_any_of("{}"sv)))
return m_input.substring_view(begin, tell() - begin);
consume();
@ -170,7 +170,7 @@ bool FormatParser::consume_specifier(FormatSpecifier& specifier)
if (!consume_specific('}'))
VERIFY_NOT_REACHED();
specifier.flags = "";
specifier.flags = ""sv;
}
return true;
@ -287,16 +287,16 @@ ErrorOr<void> FormatBuilder::put_u64(
if (prefix) {
if (base == 2) {
if (upper_case)
TRY(m_builder.try_append("0B"));
TRY(m_builder.try_append("0B"sv));
else
TRY(m_builder.try_append("0b"));
TRY(m_builder.try_append("0b"sv));
} else if (base == 8) {
TRY(m_builder.try_append("0"));
TRY(m_builder.try_append("0"sv));
} else if (base == 16) {
if (upper_case)
TRY(m_builder.try_append("0X"));
TRY(m_builder.try_append("0X"sv));
else
TRY(m_builder.try_append("0x"));
TRY(m_builder.try_append("0x"sv));
}
}
return {};
@ -587,8 +587,8 @@ ErrorOr<void> vformat(StringBuilder& builder, StringView fmtstr, TypeErasedForma
void StandardFormatter::parse(TypeErasedFormatParams& params, FormatParser& parser)
{
if (StringView { "<^>" }.contains(parser.peek(1))) {
VERIFY(!parser.next_is(is_any_of("{}")));
if ("<^>"sv.contains(parser.peek(1))) {
VERIFY(!parser.next_is(is_any_of("{}"sv)));
m_fill = parser.consume();
}
@ -788,7 +788,7 @@ ErrorOr<void> Formatter<bool>::format(FormatBuilder& builder, bool value)
return builder.put_hexdump({ &value, sizeof(value) }, m_width.value_or(32), m_fill);
} else {
Formatter<StringView> formatter { *this };
return formatter.format(builder, value ? "true" : "false");
return formatter.format(builder, value ? "true"sv : "false"sv);
}
}
#ifndef KERNEL

View file

@ -638,7 +638,7 @@ template<typename T, bool Supported = false>
struct __FormatIfSupported : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, FormatIfSupported<T> const&)
{
return Formatter<StringView>::format(builder, "?");
return Formatter<StringView>::format(builder, "?"sv);
}
};
template<typename T>
@ -673,15 +673,15 @@ struct Formatter<Error> : Formatter<FormatString> {
{
#if defined(__serenity__) && defined(KERNEL)
if (error.is_errno())
return Formatter<FormatString>::format(builder, "Error(errno={})", error.code());
return Formatter<FormatString>::format(builder, "Error({})", error.string_literal());
return Formatter<FormatString>::format(builder, "Error(errno={})"sv, error.code());
return Formatter<FormatString>::format(builder, "Error({})"sv, error.string_literal());
#else
if (error.is_syscall())
return Formatter<FormatString>::format(builder, "{}: {} (errno={})", error.string_literal(), strerror(error.code()), error.code());
return Formatter<FormatString>::format(builder, "{}: {} (errno={})"sv, error.string_literal(), strerror(error.code()), error.code());
if (error.is_errno())
return Formatter<FormatString>::format(builder, "{} (errno={})", strerror(error.code()), error.code());
return Formatter<FormatString>::format(builder, "{} (errno={})"sv, strerror(error.code()), error.code());
return Formatter<FormatString>::format(builder, "{}", error.string_literal());
return Formatter<FormatString>::format(builder, "{}"sv, error.string_literal());
#endif
}
};
@ -691,8 +691,8 @@ struct Formatter<ErrorOr<T, ErrorType>> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, ErrorOr<T, ErrorType> const& error_or)
{
if (error_or.is_error())
return Formatter<FormatString>::format(builder, "{}", error_or.error());
return Formatter<FormatString>::format(builder, "{{{}}}", error_or.value());
return Formatter<FormatString>::format(builder, "{}"sv, error_or.error());
return Formatter<FormatString>::format(builder, "{{{}}}"sv, error_or.value());
}
};

View file

@ -95,7 +95,7 @@ public:
return consume_specific(StringView { next, __builtin_strlen(next) });
}
constexpr char consume_escaped_character(char escape_char = '\\', StringView escape_map = "n\nr\rt\tb\bf\f")
constexpr char consume_escaped_character(char escape_char = '\\', StringView escape_map = "n\nr\rt\tb\bf\f"sv)
{
if (!consume_specific(escape_char))
return consume();
@ -234,8 +234,8 @@ constexpr auto is_not_any_of(StringView values)
return [values](auto c) { return !values.contains(c); };
}
constexpr auto is_path_separator = is_any_of("/\\");
constexpr auto is_quote = is_any_of("'\"");
constexpr auto is_path_separator = is_any_of("/\\"sv);
constexpr auto is_quote = is_any_of("'\""sv);
}

View file

@ -193,7 +193,7 @@ inline void JsonValue::serialize(Builder& builder) const
m_value.as_object->serialize(builder);
break;
case Type::Bool:
builder.append(m_value.as_bool ? "true" : "false");
builder.append(m_value.as_bool ? "true"sv : "false"sv);
break;
#if !defined(KERNEL)
case Type::Double:
@ -213,7 +213,7 @@ inline void JsonValue::serialize(Builder& builder) const
builder.appendff("{}", as_u64());
break;
case Type::Null:
builder.append("null");
builder.append("null"sv);
break;
default:
VERIFY_NOT_REACHED();

View file

@ -103,9 +103,9 @@ public:
{
TRY(begin_item(key));
if constexpr (IsLegacyBuilder<Builder>)
TRY(m_builder.try_append(value ? "true" : "false"));
TRY(m_builder.try_append(value ? "true"sv : "false"sv));
else
TRY(m_builder.append(value ? "true" : "false"));
TRY(m_builder.append(value ? "true"sv : "false"sv));
return {};
}
@ -234,11 +234,11 @@ private:
if constexpr (IsLegacyBuilder<Builder>) {
TRY(m_builder.try_append('"'));
TRY(m_builder.try_append_escaped_for_json(key));
TRY(m_builder.try_append("\":"));
TRY(m_builder.try_append("\":"sv));
} else {
TRY(m_builder.append('"'));
TRY(m_builder.append_escaped_for_json(key));
TRY(m_builder.append("\":"));
TRY(m_builder.append("\":"sv));
}
return {};
}

View file

@ -34,12 +34,12 @@ JsonValue JsonPath::resolve(JsonValue const& top_root) const
String JsonPath::to_string() const
{
StringBuilder builder;
builder.append("{ .");
builder.append("{ ."sv);
for (auto const& el : *this) {
builder.append(" > ");
builder.append("sv > "sv);
builder.append(el.to_string());
}
builder.append(" }");
builder.append("sv }"sv);
return builder.to_string();
}

View file

@ -133,7 +133,7 @@ String LexicalPath::relative_path(StringView a_path, StringView a_prefix)
{
if (!a_path.starts_with('/') || !a_prefix.starts_with('/')) {
// FIXME: This should probably VERIFY or return an Optional<String>.
return {};
return ""sv;
}
if (a_path == a_prefix)
@ -171,7 +171,7 @@ LexicalPath LexicalPath::prepend(StringView value) const
LexicalPath LexicalPath::parent() const
{
return append("..");
return append(".."sv);
}
}

View file

@ -45,7 +45,7 @@ template<>
struct AK::Formatter<AK::SourceLocation> : AK::Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, AK::SourceLocation location)
{
return AK::Formatter<FormatString>::format(builder, "[\x1b[34m{}\x1b[0m @ {}:{}]", location.function_name(), location.filename(), location.line_number());
return AK::Formatter<FormatString>::format(builder, "[\x1b[34m{}\x1b[0m @ {}:{}]"sv, location.function_name(), location.filename(), location.line_number());
}
};

View file

@ -346,13 +346,13 @@ String escape_html_entities(StringView html)
StringBuilder builder;
for (size_t i = 0; i < html.length(); ++i) {
if (html[i] == '<')
builder.append("&lt;");
builder.append("&lt;"sv);
else if (html[i] == '>')
builder.append("&gt;");
builder.append("&gt;"sv);
else if (html[i] == '&')
builder.append("&amp;");
builder.append("&amp;"sv);
else if (html[i] == '"')
builder.append("&quot;");
builder.append("&quot;"sv);
else
builder.append(html[i]);
}

View file

@ -180,19 +180,19 @@ ErrorOr<void> StringBuilder::try_append_escaped_for_json(StringView string)
for (auto ch : string) {
switch (ch) {
case '\b':
TRY(try_append("\\b"));
TRY(try_append("\\b"sv));
break;
case '\n':
TRY(try_append("\\n"));
TRY(try_append("\\n"sv));
break;
case '\t':
TRY(try_append("\\t"));
TRY(try_append("\\t"sv));
break;
case '\"':
TRY(try_append("\\\""));
TRY(try_append("\\\""sv));
break;
case '\\':
TRY(try_append("\\\\"));
TRY(try_append("\\\\"sv));
break;
default:
if (ch >= 0 && ch <= 0x1f)

View file

@ -329,7 +329,7 @@ StringView trim(StringView str, StringView characters, TrimMode mode)
if (mode == TrimMode::Left || mode == TrimMode::Both) {
for (size_t i = 0; i < str.length(); ++i) {
if (substring_length == 0)
return "";
return ""sv;
if (!characters.contains(str[i]))
break;
++substring_start;
@ -340,7 +340,7 @@ StringView trim(StringView str, StringView characters, TrimMode mode)
if (mode == TrimMode::Right || mode == TrimMode::Both) {
for (size_t i = str.length() - 1; i > 0; --i) {
if (substring_length == 0)
return "";
return ""sv;
if (!characters.contains(str[i]))
break;
--substring_length;
@ -352,7 +352,7 @@ StringView trim(StringView str, StringView characters, TrimMode mode)
StringView trim_whitespace(StringView str, TrimMode mode)
{
return trim(str, " \n\t\v\f\r", mode);
return trim(str, " \n\t\v\f\r"sv, mode);
}
Optional<size_t> find(StringView haystack, char needle, size_t start)

View file

@ -221,7 +221,7 @@ String URL::serialize_data_url() const
builder.append(':');
builder.append(m_data_mime_type);
if (m_data_payload_is_base64)
builder.append(";base64");
builder.append(";base64"sv);
builder.append(',');
// NOTE: The specification does not say anything about encoding this, but we should encode at least control and non-ASCII
// characters (since this is also a valid representation of the same data URL).
@ -239,7 +239,7 @@ String URL::serialize(ExcludeFragment exclude_fragment) const
builder.append(':');
if (!m_host.is_null()) {
builder.append("//");
builder.append("//"sv);
if (includes_credentials()) {
builder.append(percent_encode(m_username, PercentEncodeSet::Userinfo));
@ -259,7 +259,7 @@ String URL::serialize(ExcludeFragment exclude_fragment) const
builder.append(percent_encode(m_paths[0], PercentEncodeSet::Path));
} else {
if (m_host.is_null() && m_paths.size() > 1 && m_paths[0].is_empty())
builder.append("/.");
builder.append("/."sv);
for (auto& segment : m_paths) {
builder.append('/');
builder.append(percent_encode(segment, PercentEncodeSet::Path));
@ -293,7 +293,7 @@ String URL::serialize_for_display() const
builder.append(':');
if (!m_host.is_null()) {
builder.append("//");
builder.append("//"sv);
builder.append(m_host);
if (m_port.has_value())
builder.appendff(":{}", *m_port);
@ -303,7 +303,7 @@ String URL::serialize_for_display() const
builder.append(percent_encode(m_paths[0], PercentEncodeSet::Path));
} else {
if (m_host.is_null() && m_paths.size() > 1 && m_paths[0].is_empty())
builder.append("/.");
builder.append("/."sv);
for (auto& segment : m_paths) {
builder.append('/');
builder.append(percent_encode(segment, PercentEncodeSet::Path));

View file

@ -155,18 +155,18 @@ static String percent_encode_after_encoding(StringView input, URL::PercentEncode
Optional<URL> URLParser::parse_data_url(StringView raw_input)
{
dbgln_if(URL_PARSER_DEBUG, "URLParser::parse_data_url: Parsing '{}'.", raw_input);
VERIFY(raw_input.starts_with("data:"));
VERIFY(raw_input.starts_with("data:"sv));
auto input = raw_input.substring_view(5);
auto comma_offset = input.find(',');
if (!comma_offset.has_value())
return {};
auto mime_type = StringUtils::trim(input.substring_view(0, comma_offset.value()), "\t\n\f\r ", TrimMode::Both);
auto mime_type = StringUtils::trim(input.substring_view(0, comma_offset.value()), "\t\n\f\r "sv, TrimMode::Both);
auto encoded_body = input.substring_view(comma_offset.value() + 1);
auto body = URL::percent_decode(encoded_body);
bool is_base64_encoded = false;
if (mime_type.ends_with("base64", CaseSensitivity::CaseInsensitive)) {
if (mime_type.ends_with("base64"sv, CaseSensitivity::CaseInsensitive)) {
auto substring_view = mime_type.substring_view(0, mime_type.length() - 6);
auto trimmed_substring_view = StringUtils::trim(substring_view, " ", TrimMode::Right);
auto trimmed_substring_view = StringUtils::trim(substring_view, " "sv, TrimMode::Right);
if (trimmed_substring_view.ends_with(';')) {
is_base64_encoded = true;
mime_type = trimmed_substring_view.substring_view(0, trimmed_substring_view.length() - 1);
@ -174,14 +174,14 @@ Optional<URL> URLParser::parse_data_url(StringView raw_input)
}
StringBuilder builder;
if (mime_type.starts_with(";") || mime_type.is_empty()) {
builder.append("text/plain");
if (mime_type.starts_with(";"sv) || mime_type.is_empty()) {
builder.append("text/plain"sv);
builder.append(mime_type);
mime_type = builder.string_view();
}
// FIXME: Parse the MIME type's components according to https://mimesniff.spec.whatwg.org/#parse-a-mime-type
URL url { StringUtils::trim(mime_type, "\n\r\t ", TrimMode::Both), move(body), is_base64_encoded };
URL url { StringUtils::trim(mime_type, "\n\r\t "sv, TrimMode::Both), move(body), is_base64_encoded };
dbgln_if(URL_PARSER_DEBUG, "URLParser::parse_data_url: Parsed data URL to be '{}'.", url.serialize());
return url;
}
@ -202,7 +202,7 @@ URL URLParser::parse(StringView raw_input, URL const* base_url, Optional<URL> ur
if (raw_input.is_empty())
return {};
if (raw_input.starts_with("data:")) {
if (raw_input.starts_with("data:"sv)) {
auto maybe_url = parse_data_url(raw_input);
if (!maybe_url.has_value())
return {};
@ -243,9 +243,9 @@ URL URLParser::parse(StringView raw_input, URL const* base_url, Optional<URL> ur
String processed_input = raw_input.substring_view(start_index, end_index - start_index);
// NOTE: This replaces all tab and newline characters with nothing.
if (processed_input.contains("\t") || processed_input.contains("\n")) {
if (processed_input.contains("\t"sv) || processed_input.contains("\n"sv)) {
report_validation_error();
processed_input = processed_input.replace("\t", "", ReplaceMode::All).replace("\n", "", ReplaceMode::All);
processed_input = processed_input.replace("\t"sv, ""sv, ReplaceMode::All).replace("\n"sv, ""sv, ReplaceMode::All);
}
State state = state_override.value_or(State::SchemeStart);
@ -295,7 +295,7 @@ URL URLParser::parse(StringView raw_input, URL const* base_url, Optional<URL> ur
url->m_scheme = buffer.to_string();
buffer.clear();
if (url->scheme() == "file") {
if (!get_remaining().starts_with("//")) {
if (!get_remaining().starts_with("//"sv)) {
report_validation_error();
}
state = State::File;
@ -304,7 +304,7 @@ URL URLParser::parse(StringView raw_input, URL const* base_url, Optional<URL> ur
state = State::SpecialRelativeOrAuthority;
else
state = State::SpecialAuthoritySlashes;
} else if (get_remaining().starts_with("/")) {
} else if (get_remaining().starts_with("/"sv)) {
state = State::PathOrAuthority;
++iterator;
} else {
@ -339,7 +339,7 @@ URL URLParser::parse(StringView raw_input, URL const* base_url, Optional<URL> ur
}
break;
case State::SpecialRelativeOrAuthority:
if (code_point == '/' && get_remaining().starts_with("/")) {
if (code_point == '/' && get_remaining().starts_with("/"sv)) {
state = State::SpecialAuthorityIgnoreSlashes;
++iterator;
} else {
@ -403,7 +403,7 @@ URL URLParser::parse(StringView raw_input, URL const* base_url, Optional<URL> ur
}
break;
case State::SpecialAuthoritySlashes:
if (code_point == '/' && get_remaining().starts_with("/")) {
if (code_point == '/' && get_remaining().starts_with("/"sv)) {
state = State::SpecialAuthorityIgnoreSlashes;
++iterator;
} else {
@ -426,7 +426,7 @@ URL URLParser::parse(StringView raw_input, URL const* base_url, Optional<URL> ur
if (at_sign_seen) {
auto content = buffer.to_string();
buffer.clear();
buffer.append("%40");
buffer.append("%40"sv);
buffer.append(content);
}
at_sign_seen = true;