1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 05:54:58 +00:00

AK: Remove the ctype adapters and use the actual ctype functions instead

This finally takes care of the kind-of excessive boilerplate code that were the
ctype adapters. On the other hand, I had to link `LibC/ctype.cpp` to the Kernel
(for `AK/JsonParser.cpp` and `AK/Format.cpp`). The previous commit actually makes
sense now: the `string.h` includes in `ctype.{h,cpp}` would require to link more LibC
stuff to the Kernel when it only needs the `_ctype_` array of `ctype.cpp`, and there
wasn't any string stuff used in ctype.
Instead of all this I could have put static derivatives of `is_any_of()` in the
concerned AK files, however that would have meant more boilerplate and workarounds;
so I went for the Kernel approach.
This commit is contained in:
Benoît Lormeau 2020-09-27 12:44:03 +02:00 committed by Andreas Kling
parent f158cb27ea
commit f0f6b09acb
8 changed files with 33 additions and 125 deletions

View file

@ -28,6 +28,7 @@
#include <AK/JsonObject.h>
#include <AK/JsonParser.h>
#include <AK/Memory.h>
#include <ctype.h>
namespace AK {
@ -104,27 +105,27 @@ Optional<JsonValue> JsonParser::parse_object()
if (!consume_specific('{'))
return {};
for (;;) {
ignore_while(is_whitespace);
ignore_while(isspace);
if (peek() == '}')
break;
ignore_while(is_whitespace);
ignore_while(isspace);
auto name = consume_and_unescape_string();
if (name.is_null())
return {};
ignore_while(is_whitespace);
ignore_while(isspace);
if (!consume_specific(':'))
return {};
ignore_while(is_whitespace);
ignore_while(isspace);
auto value = parse_helper();
if (!value.has_value())
return {};
object.set(name, move(value.value()));
ignore_while(is_whitespace);
ignore_while(isspace);
if (peek() == '}')
break;
if (!consume_specific(','))
return {};
ignore_while(is_whitespace);
ignore_while(isspace);
if (peek() == '}')
return {};
}
@ -139,23 +140,23 @@ Optional<JsonValue> JsonParser::parse_array()
if (!consume_specific('['))
return {};
for (;;) {
ignore_while(is_whitespace);
ignore_while(isspace);
if (peek() == ']')
break;
auto element = parse_helper();
if (!element.has_value())
return {};
array.append(element.value());
ignore_while(is_whitespace);
ignore_while(isspace);
if (peek() == ']')
break;
if (!consume_specific(','))
return {};
ignore_while(is_whitespace);
ignore_while(isspace);
if (peek() == ']')
return {};
}
ignore_while(is_whitespace);
ignore_while(isspace);
if (!consume_specific(']'))
return {};
return array;
@ -260,7 +261,7 @@ Optional<JsonValue> JsonParser::parse_null()
Optional<JsonValue> JsonParser::parse_helper()
{
ignore_while(is_whitespace);
ignore_while(isspace);
auto type_hint = peek();
switch (type_hint) {
case '{':
@ -297,7 +298,7 @@ Optional<JsonValue> JsonParser::parse()
auto result = parse_helper();
if (!result.has_value())
return {};
ignore_while(is_whitespace);
ignore_while(isspace);
if (!is_eof())
return {};
return result;