Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Everywhere: Remove AK::StringView(char const*) :^) #14555

Merged
merged 25 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a4eee80
AK: Make CheckedFormatString pass the char array size to StringView
sin-ack Jul 11, 2022
4d5a719
AK: Explicitly calculate length of char* when printing
sin-ack Jul 11, 2022
79a7c63
AK: Add string literal helpers to AK::SourceGenerator
sin-ack Jul 12, 2022
0184889
Meta+Userland: Simplify some formatters
sin-ack Jul 11, 2022
7f4f24e
LibChess: Add convenience constructor for Chess::Square
sin-ack Jul 11, 2022
2b04aa1
LibJS: Emit StringViews for ErrorType instances
sin-ack Jul 11, 2022
fa829f4
Tests: Make TestSourceLocation basic_scenario specify StringView length
sin-ack Jul 11, 2022
ad11148
Tests: Convert TestBase64 decode test to use StringViews directly
sin-ack Jul 11, 2022
5280a74
Tests: Convert TestQuotedPrintable decode test to use StringViews
sin-ack Jul 11, 2022
6b3d3e6
Userland: Convert command line arguments to String/StringView
sin-ack Jul 11, 2022
800d27e
Userland: Remove erroneous String -> char* -> StringView conversions
sin-ack Jul 11, 2022
4b56df6
LibX86: Convert register names to StringViews
sin-ack Jul 11, 2022
2fd97ab
LibCore: Add convenience templates for System::{unveil,pledge}
sin-ack Jul 11, 2022
d80a19d
LibC: Convert getopt and getopt_long to new StringView usage
sin-ack Jul 11, 2022
178b053
Everywhere: Explicitly specify the size in StringView constructors
sin-ack Jul 11, 2022
9186d21
Everywhere: Split Error::from_string_literal and Error::from_string_view
sin-ack Jul 11, 2022
7a9bbfc
Everywhere: Add sv suffix to strings relying on StringView(char const*)
sin-ack Jul 11, 2022
326099c
Everywhere: Replace single-char StringView op. arguments with chars
sin-ack Jul 11, 2022
e67ba36
Everywhere: Use default StringView constructor over nullptr
sin-ack Jul 11, 2022
98474cb
LibCore: Add FIXME note about converting Core::Account to use StringView
sin-ack Jul 11, 2022
87d8d26
LibRegex: Remove RegexStringView(char const*) constructor
sin-ack Jul 11, 2022
c306fee
AK: Remove String <-> char const* comparison operators
sin-ack Jul 11, 2022
cb75f32
AK+Userland+Tests: Remove URL(char const*) constructor
sin-ack Jul 11, 2022
9b21264
Tests: Remove StringView char const* initialization test
sin-ack Jul 11, 2022
54094f6
AK: Remove StringView(char const*) :^)
sin-ack Jul 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion AK/CheckedFormatString.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ template<typename... Args>
struct CheckedFormatString {
template<size_t N>
consteval CheckedFormatString(char const (&fmt)[N])
: m_string { fmt }
: m_string { fmt, N - 1 }
{
#ifdef ENABLE_COMPILETIME_FORMAT_CHECK
check_format_parameter_consistency<N, sizeof...(Args)>(fmt);
Expand Down
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
2 changes: 1 addition & 1 deletion AK/Demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ inline String demangle(StringView name)
{
int status = 0;
auto* demangled_name = abi::__cxa_demangle(name.to_string().characters(), nullptr, nullptr, &status);
auto string = String(status == 0 ? demangled_name : name);
auto string = String(status == 0 ? StringView { demangled_name, strlen(demangled_name) } : name);
if (status == 0)
free(demangled_name);
return string;
Expand Down
14 changes: 13 additions & 1 deletion AK/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ class Error {
public:
static Error from_errno(int code) { return Error(code); }
static Error from_syscall(StringView syscall_name, int rc) { return Error(syscall_name, rc); }
static Error from_string_literal(StringView string_literal) { return Error(string_literal); }
static Error from_string_view(StringView string_literal) { return Error(string_literal); }

// NOTE: Prefer `from_string_literal` when directly typing out an error message:
//
// return Error::from_string_literal("Class: Some failure");
//
// If you need to return a static string based on a dynamic condition (like
// picking an error from an array), then prefer `from_string_view` instead.
template<size_t N>
ALWAYS_INLINE static Error from_string_literal(char const (&string_literal)[N])
{
return from_string_view(StringView { string_literal, N - 1 });
}

bool operator==(Error const& other) const
{
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
21 changes: 12 additions & 9 deletions AK/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ template<>
struct Formatter<Bytes> : Formatter<ReadonlyBytes> {
};

// FIXME: Printing raw char pointers is inherently dangerous. Remove this and
// its users and prefer StringView over it.
template<>
struct Formatter<char const*> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, char const* value)
Expand All @@ -403,7 +405,8 @@ struct Formatter<char const*> : Formatter<StringView> {
Formatter<FlatPtr> formatter { *this };
return formatter.format(builder, reinterpret_cast<FlatPtr>(value));
}
return Formatter<StringView>::format(builder, value);

return Formatter<StringView>::format(builder, value != nullptr ? StringView { value, __builtin_strlen(value) } : "(null)"sv);
}
};
template<>
Expand Down Expand Up @@ -635,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 @@ -670,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 @@ -688,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
8 changes: 4 additions & 4 deletions AK/GenericLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ class GenericLexer {

constexpr bool consume_specific(char const* next)
{
return consume_specific(StringView { next });
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
8 changes: 4 additions & 4 deletions AK/JsonObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ inline void JsonValue::serialize(Builder& builder) const
{
switch (m_type) {
case Type::String: {
builder.append("\"");
builder.append('\"');
builder.append_escaped_for_json({ m_value.as_string->characters(), m_value.as_string->length() });
builder.append("\"");
builder.append('\"');
} break;
case Type::Array:
m_value.as_array->serialize(builder);
Expand All @@ -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
12 changes: 6 additions & 6 deletions AK/JsonObjectSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ class JsonObjectSerializer {
TRY(begin_item(key));
if constexpr (IsLegacyBuilder<Builder>) {
TRY(m_builder.try_append('"'));
TRY(m_builder.try_append_escaped_for_json(value));
TRY(m_builder.try_append_escaped_for_json({ value, strlen(value) }));
TRY(m_builder.try_append('"'));
} else {
TRY(m_builder.append('"'));
TRY(m_builder.append_escaped_for_json(value));
TRY(m_builder.append_escaped_for_json({ value, strlen(value) }));
TRY(m_builder.append('"'));
}
return {};
Expand All @@ -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