diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-08-04 11:47:21 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-08-04 11:47:21 +0200 |
commit | b62a12c687bf1f6d6e0dde129ef783139592e6c0 (patch) | |
tree | ee382853ce303fdc022fd16c08e07199505911e5 /AK | |
parent | 93596dc00db7eef6763765fdc45686cfe0f53929 (diff) | |
download | serenity-b62a12c687bf1f6d6e0dde129ef783139592e6c0.zip |
JsonParser: Some minor optimizations
- Return more specific types from parse_array() and parse_object().
- Don't create a throwaway String in extract_while().
- Use a StringView in parse_number() to avoid a throwaway String.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/JsonParser.cpp | 13 | ||||
-rw-r--r-- | AK/JsonParser.h | 6 |
2 files changed, 10 insertions, 9 deletions
diff --git a/AK/JsonParser.cpp b/AK/JsonParser.cpp index 54fdbe4888..9d604fa2a8 100644 --- a/AK/JsonParser.cpp +++ b/AK/JsonParser.cpp @@ -31,12 +31,12 @@ void JsonParser::consume_while(C condition) } template<typename C> -String JsonParser::extract_while(C condition) +Vector<char, 128> JsonParser::extract_while(C condition) { - Vector<char, 1024> buffer; + Vector<char, 128> buffer; while (condition(peek())) buffer.append(consume()); - return String::copy(buffer); + return buffer; }; void JsonParser::consume_whitespace() @@ -95,7 +95,7 @@ String JsonParser::consume_quoted_string() return String::copy(buffer); } -JsonValue JsonParser::parse_object() +JsonObject JsonParser::parse_object() { JsonObject object; consume_specific('{'); @@ -119,7 +119,7 @@ JsonValue JsonParser::parse_object() return object; } -JsonValue JsonParser::parse_array() +JsonArray JsonParser::parse_array() { JsonArray array; consume_specific('['); @@ -145,7 +145,8 @@ JsonValue JsonParser::parse_string() JsonValue JsonParser::parse_number() { - auto number_string = extract_while([](char ch) { return ch == '-' || (ch >= '0' && ch <= '9'); }); + auto number_buffer = extract_while([](char ch) { return ch == '-' || (ch >= '0' && ch <= '9'); }); + StringView number_string(number_buffer.data(), number_buffer.size()); bool ok; auto value = JsonValue(number_string.to_uint(ok)); if (!ok) diff --git a/AK/JsonParser.h b/AK/JsonParser.h index 76f7d7e5bf..343530d7f4 100644 --- a/AK/JsonParser.h +++ b/AK/JsonParser.h @@ -23,8 +23,8 @@ private: void consume_specific(char expected_ch); void consume_string(const char*); String consume_quoted_string(); - JsonValue parse_array(); - JsonValue parse_object(); + JsonArray parse_array(); + JsonObject parse_object(); JsonValue parse_number(); JsonValue parse_string(); JsonValue parse_false(); @@ -36,7 +36,7 @@ private: void consume_while(C); template<typename C> - String extract_while(C); + Vector<char, 128> extract_while(C); StringView m_input; int m_index { 0 }; |