Skip to content

Commit

Permalink
Everywhere: Split Error::from_string_literal and Error::from_string_view
Browse files Browse the repository at this point in the history
Error::from_string_literal now takes direct char const*s, while
Error::from_string_view does what Error::from_string_literal used to do:
taking StringViews. This change will remove the need to insert `sv`
after error strings when returning string literal errors once
StringView(char const*) is removed.

No functional changes.
  • Loading branch information
sin-ack authored and awesomekling committed Jul 12, 2022
1 parent c70f45f commit e5f09ea
Show file tree
Hide file tree
Showing 51 changed files with 282 additions and 261 deletions.
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
56 changes: 28 additions & 28 deletions AK/JsonParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ constexpr bool is_space(int ch)
ErrorOr<String> JsonParser::consume_and_unescape_string()
{
if (!consume_specific('"'))
return Error::from_string_literal("JsonParser: Expected '\"'"sv);
return Error::from_string_literal("JsonParser: Expected '\"'");
StringBuilder final_sb;

for (;;) {
Expand All @@ -33,7 +33,7 @@ ErrorOr<String> JsonParser::consume_and_unescape_string()
if (ch == '"' || ch == '\\')
break;
if (is_ascii_c0_control(ch))
return Error::from_string_literal("JsonParser: Error while parsing string"sv);
return Error::from_string_literal("JsonParser: Error while parsing string");
++peek_index;
}

Expand Down Expand Up @@ -102,20 +102,20 @@ ErrorOr<String> JsonParser::consume_and_unescape_string()
if (next_is('u')) {
ignore();
if (tell_remaining() < 4)
return Error::from_string_literal("JsonParser: EOF while parsing Unicode escape"sv);
return Error::from_string_literal("JsonParser: EOF while parsing Unicode escape");

auto code_point = AK::StringUtils::convert_to_uint_from_hex(consume(4));
if (code_point.has_value()) {
final_sb.append_code_point(code_point.value());
continue;
}
return Error::from_string_literal("JsonParser: Error while parsing Unicode escape"sv);
return Error::from_string_literal("JsonParser: Error while parsing Unicode escape");
}

return Error::from_string_literal("JsonParser: Error while parsing string"sv);
return Error::from_string_literal("JsonParser: Error while parsing string");
}
if (!consume_specific('"'))
return Error::from_string_literal("JsonParser: Expected '\"'"sv);
return Error::from_string_literal("JsonParser: Expected '\"'");

return final_sb.to_string();
}
Expand All @@ -124,40 +124,40 @@ ErrorOr<JsonValue> JsonParser::parse_object()
{
JsonObject object;
if (!consume_specific('{'))
return Error::from_string_literal("JsonParser: Expected '{'"sv);
return Error::from_string_literal("JsonParser: Expected '{'");
for (;;) {
ignore_while(is_space);
if (peek() == '}')
break;
ignore_while(is_space);
auto name = TRY(consume_and_unescape_string());
if (name.is_null())
return Error::from_string_literal("JsonParser: Expected object property name"sv);
return Error::from_string_literal("JsonParser: Expected object property name");
ignore_while(is_space);
if (!consume_specific(':'))
return Error::from_string_literal("JsonParser: Expected ':'"sv);
return Error::from_string_literal("JsonParser: Expected ':'");
ignore_while(is_space);
auto value = TRY(parse_helper());
object.set(name, move(value));
ignore_while(is_space);
if (peek() == '}')
break;
if (!consume_specific(','))
return Error::from_string_literal("JsonParser: Expected ','"sv);
return Error::from_string_literal("JsonParser: Expected ','");
ignore_while(is_space);
if (peek() == '}')
return Error::from_string_literal("JsonParser: Unexpected '}'"sv);
return Error::from_string_literal("JsonParser: Unexpected '}'");
}
if (!consume_specific('}'))
return Error::from_string_literal("JsonParser: Expected '}'"sv);
return Error::from_string_literal("JsonParser: Expected '}'");
return JsonValue { move(object) };
}

ErrorOr<JsonValue> JsonParser::parse_array()
{
JsonArray array;
if (!consume_specific('['))
return Error::from_string_literal("JsonParser: Expected '['"sv);
return Error::from_string_literal("JsonParser: Expected '['");
for (;;) {
ignore_while(is_space);
if (peek() == ']')
Expand All @@ -168,14 +168,14 @@ ErrorOr<JsonValue> JsonParser::parse_array()
if (peek() == ']')
break;
if (!consume_specific(','))
return Error::from_string_literal("JsonParser: Expected ','"sv);
return Error::from_string_literal("JsonParser: Expected ','");
ignore_while(is_space);
if (peek() == ']')
return Error::from_string_literal("JsonParser: Unexpected ']'"sv);
return Error::from_string_literal("JsonParser: Unexpected ']'");
}
ignore_while(is_space);
if (!consume_specific(']'))
return Error::from_string_literal("JsonParser: Expected ']'"sv);
return Error::from_string_literal("JsonParser: Expected ']'");
return JsonValue { move(array) };
}

Expand All @@ -197,7 +197,7 @@ ErrorOr<JsonValue> JsonParser::parse_number()
char ch = peek();
if (ch == '.') {
if (is_double)
return Error::from_string_literal("JsonParser: Multiple '.' in number"sv);
return Error::from_string_literal("JsonParser: Multiple '.' in number");

is_double = true;
++m_index;
Expand All @@ -209,18 +209,18 @@ ErrorOr<JsonValue> JsonParser::parse_number()

if (is_double) {
if (ch == '-')
return Error::from_string_literal("JsonParser: Error while parsing number"sv);
return Error::from_string_literal("JsonParser: Error while parsing number");

fraction_buffer.append(ch);
} else {
if (number_buffer.size() > 0) {
if (number_buffer.at(0) == '0')
return Error::from_string_literal("JsonParser: Error while parsing number"sv);
return Error::from_string_literal("JsonParser: Error while parsing number");
}

if (number_buffer.size() > 1) {
if (number_buffer.at(0) == '-' && number_buffer.at(1) == '0')
return Error::from_string_literal("JsonParser: Error while parsing number"sv);
return Error::from_string_literal("JsonParser: Error while parsing number");
}

number_buffer.append(ch);
Expand All @@ -247,14 +247,14 @@ ErrorOr<JsonValue> JsonParser::parse_number()
} else {
auto number = number_string.to_int();
if (!number.has_value())
return Error::from_string_literal("JsonParser: Error while parsing number"sv);
return Error::from_string_literal("JsonParser: Error while parsing number");
whole = number.value();
}

StringView fraction_string(fraction_buffer.data(), fraction_buffer.size());
auto fraction_string_uint = fraction_string.to_uint<u64>();
if (!fraction_string_uint.has_value())
return Error::from_string_literal("JsonParser: Error while parsing number"sv);
return Error::from_string_literal("JsonParser: Error while parsing number");
auto fraction = static_cast<double>(fraction_string_uint.value());
double sign = (whole < 0) ? -1 : 1;

Expand All @@ -272,7 +272,7 @@ ErrorOr<JsonValue> JsonParser::parse_number()
} else {
auto number = number_string.to_int<i64>();
if (!number.has_value())
return Error::from_string_literal("JsonParser: Error while parsing number"sv);
return Error::from_string_literal("JsonParser: Error while parsing number");
if (number.value() <= NumericLimits<i32>::max()) {
value = JsonValue((i32)number.value());
} else {
Expand All @@ -289,21 +289,21 @@ ErrorOr<JsonValue> JsonParser::parse_number()
ErrorOr<JsonValue> JsonParser::parse_true()
{
if (!consume_specific("true"))
return Error::from_string_literal("JsonParser: Expected 'true'"sv);
return Error::from_string_literal("JsonParser: Expected 'true'");
return JsonValue(true);
}

ErrorOr<JsonValue> JsonParser::parse_false()
{
if (!consume_specific("false"))
return Error::from_string_literal("JsonParser: Expected 'false'"sv);
return Error::from_string_literal("JsonParser: Expected 'false'");
return JsonValue(false);
}

ErrorOr<JsonValue> JsonParser::parse_null()
{
if (!consume_specific("null"))
return Error::from_string_literal("JsonParser: Expected 'null'"sv);
return Error::from_string_literal("JsonParser: Expected 'null'");
return JsonValue(JsonValue::Type::Null);
}

Expand Down Expand Up @@ -338,15 +338,15 @@ ErrorOr<JsonValue> JsonParser::parse_helper()
return parse_null();
}

return Error::from_string_literal("JsonParser: Unexpected character"sv);
return Error::from_string_literal("JsonParser: Unexpected character");
}

ErrorOr<JsonValue> JsonParser::parse()
{
auto result = TRY(parse_helper());
ignore_while(is_space);
if (!is_eof())
return Error::from_string_literal("JsonParser: Didn't consume all input"sv);
return Error::from_string_literal("JsonParser: Didn't consume all input");
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion AK/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Stream {
{
if (!handle_any_error())
return {};
return Error::from_string_literal("Stream error"sv);
return Error::from_string_literal("Stream error");
}

virtual void set_recoverable_error() const { m_recoverable_error = true; }
Expand Down
34 changes: 17 additions & 17 deletions Meta/Lagom/Tools/CodeGenerators/LibEDID/GeneratePnpIDs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static ErrorOr<String> decode_html_entities(StringView const& str)
}

if (!found_entity)
return Error::from_string_literal("Failed to decode html entity"sv);
return Error::from_string_literal("Failed to decode html entity");

if (entity_start.value() != start)
decoded_str.append(str.substring_view(start, entity_start.value() - start));
Expand All @@ -94,25 +94,25 @@ static ErrorOr<ApprovalDate> parse_approval_date(StringView const& str)
{
auto parts = str.trim_whitespace().split_view('/', true);
if (parts.size() != 3)
return Error::from_string_literal("Failed to parse approval date parts (mm/dd/yyyy)"sv);
return Error::from_string_literal("Failed to parse approval date parts (mm/dd/yyyy)");

auto month = parts[0].to_uint();
if (!month.has_value())
return Error::from_string_literal("Failed to parse month from approval date"sv);
return Error::from_string_literal("Failed to parse month from approval date");
if (month.value() == 0 || month.value() > 12)
return Error::from_string_literal("Invalid month in approval date"sv);
return Error::from_string_literal("Invalid month in approval date");

auto day = parts[1].to_uint();
if (!day.has_value())
return Error::from_string_literal("Failed to parse day from approval date"sv);
return Error::from_string_literal("Failed to parse day from approval date");
if (day.value() == 0 || day.value() > 31)
return Error::from_string_literal("Invalid day in approval date"sv);
return Error::from_string_literal("Invalid day in approval date");

auto year = parts[2].to_uint();
if (!year.has_value())
return Error::from_string_literal("Failed to parse year from approval date"sv);
return Error::from_string_literal("Failed to parse year from approval date");
if (year.value() < 1900 || year.value() > 2999)
return Error::from_string_literal("Invalid year approval date"sv);
return Error::from_string_literal("Invalid year approval date");

return ApprovalDate { .year = year.value(), .month = month.value(), .day = day.value() };
}
Expand All @@ -132,15 +132,15 @@ static ErrorOr<HashMap<String, PnpIdData>> parse_pnp_ids_database(Core::File& pn

auto row_start_tag_end = pnp_ids_file_contents.find(">"sv, row_start.value() + row_start_tag.length());
if (!row_start_tag_end.has_value())
return Error::from_string_literal("Incomplete row start tag"sv);
return Error::from_string_literal("Incomplete row start tag");

static auto const row_end_tag = "</tr>"sv;
auto row_end = pnp_ids_file_contents.find(row_end_tag, row_start.value());
if (!row_end.has_value())
return Error::from_string_literal("No matching row end tag found"sv);
return Error::from_string_literal("No matching row end tag found");

if (row_start_tag_end.value() > row_end.value() + row_end_tag.length())
return Error::from_string_literal("Invalid row start tag"sv);
return Error::from_string_literal("Invalid row start tag");

auto row_string = pnp_ids_file_contents.substring_view(row_start_tag_end.value() + 1, row_end.value() - row_start_tag_end.value() - 1);
Vector<String, (size_t)PnpIdColumns::ColumnCount> columns;
Expand All @@ -153,31 +153,31 @@ static ErrorOr<HashMap<String, PnpIdData>> parse_pnp_ids_database(Core::File& pn
static auto const column_end_tag = "</td>"sv;
auto column_end = row_string.find(column_end_tag, column_start.value() + column_start_tag.length());
if (!column_end.has_value())
return Error::from_string_literal("No matching column end tag found"sv);
return Error::from_string_literal("No matching column end tag found");

auto column_content_row_offset = column_start.value() + column_start_tag.length();
auto column_str = row_string.substring_view(column_content_row_offset, column_end.value() - column_content_row_offset).trim_whitespace();
if (column_str.find('\"').has_value())
return Error::from_string_literal("Found '\"' in column content, escaping not supported!"sv);
return Error::from_string_literal("Found '\"' in column content, escaping not supported!");
columns.append(column_str);

column_row_offset = column_end.value() + column_end_tag.length();
}

if (columns.size() != (size_t)PnpIdColumns::ColumnCount)
return Error::from_string_literal("Unexpected number of columns found"sv);
return Error::from_string_literal("Unexpected number of columns found");

auto approval_date = TRY(parse_approval_date(columns[(size_t)PnpIdColumns::ApprovalDate]));
auto decoded_manufacturer_name = TRY(decode_html_entities(columns[(size_t)PnpIdColumns::ManufacturerName]));
auto hash_set_result = pnp_id_data.set(columns[(size_t)PnpIdColumns::ManufacturerId], PnpIdData { .manufacturer_name = decoded_manufacturer_name, .approval_date = move(approval_date) });
if (hash_set_result != AK::HashSetResult::InsertedNewEntry)
return Error::from_string_literal("Duplicate manufacturer ID encountered"sv);
return Error::from_string_literal("Duplicate manufacturer ID encountered");

row_content_offset = row_end.value() + row_end_tag.length();
}

if (pnp_id_data.size() <= 1)
return Error::from_string_literal("Expected more than one row"sv);
return Error::from_string_literal("Expected more than one row");

return pnp_id_data;
}
Expand Down Expand Up @@ -283,7 +283,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto open_file = [&](StringView path, Core::OpenMode mode = Core::OpenMode::ReadOnly) -> ErrorOr<NonnullRefPtr<Core::File>> {
if (path.is_empty()) {
args_parser.print_usage(stderr, arguments.argv[0]);
return Error::from_string_literal("Must provide all command line options"sv);
return Error::from_string_literal("Must provide all command line options");
}

return Core::File::open(path, mode);
Expand Down
Loading

0 comments on commit e5f09ea

Please sign in to comment.