Skip to content

Commit

Permalink
Everywhere: Add sv suffix to strings relying on StringView(char const*)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sin-ack authored and awesomekling committed Jul 12, 2022
1 parent e5f09ea commit 3f3f455
Show file tree
Hide file tree
Showing 762 changed files with 8,333 additions and 8,334 deletions.
16 changes: 8 additions & 8 deletions AK/DateConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

}
Expand Down
20 changes: 10 additions & 10 deletions AK/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -170,7 +170,7 @@ bool FormatParser::consume_specifier(FormatSpecifier& specifier)
if (!consume_specific('}'))
VERIFY_NOT_REACHED();

specifier.flags = "";
specifier.flags = ""sv;
}

return true;
Expand Down Expand Up @@ -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 {};
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions AK/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
}
};
Expand All @@ -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());
}
};

Expand Down
6 changes: 3 additions & 3 deletions AK/GenericLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class GenericLexer {
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();
Expand Down Expand Up @@ -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);

}

Expand Down
4 changes: 2 additions & 2 deletions AK/JsonObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions AK/JsonObjectSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ class JsonObjectSerializer {
{
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 {};
}

Expand Down Expand Up @@ -234,11 +234,11 @@ class JsonObjectSerializer {
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 {};
}
Expand Down
6 changes: 3 additions & 3 deletions AK/JsonPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions AK/LexicalPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -171,7 +171,7 @@ LexicalPath LexicalPath::prepend(StringView value) const

LexicalPath LexicalPath::parent() const
{
return append("..");
return append(".."sv);
}

}
2 changes: 1 addition & 1 deletion AK/SourceLocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
};

Expand Down
8 changes: 4 additions & 4 deletions AK/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
10 changes: 5 additions & 5 deletions AK/StringBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions AK/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions AK/URL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down
Loading

0 comments on commit 3f3f455

Please sign in to comment.