From d108343bd013480b01b5ecd4f532a21329f3d961 Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Tue, 11 Feb 2020 14:53:09 -0800 Subject: [PATCH] Victor/json/remove span reader from json (#342) * remove az_result_byte and az_span_reader --- sdk/core/core/CMakeLists.txt | 1 - sdk/core/core/inc/az_json.h | 3 +- sdk/core/core/inc/az_span_reader.h | 51 ------ sdk/core/core/src/az_http_response_parser.c | 17 +- sdk/core/core/src/az_json_builder.c | 4 +- sdk/core/core/src/az_json_get.c | 7 +- sdk/core/core/src/az_json_parser.c | 190 +++++++++++++------- sdk/core/core/src/az_json_pointer.c | 71 +++++--- sdk/core/core/src/az_json_string.c | 91 ++++++---- sdk/core/core/src/az_json_string_private.h | 10 +- sdk/core/core/src/az_span.c | 48 +++-- sdk/core/core/src/az_span_private.h | 22 +-- sdk/core/core/src/az_span_reader.c | 48 ----- sdk/core/core/src/az_span_reader_private.h | 21 --- sdk/core/core/test/main.c | 13 +- sdk/core/core/test/test_json_pointer.c | 31 ++-- sdk/core/core/test/test_json_string.c | 6 +- 17 files changed, 295 insertions(+), 339 deletions(-) delete mode 100644 sdk/core/core/inc/az_span_reader.h delete mode 100644 sdk/core/core/src/az_span_reader.c delete mode 100644 sdk/core/core/src/az_span_reader_private.h diff --git a/sdk/core/core/CMakeLists.txt b/sdk/core/core/CMakeLists.txt index 6027ec9108..ccb1e3a8e6 100644 --- a/sdk/core/core/CMakeLists.txt +++ b/sdk/core/core/CMakeLists.txt @@ -23,7 +23,6 @@ add_library ( src/az_json_token.c src/az_log.c src/az_span.c - src/az_span_reader.c ) if(MSVC) diff --git a/sdk/core/core/inc/az_json.h b/sdk/core/core/inc/az_json.h index 2836ed098d..9e0fd1a324 100644 --- a/sdk/core/core/inc/az_json.h +++ b/sdk/core/core/inc/az_json.h @@ -6,7 +6,6 @@ #include #include -#include #include @@ -132,7 +131,7 @@ AZ_NODISCARD az_result az_json_builder_append_array_close(az_json_builder * self typedef uint64_t az_json_stack; typedef struct { struct { - az_span_reader reader; + az_span reader; az_json_stack stack; } _internal; } az_json_parser; diff --git a/sdk/core/core/inc/az_span_reader.h b/sdk/core/core/inc/az_span_reader.h deleted file mode 100644 index 42f6c10756..0000000000 --- a/sdk/core/core/inc/az_span_reader.h +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// SPDX-License-Identifier: MIT - -#ifndef _az_SPAN_READER_H -#define _az_SPAN_READER_H - -#include -#include - -#include -#include -#include - -#include <_az_cfg_prefix.h> - -typedef struct { - az_span span; - int32_t i; -} az_span_reader; - -AZ_NODISCARD AZ_INLINE az_span_reader az_span_reader_create(az_span span) { - return (az_span_reader){ .span = span, .i = 0 }; -} - -AZ_NODISCARD AZ_INLINE bool az_span_reader_is_empty(az_span_reader * p_reader) { - return az_span_length(p_reader->span) <= p_reader->i; -} - -AZ_INLINE void az_span_reader_next(az_span_reader * p_reader) { - if (az_span_reader_is_empty(p_reader)) { - return; - } - p_reader->i += 1; -} - -AZ_NODISCARD az_result az_span_reader_set_pos(az_span_reader * p_reader, int32_t i); - -/** - * Read a span form a reader and compare it with the given @var span - * - * If it doesn't match the given @var span, the function returns AZ_ERROR_UNEXPECTED_CHAR. - */ -AZ_NODISCARD az_result az_span_reader_expect_span(az_span_reader * self, az_span span); - -AZ_NODISCARD az_result az_span_reader_expect_digit(az_span_reader * self, uint8_t * digit); - -AZ_NODISCARD az_result az_span_reader_expect_char(az_span_reader * p_reader, uint8_t expected); - -#include <_az_cfg_suffix.h> - -#endif diff --git a/sdk/core/core/src/az_http_response_parser.c b/sdk/core/core/src/az_http_response_parser.c index 230e1ffc6b..45a930a44e 100644 --- a/sdk/core/core/src/az_http_response_parser.c +++ b/sdk/core/core/src/az_http_response_parser.c @@ -33,21 +33,6 @@ AZ_NODISCARD az_result _az_slice_is_not_http_whitespace(az_span slice) { return _az_is_http_whitespace(az_span_ptr(slice)[0]) == true ? AZ_CONTINUE : AZ_OK; } -AZ_NODISCARD az_result _az_is_expected_span(az_span * self, az_span expected) { - az_span actual_span = { 0 }; - - int32_t expected_length = az_span_length(expected); - AZ_RETURN_IF_FAILED(az_span_slice(*self, 0, expected_length, &actual_span)); - - if (!az_span_is_equal(actual_span, expected)) { - return AZ_ERROR_PARSER_UNEXPECTED_CHAR; - } - // move reader after the expected span (means it was parsed as expected) - AZ_RETURN_IF_FAILED(az_span_slice(*self, expected_length, -1, self)); - - return AZ_OK; -} - /* PRIVATE Function. parse next */ AZ_NODISCARD az_result _az_get_digit(az_span * self, uint8_t * save_here) { @@ -194,7 +179,7 @@ AZ_NODISCARD az_result az_http_response_get_next_header(az_http_response * self, int32_t offset = 0; int32_t offset_value_end = offset; while (true) { - uint8_t c = reader->_internal.ptr[offset]; + uint8_t c = az_span_ptr(*reader)[offset]; offset += 1; if (c == '\r') { break; // break as soon as end of value char is found diff --git a/sdk/core/core/src/az_json_builder.c b/sdk/core/core/src/az_json_builder.c index f5baaa01d2..cbbf87577e 100644 --- a/sdk/core/core/src/az_json_builder.c +++ b/sdk/core/core/src/az_json_builder.c @@ -27,7 +27,7 @@ AZ_NODISCARD az_result az_json_builder_write_span(az_json_builder * self, az_spa AZ_RETURN_IF_FAILED(az_span_append(*json, AZ_SPAN_FROM_STR("\""), json)); for (int32_t i = 0; i < az_span_length(value); ++i) { - az_result_byte const c = az_span_ptr(value)[i]; + uint8_t const c = az_span_ptr(value)[i]; // check if the character has to be escaped. { @@ -38,7 +38,7 @@ AZ_NODISCARD az_result az_json_builder_write_span(az_json_builder * self, az_spa } } // check if the character has to be escaped as a UNICODE escape sequence. - if (0 <= c && c < 0x20) { + if (c < 0x20) { uint8_t array[6] = { '\\', 'u', diff --git a/sdk/core/core/src/az_json_get.c b/sdk/core/core/src/az_json_get.c index e1f0cb3344..34d731b5e1 100644 --- a/sdk/core/core/src/az_json_get.c +++ b/sdk/core/core/src/az_json_get.c @@ -7,8 +7,9 @@ #include <_az_cfg.h> AZ_NODISCARD bool az_json_pointer_token_eq_json_string(az_span pointer_token, az_span json_string) { - az_span_reader pt_reader = az_span_reader_create(pointer_token); - az_span_reader js_reader = az_span_reader_create(json_string); + // copy spans to read them + az_span pt_reader = pointer_token; + az_span js_reader = json_string; while (true) { uint32_t pt_c = { 0 }; az_result const pt_result = az_span_reader_read_json_pointer_token_char(&pt_reader, &pt_c); @@ -68,7 +69,7 @@ az_json_parse_by_pointer(az_span json, az_span pointer, az_json_token * out_toke az_json_parser json_parser = { 0 }; AZ_RETURN_IF_FAILED(az_json_parser_init(&json_parser, json)); - az_span_reader pointer_parser = az_span_reader_create(pointer); + az_span pointer_parser = pointer; AZ_RETURN_IF_FAILED(az_json_parser_parse_token(&json_parser, out_token)); diff --git a/sdk/core/core/src/az_json_parser.c b/sdk/core/core/src/az_json_parser.c index 71c18879fe..8ce849d3ba 100644 --- a/sdk/core/core/src/az_json_parser.c +++ b/sdk/core/core/src/az_json_parser.c @@ -4,7 +4,8 @@ #include #include "az_json_string_private.h" -#include "az_span_reader_private.h" +#include "az_span_private.h" +#include #include @@ -20,7 +21,12 @@ typedef enum { AZ_JSON_STACK_ARRAY = 1, } az_json_stack_item; -AZ_NODISCARD AZ_INLINE bool az_json_is_white_space(az_result_byte c) { +/** + * @brief check if input @p c is a white space. Utility function that help discarding empty spaces + * from tokens + * + */ +AZ_NODISCARD AZ_INLINE bool az_json_is_white_space(uint8_t c) { switch (c) { case ' ': case '\t': @@ -31,7 +37,12 @@ AZ_NODISCARD AZ_INLINE bool az_json_is_white_space(az_result_byte c) { return false; } -AZ_NODISCARD AZ_INLINE bool az_json_is_e(az_result_byte c) { +/** + * @brief check if @p c is either an 'e' or an 'E'. This is a helper function to handle exponential + * numbers like 10e10 + * + */ +AZ_NODISCARD AZ_INLINE bool az_json_is_e(uint8_t c) { switch (c) { case 'e': case 'E': @@ -70,15 +81,21 @@ AZ_NODISCARD AZ_INLINE az_result az_json_parser_pop_stack(az_json_parser * self) } AZ_NODISCARD az_result az_json_parser_init(az_json_parser * self, az_span json_buffer) { - *self = (az_json_parser){ ._internal - = { .reader = az_span_reader_create(json_buffer), .stack = 1 } }; + *self = (az_json_parser){ ._internal = { .reader = json_buffer, .stack = 1 } }; return AZ_OK; } -static void az_span_reader_skip_json_white_space(az_span_reader * self) { - while (az_json_is_white_space(az_span_reader_current(self))) { - az_span_reader_next(self); +static az_result az_span_reader_skip_json_white_space(az_span * self) { + if (az_span_length(*self) == 0) { + return AZ_OK; + } + while (az_json_is_white_space(az_span_ptr(*self)[0])) { + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); + if (az_span_length(*self) == 0) { + return AZ_OK; + } } + return AZ_OK; } // 18 decimal digits. 10^18 - 1. @@ -98,11 +115,11 @@ AZ_NODISCARD static double az_json_number_to_double(az_dec_number const * p) { } AZ_NODISCARD static az_result az_span_reader_get_json_number_int( - az_span_reader * self, + az_span * self, az_dec_number * p_n, int16_t e_offset, - az_result_byte first) { - az_result_byte c = first; + uint8_t first) { + uint8_t c = first; // read an integer part of the number while (true) { int d = c - '0'; @@ -115,8 +132,11 @@ AZ_NODISCARD static az_result az_span_reader_get_json_number_int( } p_n->exp += e_offset + 1; } - az_span_reader_next(self); - c = az_span_reader_current(self); + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); + if (az_span_length(*self) == 0) { + return AZ_OK; // end of reader is fine. Means int number is over + } + c = az_span_ptr(*self)[0]; if (!isdigit(c)) { return AZ_OK; } @@ -124,7 +144,7 @@ AZ_NODISCARD static az_result az_span_reader_get_json_number_int( } AZ_NODISCARD static az_result az_span_reader_get_json_number_digit_rest( - az_span_reader * self, + az_span * self, double * out_value) { az_dec_number i = { .sign = 1, @@ -135,37 +155,55 @@ AZ_NODISCARD static az_result az_span_reader_get_json_number_digit_rest( // integer part { - az_result_byte o = az_span_reader_current(self); + uint8_t o = az_span_ptr(*self)[0]; if (o == '-') { i.sign = -1; - az_span_reader_next(self); - o = az_span_reader_current(self); + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); + if (az_span_length(*self) == 0) { + return AZ_ERROR_EOF; + } + o = az_span_ptr(*self)[0]; if (!isdigit(o)) { - return az_error_unexpected_char(o); + return AZ_ERROR_PARSER_UNEXPECTED_CHAR; } } if (o != '0') { AZ_RETURN_IF_FAILED(az_span_reader_get_json_number_int(self, &i, 0, o)); } else { - az_span_reader_next(self); + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); } } + if (az_span_length(*self) == 0) { + *out_value = az_json_number_to_double(&i); + return AZ_OK; // it's fine is int finish here (no fraction or something else) + } // fraction - if (az_span_reader_current(self) == '.') { - az_span_reader_next(self); - az_result_byte o = az_span_reader_current(self); + if (az_span_ptr(*self)[0] == '.') { + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); + if (az_span_length(*self) == 0) { + return AZ_ERROR_EOF; // uncompleted number + } + uint8_t o = az_span_ptr(*self)[0]; if (!isdigit(o)) { - return az_error_unexpected_char(o); + return AZ_ERROR_PARSER_UNEXPECTED_CHAR; } AZ_RETURN_IF_FAILED(az_span_reader_get_json_number_int(self, &i, -1, o)); } + if (az_span_length(*self) == 0) { + *out_value = az_json_number_to_double(&i); + return AZ_OK; // fine if number ends after a fraction + } + // exp - if (az_json_is_e(az_span_reader_current(self))) { + if (az_json_is_e(az_span_ptr(*self)[0])) { // skip 'e' or 'E' - az_span_reader_next(self); - az_result_byte c = az_span_reader_current(self); + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); + if (az_span_length(*self) == 0) { + return AZ_ERROR_EOF; // mising expo info + } + uint8_t c = az_span_ptr(*self)[0]; // read sign, if any. int8_t e_sign = 1; @@ -174,20 +212,26 @@ AZ_NODISCARD static az_result az_span_reader_get_json_number_digit_rest( e_sign = -1; AZ_FALLTHROUGH; case '+': - az_span_reader_next(self); - c = az_span_reader_current(self); + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); + if (az_span_length(*self) == 0) { + return AZ_ERROR_EOF; // uncompleted exp data + } + c = az_span_ptr(*self)[0]; } // expect at least one digit. if (!isdigit(c)) { - return az_error_unexpected_char(c); + return AZ_ERROR_PARSER_UNEXPECTED_CHAR; } int16_t e_int = 0; do { e_int = e_int * 10 + (int16_t)(c - '0'); - az_span_reader_next(self); - c = az_span_reader_current(self); + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); + if (az_span_length(*self) == 0) { + break; // nothing more to read + } + c = az_span_ptr(*self)[0]; } while (isdigit(c)); i.exp += e_int * e_sign; } @@ -197,25 +241,25 @@ AZ_NODISCARD static az_result az_span_reader_get_json_number_digit_rest( } AZ_NODISCARD static az_result az_span_reader_get_json_string_rest( - az_span_reader * self, + az_span * self, az_span * string) { // skip '"' - size_t const begin = self->i; + int32_t reader_initial_length = az_span_capacity(*self); + uint8_t * p_reader = az_span_ptr(*self); while (true) { uint32_t ignore = { 0 }; az_result const result = az_span_reader_read_json_string_char(self, &ignore); switch (result) { case AZ_ERROR_JSON_STRING_END: { - AZ_RETURN_IF_FAILED(az_span_slice(self->span, (int32_t)begin, (int32_t)self->i, string)); - az_span_reader_next(self); + int32_t read_count = reader_initial_length - az_span_capacity(*self); + *string = az_span_init(p_reader, read_count, read_count); + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); return AZ_OK; } case AZ_ERROR_ITEM_NOT_FOUND: { return AZ_ERROR_EOF; } - default: { - AZ_RETURN_IF_FAILED(result); - } + default: { AZ_RETURN_IF_FAILED(result); } } } } @@ -224,8 +268,13 @@ AZ_NODISCARD static az_result az_span_reader_get_json_string_rest( AZ_NODISCARD static az_result az_json_parser_get_value( az_json_parser * self, az_json_token * out_token) { - az_span_reader * const p_reader = &self->_internal.reader; - az_result_byte const c = az_span_reader_current(p_reader); + az_span * p_reader = &self->_internal.reader; + + if (az_span_length(*p_reader) == 0) { + return AZ_ERROR_EOF; + } + + uint8_t c = az_span_ptr(*p_reader)[0]; if (isdigit(c)) { out_token->kind = AZ_JSON_TOKEN_NUMBER; return az_span_reader_get_json_number_digit_rest(p_reader, &out_token->value.number); @@ -234,38 +283,40 @@ AZ_NODISCARD static az_result az_json_parser_get_value( case 't': out_token->kind = AZ_JSON_TOKEN_BOOLEAN; out_token->value.boolean = true; - return az_span_reader_expect_span(p_reader, AZ_SPAN_FROM_STR("true")); + return _az_is_expected_span(p_reader, AZ_SPAN_FROM_STR("true")); case 'f': out_token->kind = AZ_JSON_TOKEN_BOOLEAN; out_token->value.boolean = false; - return az_span_reader_expect_span(p_reader, AZ_SPAN_FROM_STR("false")); + return _az_is_expected_span(p_reader, AZ_SPAN_FROM_STR("false")); case 'n': out_token->kind = AZ_JSON_TOKEN_NULL; - return az_span_reader_expect_span(p_reader, AZ_SPAN_FROM_STR("null")); + return _az_is_expected_span(p_reader, AZ_SPAN_FROM_STR("null")); case '"': out_token->kind = AZ_JSON_TOKEN_STRING; - az_span_reader_next(p_reader); + AZ_RETURN_IF_FAILED(az_span_slice(*p_reader, 1, -1, p_reader)); return az_span_reader_get_json_string_rest(p_reader, &out_token->value.string); case '-': out_token->kind = AZ_JSON_TOKEN_NUMBER; return az_span_reader_get_json_number_digit_rest(p_reader, &out_token->value.number); case '{': out_token->kind = AZ_JSON_TOKEN_OBJECT; - az_span_reader_next(p_reader); + AZ_RETURN_IF_FAILED(az_span_slice(*p_reader, 1, -1, p_reader)); return az_json_parser_push_stack(self, AZ_JSON_STACK_OBJECT); case '[': out_token->kind = AZ_JSON_TOKEN_ARRAY; - az_span_reader_next(p_reader); + AZ_RETURN_IF_FAILED(az_span_slice(*p_reader, 1, -1, p_reader)); return az_json_parser_push_stack(self, AZ_JSON_STACK_ARRAY); } - return az_error_unexpected_char(c); + return AZ_ERROR_PARSER_UNEXPECTED_CHAR; } AZ_NODISCARD static az_result az_json_parser_get_value_space( az_json_parser * p_state, az_json_token * out_token) { AZ_RETURN_IF_FAILED(az_json_parser_get_value(p_state, out_token)); - az_span_reader_skip_json_white_space(&p_state->_internal.reader); + if (az_span_length(p_state->_internal.reader) > 0) { + AZ_RETURN_IF_FAILED(az_span_reader_skip_json_white_space(&p_state->_internal.reader)); + } return AZ_OK; } @@ -277,10 +328,10 @@ az_json_parser_parse_token(az_json_parser * self, az_json_token * out_token) { if (!az_json_parser_stack_is_empty(self)) { return AZ_ERROR_JSON_INVALID_STATE; } - az_span_reader * const p_reader = &self->_internal.reader; - az_span_reader_skip_json_white_space(p_reader); + az_span * p_reader = &self->_internal.reader; + AZ_RETURN_IF_FAILED(az_span_reader_skip_json_white_space(p_reader)); AZ_RETURN_IF_FAILED(az_json_parser_get_value_space(self, out_token)); - bool const is_empty = az_span_reader_is_empty(p_reader); + bool const is_empty = az_span_length(*p_reader) == 0; // everything was read switch (out_token->kind) { case AZ_JSON_TOKEN_ARRAY: case AZ_JSON_TOKEN_OBJECT: @@ -296,17 +347,17 @@ AZ_NODISCARD AZ_INLINE uint8_t az_json_stack_item_to_close(az_json_stack_item it } AZ_NODISCARD static az_result az_json_parser_read_comma_or_close(az_json_parser * self) { - az_span_reader * const p_reader = &self->_internal.reader; - az_result_byte const c = az_span_reader_current(p_reader); + az_span * p_reader = &self->_internal.reader; + uint8_t const c = az_span_ptr(*p_reader)[0]; if (c == ',') { // skip ',' and read all whitespaces. - az_span_reader_next(p_reader); - az_span_reader_skip_json_white_space(p_reader); + AZ_RETURN_IF_FAILED(az_span_slice(*p_reader, 1, -1, p_reader)); + AZ_RETURN_IF_FAILED(az_span_reader_skip_json_white_space(p_reader)); return AZ_OK; } uint8_t const close = az_json_stack_item_to_close(az_json_parser_stack_last(self)); if (c != close) { - return az_error_unexpected_char(c); + return AZ_ERROR_PARSER_UNEXPECTED_CHAR; } return AZ_OK; } @@ -317,15 +368,18 @@ AZ_NODISCARD static az_result az_json_parser_check_item_begin( if (az_json_parser_stack_is_empty(self) || az_json_parser_stack_last(self) != stack_item) { return AZ_ERROR_JSON_INVALID_STATE; } - az_span_reader * const p_reader = &self->_internal.reader; - az_result_byte const c = az_span_reader_current(p_reader); + az_span * p_reader = &self->_internal.reader; + if (az_span_length(*p_reader) == 0) { + return AZ_ERROR_EOF; + } + uint8_t const c = az_span_ptr(*p_reader)[0]; if (c != az_json_stack_item_to_close(stack_item)) { return AZ_OK; } // c == close AZ_RETURN_IF_FAILED(az_json_parser_pop_stack(self)); - az_span_reader_next(p_reader); - az_span_reader_skip_json_white_space(p_reader); + AZ_RETURN_IF_FAILED(az_span_slice(*p_reader, 1, -1, p_reader)); + AZ_RETURN_IF_FAILED(az_span_reader_skip_json_white_space(p_reader)); if (!az_json_parser_stack_is_empty(self)) { AZ_RETURN_IF_FAILED(az_json_parser_read_comma_or_close(self)); } @@ -350,13 +404,13 @@ az_json_parser_parse_token_member(az_json_parser * self, az_json_token_member * AZ_CONTRACT_ARG_NOT_NULL(self); AZ_CONTRACT_ARG_NOT_NULL(out_token_member); - az_span_reader * const p_reader = &self->_internal.reader; + az_span * p_reader = &self->_internal.reader; AZ_RETURN_IF_FAILED(az_json_parser_check_item_begin(self, AZ_JSON_STACK_OBJECT)); - AZ_RETURN_IF_FAILED(az_span_reader_expect_char(p_reader, '"')); + AZ_RETURN_IF_FAILED(_az_is_expected_span(p_reader, AZ_SPAN_FROM_STR("\""))); AZ_RETURN_IF_FAILED(az_span_reader_get_json_string_rest(p_reader, &out_token_member->name)); - az_span_reader_skip_json_white_space(p_reader); - AZ_RETURN_IF_FAILED(az_span_reader_expect_char(p_reader, ':')); - az_span_reader_skip_json_white_space(p_reader); + AZ_RETURN_IF_FAILED(az_span_reader_skip_json_white_space(p_reader)); + AZ_RETURN_IF_FAILED(_az_is_expected_span(p_reader, AZ_SPAN_FROM_STR(":"))); + AZ_RETURN_IF_FAILED(az_span_reader_skip_json_white_space(p_reader)); AZ_RETURN_IF_FAILED(az_json_parser_get_value_space(self, &out_token_member->token)); return az_json_parser_check_item_end(self, out_token_member->token); } @@ -374,7 +428,7 @@ az_json_parser_parse_array_item(az_json_parser * self, az_json_token * out_token AZ_NODISCARD az_result az_json_parser_done(az_json_parser * self) { AZ_CONTRACT_ARG_NOT_NULL(self); - if (!az_span_reader_is_empty(&self->_internal.reader) || !az_json_parser_stack_is_empty(self)) { + if (az_span_length(self->_internal.reader) > 0 || !az_json_parser_stack_is_empty(self)) { return AZ_ERROR_JSON_INVALID_STATE; } return AZ_OK; @@ -388,9 +442,7 @@ AZ_NODISCARD az_result az_json_parser_skip_children(az_json_parser * self, az_js case AZ_JSON_TOKEN_ARRAY: { break; } - default: { - return AZ_OK; - } + default: { return AZ_OK; } } az_json_stack target_stack = self->_internal.stack; diff --git a/sdk/core/core/src/az_json_pointer.c b/sdk/core/core/src/az_json_pointer.c index 9be7b39f0c..fd5e551a7a 100644 --- a/sdk/core/core/src/az_json_pointer.c +++ b/sdk/core/core/src/az_json_pointer.c @@ -1,28 +1,38 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#include "az_span_reader_private.h" +#include #include #include <_az_cfg.h> AZ_NODISCARD static az_result az_span_reader_read_json_pointer_char( - az_span_reader * self, + az_span * self, uint32_t * out) { AZ_CONTRACT_ARG_NOT_NULL(self); + int32_t reader_current_length = az_span_length(*self); - az_result_byte const result = az_span_reader_current(self); + // check for EOF + if (reader_current_length == 0) { + return AZ_ERROR_ITEM_NOT_FOUND; + } + + uint8_t const result = az_span_ptr(*self)[0]; switch (result) { - case AZ_ERROR_EOF: { - return AZ_ERROR_ITEM_NOT_FOUND; - } case '/': { return AZ_ERROR_JSON_POINTER_TOKEN_END; } case '~': { - az_span_reader_next(self); - az_result_byte const e = az_span_reader_current(self); - az_span_reader_next(self); + // move reader to next position + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); + // check for EOF + if (az_span_length(*self) == 0) { + return AZ_ERROR_EOF; + } + // get char + uint8_t const e = az_span_ptr(*self)[0]; + // move to next position again + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); switch (e) { case '0': { *out = '~'; @@ -32,52 +42,63 @@ AZ_NODISCARD static az_result az_span_reader_read_json_pointer_char( *out = '/'; return AZ_OK; } - default: { - return az_error_unexpected_char(e); - } + default: { return AZ_ERROR_PARSER_UNEXPECTED_CHAR; } } } default: { - az_span_reader_next(self); + // move reader to next position + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); + *out = (uint8_t)result; return AZ_OK; } } } -AZ_NODISCARD az_result -az_span_reader_read_json_pointer_token(az_span_reader * self, az_span * out) { +AZ_NODISCARD az_result az_span_reader_read_json_pointer_token(az_span * self, az_span * out) { AZ_CONTRACT_ARG_NOT_NULL(self); AZ_CONTRACT_ARG_NOT_NULL(out); // read `/` if any. { - az_result const result = az_span_reader_expect_char(self, '/'); - if (result == AZ_ERROR_EOF) { + // check there is something still to read + if (az_span_length(*self) == 0) { return AZ_ERROR_ITEM_NOT_FOUND; } - AZ_RETURN_IF_FAILED(result); + // ensure first char of pointer is `/` + if (az_span_ptr(*self)[0] != '/') { + return AZ_ERROR_PARSER_UNEXPECTED_CHAR; + } + } + // move forward + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); + if (az_span_length(*self) == 0) { + *out = *self; + return AZ_OK; } - int32_t const begin = self->i; + // What's happening below: Keep reading/scaning until POINTER_TOKEN_END is found or we get to the + // end of a Json token. var begin will record the number of bytes read until token_end or + // pointer_end. TODO: We might be able to implement _az_span_scan_until() here, since we ignore + // the out of az_span_reader_read_json_pointer_char() + int32_t initial_capacity = az_span_capacity(*self); + uint8_t * p_reader = az_span_ptr(*self); while (true) { uint32_t ignore = { 0 }; az_result const result = az_span_reader_read_json_pointer_char(self, &ignore); switch (result) { case AZ_ERROR_ITEM_NOT_FOUND: case AZ_ERROR_JSON_POINTER_TOKEN_END: { - AZ_RETURN_IF_FAILED(az_span_slice(self->span, begin, self->i, out)); + int32_t current_capacity = initial_capacity - az_span_capacity(*self); + *out = az_span_init(p_reader, current_capacity, current_capacity); return AZ_OK; } - default: { - AZ_RETURN_IF_FAILED(result); - } + default: { AZ_RETURN_IF_FAILED(result); } } } } -AZ_NODISCARD az_result -az_span_reader_read_json_pointer_token_char(az_span_reader * self, uint32_t * out) { +AZ_NODISCARD az_result az_span_reader_read_json_pointer_token_char(az_span * self, uint32_t * out) { AZ_CONTRACT_ARG_NOT_NULL(self); AZ_CONTRACT_ARG_NOT_NULL(out); diff --git a/sdk/core/core/src/az_json_string.c b/sdk/core/core/src/az_json_string.c index 31f1c61393..ba8e54da06 100644 --- a/sdk/core/core/src/az_json_string.c +++ b/sdk/core/core/src/az_json_string.c @@ -1,57 +1,63 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT +#include #include -#include "az_span_reader_private.h" - #include "az_hex_private.h" #include #include <_az_cfg.h> -AZ_NODISCARD AZ_INLINE az_result_byte az_hex_to_digit(az_result_byte c) { +AZ_NODISCARD AZ_INLINE az_result az_hex_to_digit(uint8_t c, uint8_t * out) { if (isdigit(c)) { - return c - '0'; - } - if ('a' <= c && c <= 'f') { - return c - _az_HEX_LOWER_OFFSET; - } - if ('A' <= c && c <= 'F') { - return c - _az_HEX_UPPER_OFFSET; + *out = c - '0'; + } else if ('a' <= c && c <= 'f') { + *out = c - _az_HEX_LOWER_OFFSET; + } else if ('A' <= c && c <= 'F') { + *out = c - _az_HEX_UPPER_OFFSET; + } else { + return AZ_ERROR_PARSER_UNEXPECTED_CHAR; } - return az_error_unexpected_char(c); + return AZ_OK; } -AZ_NODISCARD AZ_INLINE az_result_byte az_json_esc_decode(az_result_byte c) { +AZ_NODISCARD AZ_INLINE az_result az_json_esc_decode(uint8_t c, uint8_t * out) { switch (c) { case '\\': case '"': case '/': { - return c; + *out = c; + break; } case 'b': { - return '\b'; + *out = '\b'; + break; } case 'f': { - return '\f'; + *out = '\f'; + break; } case 'n': { - return '\n'; + *out = '\n'; + break; } case 'r': { - return '\r'; + *out = '\r'; + break; } case 't': { - return '\t'; + *out = '\t'; + break; } default: - return az_error_unexpected_char(c); + return AZ_ERROR_PARSER_UNEXPECTED_CHAR; } + return AZ_OK; } -AZ_NODISCARD az_span az_json_esc_encode(az_result_byte c) { +AZ_NODISCARD az_span az_json_esc_encode(uint8_t c) { switch (c) { case '\\': { return AZ_SPAN_FROM_STR("\\\\"); @@ -74,47 +80,56 @@ AZ_NODISCARD az_span az_json_esc_encode(az_result_byte c) { case '\t': { return AZ_SPAN_FROM_STR("\\t"); } - default: { - return az_span_null(); - } + default: { return az_span_null(); } } } -AZ_NODISCARD az_result az_span_reader_read_json_string_char(az_span_reader * self, uint32_t * out) { +AZ_NODISCARD az_result az_span_reader_read_json_string_char(az_span * self, uint32_t * out) { AZ_CONTRACT_ARG_NOT_NULL(self); - az_result_byte const result = az_span_reader_current(self); + int32_t reader_length = az_span_length(*self); + if (reader_length == 0) { + return AZ_ERROR_ITEM_NOT_FOUND; + } + + uint8_t const result = az_span_ptr(*self)[0]; switch (result) { - case AZ_ERROR_EOF: { - return AZ_ERROR_ITEM_NOT_FOUND; - } case '"': { return AZ_ERROR_JSON_STRING_END; } case '\\': { - az_span_reader_next(self); - az_result const c = az_span_reader_current(self); - az_span_reader_next(self); + // moving reader fw + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); + if (az_span_length(*self) == 0) { + return AZ_ERROR_EOF; + } + uint8_t const c = az_span_ptr(*self)[0]; + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); + if (c == 'u') { uint32_t r = 0; - for (size_t i = 0; i < 4; ++i, az_span_reader_next(self)) { - az_result_byte const digit = az_hex_to_digit(az_span_reader_current(self)); - AZ_RETURN_IF_FAILED(digit); + for (size_t i = 0; i < 4; ++i) { + uint8_t digit = 0; + if (az_span_length(*self) == 0) { + return AZ_ERROR_EOF; + } + AZ_RETURN_IF_FAILED(az_hex_to_digit(az_span_ptr(*self)[0], &digit)); r = (r << 4) + digit; + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); } *out = r; } else { - az_result_byte const r = az_json_esc_decode(c); - AZ_RETURN_IF_FAILED(r); + uint8_t r = 0; + AZ_RETURN_IF_FAILED(az_json_esc_decode(c, &r)); *out = r; } return AZ_OK; } default: { if (result < 0x20) { - return az_error_unexpected_char(result); + return AZ_ERROR_PARSER_UNEXPECTED_CHAR; } - az_span_reader_next(self); + AZ_RETURN_IF_FAILED(az_span_slice(*self, 1, -1, self)); *out = (uint16_t)result; return AZ_OK; } diff --git a/sdk/core/core/src/az_json_string_private.h b/sdk/core/core/src/az_json_string_private.h index 75d55d148c..7b929645d2 100644 --- a/sdk/core/core/src/az_json_string_private.h +++ b/sdk/core/core/src/az_json_string_private.h @@ -6,7 +6,6 @@ #include "az_span_private.h" #include -#include #include @@ -16,13 +15,13 @@ * Encodes the given character into a JSON escape sequence. The function returns an empty span if * the given character doesn't require to be escaped. */ -AZ_NODISCARD az_span az_json_esc_encode(az_result_byte c); +AZ_NODISCARD az_span az_json_esc_encode(uint8_t c); /** * TODO: this function and JSON pointer read functions should return proper UNICODE * code-point to be compatible. */ -AZ_NODISCARD az_result az_span_reader_read_json_string_char(az_span_reader * self, uint32_t * out); +AZ_NODISCARD az_result az_span_reader_read_json_string_char(az_span * self, uint32_t * out); /** * Returns a next reference token in the JSON pointer. The JSON pointer parser is @var @@ -30,13 +29,12 @@ AZ_NODISCARD az_result az_span_reader_read_json_string_char(az_span_reader * sel * * See https://tools.ietf.org/html/rfc6901 */ -AZ_NODISCARD az_result az_span_reader_read_json_pointer_token(az_span_reader * self, az_span * out); +AZ_NODISCARD az_result az_span_reader_read_json_pointer_token(az_span * self, az_span * out); /** * Returns a next character in the given span reader of JSON pointer reference token. */ -AZ_NODISCARD az_result -az_span_reader_read_json_pointer_token_char(az_span_reader * self, uint32_t * out); +AZ_NODISCARD az_result az_span_reader_read_json_pointer_token_char(az_span * self, uint32_t * out); #include <_az_cfg_suffix.h> diff --git a/sdk/core/core/src/az_span.c b/sdk/core/core/src/az_span.c index 8be0603f02..5eba06ca31 100644 --- a/sdk/core/core/src/az_span.c +++ b/sdk/core/core/src/az_span.c @@ -31,7 +31,7 @@ az_span_slice(az_span span, int32_t low_index, int32_t high_index, az_span * out /** * ASCII lower case. */ -AZ_NODISCARD AZ_INLINE az_result_byte az_ascii_lower(az_result_byte value) { +AZ_NODISCARD AZ_INLINE uint8_t az_ascii_lower(uint8_t value) { return 'A' <= value && value <= 'Z' ? value + AZ_ASCII_LOWER_DIF : value; } @@ -41,7 +41,7 @@ AZ_NODISCARD bool az_span_is_content_equal_ignoring_case(az_span a, az_span b) { return false; } for (int32_t i = 0; i < size; ++i) { - if (az_ascii_lower(az_span_get(a, i)) != az_ascii_lower(az_span_get(b, i))) { + if (az_ascii_lower(az_span_ptr(a)[i]) != az_ascii_lower(az_span_ptr(b)[i])) { return false; } } @@ -49,27 +49,26 @@ AZ_NODISCARD bool az_span_is_content_equal_ignoring_case(az_span a, az_span b) { } AZ_NODISCARD az_result az_span_to_uint64(az_span self, uint64_t * out) { - if (az_span_length(self) <= 0) { + int32_t self_length = az_span_length(self); + if (self_length <= 0) { return AZ_ERROR_EOF; } + uint64_t value = 0; - int32_t i = 0; - while (true) { - az_result_byte const result = az_span_get(self, i); - if (result == AZ_ERROR_EOF) { - *out = value; - return AZ_OK; - } + for (int32_t i = 0; i < self_length; i++) { + uint8_t result = az_span_ptr(self)[i]; if (!isdigit(result)) { - return az_error_unexpected_char(result); + return AZ_ERROR_PARSER_UNEXPECTED_CHAR; } uint64_t const d = (uint64_t)result - '0'; if ((UINT64_MAX - d) / 10 < value) { return AZ_ERROR_PARSER_UNEXPECTED_CHAR; } value = value * 10 + d; - i += 1; } + + *out = value; + return AZ_OK; } /****************** Mutating az_span (used to be az_span in the origins) ******/ @@ -392,6 +391,27 @@ AZ_NODISCARD az_result az_span_append_int64(az_span * self, int64_t n) { return _az_span_builder_append_uint64(self, n); } +AZ_NODISCARD az_result _az_is_expected_span(az_span * self, az_span expected) { + az_span actual_span = { 0 }; + + int32_t expected_length = az_span_length(expected); + + // EOF because self is smaller than the expected span + if (expected_length > az_span_length(*self)) { + return AZ_ERROR_EOF; + } + + AZ_RETURN_IF_FAILED(az_span_slice(*self, 0, expected_length, &actual_span)); + + if (!az_span_is_equal(actual_span, expected)) { + return AZ_ERROR_PARSER_UNEXPECTED_CHAR; + } + // move reader after the expected span (means it was parsed as expected) + AZ_RETURN_IF_FAILED(az_span_slice(*self, expected_length, -1, self)); + + return AZ_OK; +} + // PRIVATE. read until condition is true on character. // Then return number of positions read with output parameter AZ_NODISCARD az_result _az_scan_until(az_span self, _az_predicate predicate, int32_t * out_index) { @@ -407,9 +427,7 @@ AZ_NODISCARD az_result _az_scan_until(az_span self, _az_predicate predicate, int case AZ_CONTINUE: { break; } - default: { - return predicate_result; - } + default: { return predicate_result; } } } return AZ_ERROR_ITEM_NOT_FOUND; diff --git a/sdk/core/core/src/az_span_private.h b/sdk/core/core/src/az_span_private.h index e454b4e996..0a5ee40f7f 100644 --- a/sdk/core/core/src/az_span_private.h +++ b/sdk/core/core/src/az_span_private.h @@ -12,8 +12,6 @@ #include <_az_cfg_prefix.h> -typedef int32_t az_result_byte; - /** * @brief returns a span with the left @var n bytes of the given @var span. * @@ -49,23 +47,7 @@ AZ_NODISCARD AZ_INLINE az_span az_span_drop(az_span span, int32_t n) { return az_span_init(az_span_ptr(span) + n, new_length, current_capacity - n); } -/** - * Returns a byte in `index` position. - * Returns `AZ_ERROR_EOF` if the `index` is out of the span range. - */ -AZ_NODISCARD AZ_INLINE az_result_byte az_span_get(az_span span, int32_t index) { - if (az_span_length(span) <= index) { - return AZ_ERROR_EOF; - } - return az_span_ptr(span)[index]; -} - -// Parsing utilities -AZ_NODISCARD AZ_INLINE az_result az_error_unexpected_char(az_result_byte c) { - return az_failed(c) ? c : AZ_ERROR_PARSER_UNEXPECTED_CHAR; -} - -AZ_NODISCARD AZ_INLINE bool az_span_is_overlap(az_span a, az_span b) { +AZ_NODISCARD AZ_INLINE bool az_span_is_overlap(az_span const a, az_span const b) { uint8_t * a_ptr = az_span_ptr(a); uint8_t * b_ptr = az_span_ptr(b); int32_t a_length = az_span_length(a); @@ -126,6 +108,8 @@ typedef az_result (*_az_predicate)(az_span slice); // Then return number of positions read with output parameter AZ_NODISCARD az_result _az_scan_until(az_span self, _az_predicate predicate, int32_t * out_index); +AZ_NODISCARD az_result _az_is_expected_span(az_span * self, az_span expected); + #include <_az_cfg_suffix.h> #endif diff --git a/sdk/core/core/src/az_span_reader.c b/sdk/core/core/src/az_span_reader.c deleted file mode 100644 index 9ab2c10d36..0000000000 --- a/sdk/core/core/src/az_span_reader.c +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// SPDX-License-Identifier: MIT - -#include "az_span_reader_private.h" -#include -#include - -#include - -#include <_az_cfg.h> - -AZ_NODISCARD az_result az_span_reader_expect_char(az_span_reader * p_reader, uint8_t expected) { - az_result_byte const c = az_span_reader_current(p_reader); - if (c != expected) { - return az_error_unexpected_char(c); - } - az_span_reader_next(p_reader); - return AZ_OK; -} - -AZ_NODISCARD az_result az_span_reader_expect_span(az_span_reader * self, az_span span) { - az_span_reader k = az_span_reader_create(span); - while (true) { - az_result_byte const ko = az_span_reader_current(&k); - if (ko == AZ_ERROR_EOF) { - return AZ_OK; - } - az_span_reader_next(&k); - AZ_RETURN_IF_FAILED(az_span_reader_expect_char(self, (uint8_t)ko)); - } -} - -AZ_NODISCARD az_result az_span_reader_expect_digit(az_span_reader * self, uint8_t * digit) { - az_result_byte const c = az_span_reader_current(self); - if (!isdigit(c)) { - return az_error_unexpected_char(c); - } - *digit = (uint8_t)(c - '0'); - az_span_reader_next(self); - return AZ_OK; -} - -AZ_NODISCARD az_result az_span_reader_set_pos(az_span_reader * p_reader, int32_t i) { - AZ_CONTRACT(i <= az_span_length(p_reader->span), AZ_ERROR_ARG); - - p_reader->i = i; - return AZ_OK; -} diff --git a/sdk/core/core/src/az_span_reader_private.h b/sdk/core/core/src/az_span_reader_private.h deleted file mode 100644 index 01b92cd154..0000000000 --- a/sdk/core/core/src/az_span_reader_private.h +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// SPDX-License-Identifier: MIT - -#ifndef _az_SPAN_READER_PRIVATE_H -#define _az_SPAN_READER_PRIVATE_H - -#include "az_span_private.h" -#include -#include - -#include - -#include <_az_cfg_prefix.h> - -AZ_NODISCARD AZ_INLINE az_result_byte az_span_reader_current(az_span_reader const * p_reader) { - return az_span_get(p_reader->span, p_reader->i); -} - -#include <_az_cfg_suffix.h> - -#endif diff --git a/sdk/core/core/test/main.c b/sdk/core/core/test/main.c index 9fd9977e59..fb214c5a94 100644 --- a/sdk/core/core/test/main.c +++ b/sdk/core/core/test/main.c @@ -6,7 +6,6 @@ #include #include -#include #include #include @@ -191,6 +190,9 @@ static az_span hrb_header_authorization_token2 = AZ_SPAN_LITERAL_FROM_STR("Bearer 99887766554433221100"); int main() { + test_json_get_by_pointer(); + test_json_pointer(); + test_json_builder(); { az_json_parser parser = { 0 }; TEST_EXPECT_SUCCESS(az_json_parser_init(&parser, AZ_SPAN_FROM_STR(" "))); @@ -352,6 +354,7 @@ int main() { } { int32_t o = 0; + output = AZ_SPAN_FROM_BUFFER(buffer); az_span const json = AZ_SPAN_FROM_STR( // 0 1 2 3 4 5 6 // 01234 56789 01234 56678 01234 56789 01234 56789 01234 56789 01234 @@ -363,6 +366,7 @@ int main() { } { int32_t o = 0; + output = AZ_SPAN_FROM_BUFFER(buffer); az_span const json = AZ_SPAN_FROM_STR( // 0 1 2 3 4 5 6 01234 // 56789 01234 56678 01234 56789 01234 56789 01234 56789 01234 56789 012 @@ -373,6 +377,7 @@ int main() { } { int32_t o = 0; + output = AZ_SPAN_FROM_BUFFER(buffer); az_span const json = AZ_SPAN_FROM_STR( // 0 1 2 3 4 5 6 01234 // 56789 01234 56678 01234 56789 01234 56789 01234 56789 01234 56789 012 @@ -382,6 +387,7 @@ int main() { "}]]]] ]]]]] ]]]]] ]]]]] ]]]]] ]]]]] ]]]]] ]]]]] ]]]]] ]]]]] ]]]]] " "]]]]] ]]]"); output._internal.length = 0; + output = AZ_SPAN_FROM_BUFFER(buffer); az_result const result = read_write(json, &output, &o); TEST_ASSERT(result == AZ_OK); @@ -396,6 +402,7 @@ int main() { // { int32_t o = 0; + output = AZ_SPAN_FROM_BUFFER(buffer); az_result const result = read_write(sample1, &output, &o); TEST_ASSERT(result == AZ_OK); } @@ -490,9 +497,7 @@ int main() { test_http_response(); test_span_builder_replace(); test_mut_span(); - test_json_builder(); - test_json_get_by_pointer(); - test_json_pointer(); + test_json_string(); test_log(); return exit_code; diff --git a/sdk/core/core/test/test_json_pointer.c b/sdk/core/core/test/test_json_pointer.c index 9b90ec389c..fe101c0ff8 100644 --- a/sdk/core/core/test/test_json_pointer.c +++ b/sdk/core/core/test/test_json_pointer.c @@ -3,7 +3,6 @@ #include "az_json_string_private.h" #include -#include #include @@ -11,24 +10,24 @@ void test_json_pointer() { { - az_span_reader parser = az_span_reader_create(AZ_SPAN_FROM_STR("")); + az_span parser = AZ_SPAN_FROM_STR(""); az_span p; TEST_ASSERT(az_span_reader_read_json_pointer_token(&parser, &p) == AZ_ERROR_ITEM_NOT_FOUND); } { - az_span_reader parser = az_span_reader_create(AZ_SPAN_FROM_STR("Hello")); + az_span parser = AZ_SPAN_FROM_STR("Hello"); az_span p; TEST_ASSERT( az_span_reader_read_json_pointer_token(&parser, &p) == AZ_ERROR_PARSER_UNEXPECTED_CHAR); } { - az_span_reader parser = az_span_reader_create(AZ_SPAN_FROM_STR("/abc")); + az_span parser = AZ_SPAN_FROM_STR("/abc"); az_span p; TEST_ASSERT(az_span_reader_read_json_pointer_token(&parser, &p) == AZ_OK); TEST_ASSERT(az_span_is_equal(p, AZ_SPAN_FROM_STR("abc"))); // test az_json_pointer_token_parser_get { - az_span_reader token_parser = az_span_reader_create(p); + az_span token_parser = p; uint8_t buffer[10]; int i = 0; while (true) { @@ -48,13 +47,13 @@ void test_json_pointer() { TEST_ASSERT(az_span_reader_read_json_pointer_token(&parser, &p) == AZ_ERROR_ITEM_NOT_FOUND); } { - az_span_reader parser = az_span_reader_create(AZ_SPAN_FROM_STR("/abc//dffgg21")); + az_span parser = AZ_SPAN_FROM_STR("/abc//dffgg21"); az_span p; TEST_ASSERT(az_span_reader_read_json_pointer_token(&parser, &p) == AZ_OK); TEST_ASSERT(az_span_is_equal(p, AZ_SPAN_FROM_STR("abc"))); // test az_json_pointer_token_parser_get { - az_span_reader token_parser = az_span_reader_create(p); + az_span token_parser = p; uint8_t buffer[10]; int i = 0; while (true) { @@ -78,13 +77,13 @@ void test_json_pointer() { TEST_ASSERT(az_span_reader_read_json_pointer_token(&parser, &p) == AZ_ERROR_ITEM_NOT_FOUND); } { - az_span_reader parser = az_span_reader_create(AZ_SPAN_FROM_STR("/ab~1c/dff~0x")); + az_span parser = AZ_SPAN_FROM_STR("/ab~1c/dff~0x"); az_span p; TEST_ASSERT(az_span_reader_read_json_pointer_token(&parser, &p) == AZ_OK); TEST_ASSERT(az_span_is_equal(p, AZ_SPAN_FROM_STR("ab~1c"))); // test az_json_pointer_token_parser_get { - az_span_reader token_parser = az_span_reader_create(p); + az_span token_parser = p; uint8_t buffer[10]; int i = 0; while (true) { @@ -105,7 +104,7 @@ void test_json_pointer() { TEST_ASSERT(az_span_is_equal(p, AZ_SPAN_FROM_STR("dff~0x"))); // test az_json_pointer_token_parser_get { - az_span_reader token_parser = az_span_reader_create(p); + az_span token_parser = p; uint8_t buffer[10]; int i = 0; while (true) { @@ -125,7 +124,7 @@ void test_json_pointer() { TEST_ASSERT(az_span_reader_read_json_pointer_token(&parser, &p) == AZ_ERROR_ITEM_NOT_FOUND); } { - az_span_reader parser = az_span_reader_create(AZ_SPAN_FROM_STR("/ab~1c/dff~x")); + az_span parser = AZ_SPAN_FROM_STR("/ab~1c/dff~x"); az_span p; TEST_ASSERT(az_span_reader_read_json_pointer_token(&parser, &p) == AZ_OK); TEST_ASSERT(az_span_is_equal(p, AZ_SPAN_FROM_STR("ab~1c"))); @@ -133,7 +132,7 @@ void test_json_pointer() { az_span_reader_read_json_pointer_token(&parser, &p) == AZ_ERROR_PARSER_UNEXPECTED_CHAR); } { - az_span_reader parser = az_span_reader_create(AZ_SPAN_FROM_STR("/ab~1c/dff~")); + az_span parser = AZ_SPAN_FROM_STR("/ab~1c/dff~"); az_span p; TEST_ASSERT(az_span_reader_read_json_pointer_token(&parser, &p) == AZ_OK); TEST_ASSERT(az_span_is_equal(p, AZ_SPAN_FROM_STR("ab~1c"))); @@ -141,20 +140,20 @@ void test_json_pointer() { } // test az_json_pointer_token_parser_get { - az_span_reader token_parser = az_span_reader_create(AZ_SPAN_FROM_STR("~")); + az_span token_parser = AZ_SPAN_FROM_STR("~"); uint32_t c; TEST_ASSERT(az_span_reader_read_json_pointer_token_char(&token_parser, &c) == AZ_ERROR_EOF); } // test az_json_pointer_token_parser_get { - az_span_reader token_parser = az_span_reader_create(AZ_SPAN_FROM_STR("")); + az_span token_parser = AZ_SPAN_FROM_STR(""); uint32_t c; TEST_ASSERT( az_span_reader_read_json_pointer_token_char(&token_parser, &c) == AZ_ERROR_ITEM_NOT_FOUND); } // test az_json_pointer_token_parser_get { - az_span_reader token_parser = az_span_reader_create(AZ_SPAN_FROM_STR("/")); + az_span token_parser = AZ_SPAN_FROM_STR("/"); uint32_t c; TEST_ASSERT( az_span_reader_read_json_pointer_token_char(&token_parser, &c) @@ -162,7 +161,7 @@ void test_json_pointer() { } // test az_json_pointer_token_parser_get { - az_span_reader token_parser = az_span_reader_create(AZ_SPAN_FROM_STR("~2")); + az_span token_parser = AZ_SPAN_FROM_STR("~2"); uint32_t c; TEST_ASSERT( az_span_reader_read_json_pointer_token_char(&token_parser, &c) diff --git a/sdk/core/core/test/test_json_string.c b/sdk/core/core/test/test_json_string.c index b69390c14e..577347f7f5 100644 --- a/sdk/core/core/test/test_json_string.c +++ b/sdk/core/core/test/test_json_string.c @@ -10,7 +10,7 @@ void test_json_string() { { az_span const s = AZ_SPAN_FROM_STR("tr\\\"ue\\t"); - az_span_reader reader = az_span_reader_create(s); + az_span reader = s; uint32_t c; TEST_ASSERT(az_span_reader_read_json_string_char(&reader, &c) == AZ_OK); TEST_ASSERT(c == 't'); @@ -28,7 +28,7 @@ void test_json_string() { } { az_span const s = AZ_SPAN_FROM_STR("\\uFf0F"); - az_span_reader reader = az_span_reader_create(s); + az_span reader = s; uint32_t c = { 0 }; TEST_ASSERT(az_span_reader_read_json_string_char(&reader, &c) == AZ_OK); TEST_ASSERT(c == 0xFF0F); @@ -36,7 +36,7 @@ void test_json_string() { } { az_span const s = AZ_SPAN_FROM_STR("\\uFf0"); - az_span_reader reader = az_span_reader_create(s); + az_span reader = s; uint32_t c; TEST_ASSERT(az_span_reader_read_json_string_char(&reader, &c) == AZ_ERROR_EOF); }