From a4b3ebb8061097c080c542db6be3802dad4850b3 Mon Sep 17 00:00:00 2001 From: Xie Gengxin Date: Fri, 25 Nov 2022 12:20:17 +0800 Subject: [PATCH] style: Add workflow to check style and format include/sonic --- .clang-format | 1 + .github/workflows/clang-format-check.yml | 14 ++ include/sonic/allocator.h | 16 +- include/sonic/dom/dynamicnode.h | 9 +- include/sonic/dom/generic_document.h | 27 +-- include/sonic/dom/genericnode.h | 22 +-- include/sonic/dom/handler.h | 36 ++-- include/sonic/dom/parser.h | 238 +++++++++++------------ include/sonic/dom/type.h | 32 +-- include/sonic/experiment/lazy_update.h | 7 +- include/sonic/internal/atof_native.h | 4 +- include/sonic/internal/haswell.h | 3 +- include/sonic/internal/quote.h | 4 +- include/sonic/internal/skip.h | 232 +++++++++++----------- include/sonic/macro.h | 2 +- include/sonic/writebuffer.h | 20 +- 16 files changed, 334 insertions(+), 333 deletions(-) create mode 100644 .clang-format create mode 100644 .github/workflows/clang-format-check.yml diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..f6cb8ad --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +BasedOnStyle: Google diff --git a/.github/workflows/clang-format-check.yml b/.github/workflows/clang-format-check.yml new file mode 100644 index 0000000..33c391e --- /dev/null +++ b/.github/workflows/clang-format-check.yml @@ -0,0 +1,14 @@ +name: clang-format Check +on: [push, pull_request] +jobs: + formatting-check: + name: Formatting Check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Run clang-format style check. + uses: jidicula/clang-format-action@v4.9.0 + with: + clang-format-version: '11' + check-path: 'include/sonic' + fallback-style: 'Google' diff --git a/include/sonic/allocator.h b/include/sonic/allocator.h index 1857461..eb8230d 100644 --- a/include/sonic/allocator.h +++ b/include/sonic/allocator.h @@ -59,9 +59,7 @@ class SimpleAllocator { return new_ptr; } - static void Free(void* ptr) { - std::free(ptr); - } + static void Free(void* ptr) { std::free(ptr); } bool operator==(const SimpleAllocator&) const { return true; } bool operator!=(const SimpleAllocator&) const { return false; } @@ -80,11 +78,11 @@ class SimpleAllocator { #endif // SpinLock is copied from https://rigtorp.se/spinlock/ -class SpinLock -{ -private: +class SpinLock { + private: std::atomic lock_ = {false}; -public: + + public: void lock() { for (;;) { if (!lock_.exchange(true, std::memory_order_acquire)) { @@ -95,9 +93,7 @@ class SpinLock } } } - void unlock() { - lock_.store(false, std::memory_order_release); - } + void unlock() { lock_.store(false, std::memory_order_release); } }; #ifdef SONIC_LOCKED_ALLOCATOR diff --git a/include/sonic/dom/dynamicnode.h b/include/sonic/dom/dynamicnode.h index 98e8b21..abd2552 100644 --- a/include/sonic/dom/dynamicnode.h +++ b/include/sonic/dom/dynamicnode.h @@ -387,7 +387,7 @@ class DNode : public GenericNode { return *this; } - DNode &setRawImpl(const char *s, size_t len) { + DNode& setRawImpl(const char* s, size_t len) { this->destroy(); this->raw.p = s; this->setLength(len, kRaw); @@ -582,7 +582,7 @@ class DNode : public GenericNode { return const_cast(it); } - sonic_force_inline DNode &findValueImpl(StringView key) const noexcept { + sonic_force_inline DNode& findValueImpl(StringView key) const noexcept { auto m = findMemberImpl(key); if (m != this->MemberEnd()) { return m->value; @@ -648,8 +648,7 @@ class DNode : public GenericNode { } else { m = memberBeginUnsafe(); for (; m != memberEndUnsafe(); ++m) // { - if (m->name.GetStringView() == key) - goto find; + if (m->name.GetStringView() == key) goto find; goto not_find; } @@ -667,7 +666,7 @@ class DNode : public GenericNode { size_t pos = m - memberBeginUnsafe(); // erase tail auto range = - map->equal_range(m->name.GetStringView()); // already moved. + map->equal_range(m->name.GetStringView()); // already moved. for (auto i = range.first; i != range.second; ++i) { if (i->second == this->Size() - 1) { map->erase(i); diff --git a/include/sonic/dom/generic_document.h b/include/sonic/dom/generic_document.h index 5a7518b..eab9e7d 100644 --- a/include/sonic/dom/generic_document.h +++ b/include/sonic/dom/generic_document.h @@ -44,11 +44,14 @@ class GenericDocument : public NodeType { /** * @brief Move constructor */ - GenericDocument(GenericDocument &&rhs) + GenericDocument(GenericDocument&& rhs) : NodeType(std::forward(rhs)), - own_alloc_(rhs.own_alloc_.release()), alloc_(rhs.alloc_), - parse_result_(rhs.parse_result_), str_(rhs.str_), - str_cap_(rhs.str_cap_), strp_(rhs.strp_) { + own_alloc_(rhs.own_alloc_.release()), + alloc_(rhs.alloc_), + parse_result_(rhs.parse_result_), + str_(rhs.str_), + str_cap_(rhs.str_cap_), + strp_(rhs.strp_) { rhs.clear(); } @@ -111,12 +114,12 @@ class GenericDocument : public NodeType { * before parsing to avoid memory overuse. */ template - GenericDocument &Parse(StringView json) { + GenericDocument& Parse(StringView json) { return Parse(json.data(), json.size()); } template - GenericDocument &Parse(const char *data, size_t len) { + GenericDocument& Parse(const char* data, size_t len) { destroyDom(); return parseImpl(data, len); } @@ -132,15 +135,15 @@ class GenericDocument : public NodeType { */ template - GenericDocument &ParseOnDemand(StringView json, - const GenericJsonPointer &path) { + GenericDocument& ParseOnDemand(StringView json, + const GenericJsonPointer& path) { return ParseOnDemand(json.data(), json.size(), path); } template - GenericDocument &ParseOnDemand(const char *data, size_t len, - const GenericJsonPointer &path) { + GenericDocument& ParseOnDemand(const char* data, size_t len, + const GenericJsonPointer& path) { destroyDom(); return parseOnDemandImpl(data, len, path); } @@ -218,9 +221,9 @@ class GenericDocument : public NodeType { return parseImpl(target.data(), target.size()); } - SonicError allocateStringBuffer(const char *json, size_t len) { + SonicError allocateStringBuffer(const char* json, size_t len) { size_t pad_len = len + 64; - str_ = (char *)(alloc_->Malloc(pad_len)); + str_ = (char*)(alloc_->Malloc(pad_len)); if (str_ == nullptr) { return kErrorNoMem; } diff --git a/include/sonic/dom/genericnode.h b/include/sonic/dom/genericnode.h index 7efe0e9..506b589 100644 --- a/include/sonic/dom/genericnode.h +++ b/include/sonic/dom/genericnode.h @@ -189,7 +189,7 @@ class GenericNode { * @param s string_view that contina string pointer and length. * @param alloc Allocator */ - GenericNode(StringView s, alloc_type &alloc) { + GenericNode(StringView s, alloc_type& alloc) { StringCopy(s.data(), s.size(), alloc); } @@ -466,7 +466,7 @@ class GenericNode { * @return NodeType& Reference to this. * @note this node will deconstruct firstly. */ - NodeType &SetString(StringView s, alloc_type &alloc) { + NodeType& SetString(StringView s, alloc_type& alloc) { return SetString(s.data(), s.size(), alloc); } /** @@ -488,7 +488,7 @@ class GenericNode { * @return NodeType& Reference to this. * @note this node will deconstruct firstly. */ - NodeType &SetString(StringView s) { return SetString(s.data(), s.size()); } + NodeType& SetString(StringView s) { return SetString(s.data(), s.size()); } /** * @brief Set this node as string type. Only copy string pointer. * @param s char array pointer @@ -663,7 +663,7 @@ class GenericNode { * @retval null-node Expected node doesn't exist. * @retval others Reference to the expected node. */ - sonic_force_inline NodeType &operator[](StringView key) noexcept { + sonic_force_inline NodeType& operator[](StringView key) noexcept { sonic_assert(this->IsObject()); return downCast()->findValueImpl(key); } @@ -674,7 +674,7 @@ class GenericNode { * @retval null-node Expected node doesn't exist. * @retval others Reference to the expected node. */ - sonic_force_inline const NodeType &operator[](StringView key) const noexcept { + sonic_force_inline const NodeType& operator[](StringView key) const noexcept { sonic_assert(this->IsObject()); return downCast()->findValueImpl(key); } @@ -731,8 +731,8 @@ class GenericNode { * @retval others success */ template - sonic_force_inline const NodeType * - AtPointer(const GenericJsonPointer &pointer) const { + sonic_force_inline const NodeType* AtPointer( + const GenericJsonPointer& pointer) const { return atPointerImpl(pointer); } @@ -1003,7 +1003,7 @@ class GenericNode { setEmptyString(); } } - NodeType &setRaw(StringView s) { + NodeType& setRaw(StringView s) { return downCast()->setRawImpl(s.data(), s.size()); } void setEmptyString() noexcept { @@ -1024,7 +1024,7 @@ class GenericNode { return std::numeric_limits::max(); } -private: + private: friend NodeType; friend class SAXHandler; friend class LazySAXHandler; @@ -1052,8 +1052,8 @@ class GenericNode { struct Raw { uint64_t len; - const char *p; - }; // 16 bytes + const char* p; + }; // 16 bytes struct Number { uint64_t t; union { diff --git a/include/sonic/dom/handler.h b/include/sonic/dom/handler.h index eeb3bc5..82923ca 100644 --- a/include/sonic/dom/handler.h +++ b/include/sonic/dom/handler.h @@ -24,10 +24,12 @@ namespace sonic_json { -template class GenericDocument; +template +class GenericDocument; -template class SAXHandler { -public: +template +class SAXHandler { + public: using Allocator = typename NodeType::AllocatorType; SAXHandler() = default; @@ -36,7 +38,10 @@ template class SAXHandler { SAXHandler(const SAXHandler &) = delete; SAXHandler &operator=(const SAXHandler &rhs) = delete; SAXHandler(SAXHandler &&rhs) - : st_(rhs.st_), np_(rhs.np_), cap_(rhs.cap_), parent_(rhs.parent_), + : st_(rhs.st_), + np_(rhs.np_), + cap_(rhs.cap_), + parent_(rhs.parent_), alloc_(rhs.alloc_) { rhs.st_ = nullptr; rhs.cap_ = 0; @@ -65,20 +70,17 @@ template class SAXHandler { sonic_force_inline bool SetUp(StringView json) { size_t len = json.size(); size_t cap = len / 2 + 2; - if (cap < 16) - cap = 16; + if (cap < 16) cap = 16; if (!st_ || cap_ < cap) { st_ = static_cast(std::realloc(st_, sizeof(NodeType) * cap)); - if (!st_) - return false; + if (!st_) return false; cap_ = cap; } return true; }; sonic_force_inline void TearDown() { - if (st_ == nullptr) - return; + if (st_ == nullptr) return; for (size_t i = 0; i < np_; i++) { st_[i].~NodeType(); } @@ -86,10 +88,9 @@ template class SAXHandler { st_ = nullptr; }; -#define SONIC_ADD_NODE() \ - { \ - if (!node()) \ - return false; \ +#define SONIC_ADD_NODE() \ + { \ + if (!node()) return false; \ } sonic_force_inline bool Null() noexcept { @@ -181,7 +182,7 @@ template class SAXHandler { return true; } -private: + private: friend class GenericDocument; sonic_force_inline bool stringImpl(StringView s) { @@ -209,8 +210,9 @@ template class SAXHandler { Allocator *alloc_{nullptr}; }; -template class LazySAXHandler { -public: +template +class LazySAXHandler { + public: using Allocator = typename NodeType::AllocatorType; LazySAXHandler() = delete; diff --git a/include/sonic/dom/parser.h b/include/sonic/dom/parser.h index f713d8f..ef8f3f7 100644 --- a/include/sonic/dom/parser.h +++ b/include/sonic/dom/parser.h @@ -38,10 +38,10 @@ class Parser { public: explicit Parser() noexcept = default; // sonic_force_inline Parser(JsonInput& input) : input_(input) {} - Parser(Parser&& other) noexcept = default; - sonic_force_inline Parser(const Parser& other) = delete; - sonic_force_inline Parser& operator=(const Parser& other) = delete; - sonic_force_inline Parser& operator=(Parser&& other) noexcept = default; + Parser(Parser &&other) noexcept = default; + sonic_force_inline Parser(const Parser &other) = delete; + sonic_force_inline Parser &operator=(const Parser &other) = delete; + sonic_force_inline Parser &operator=(Parser &&other) noexcept = default; ~Parser() noexcept = default; template @@ -65,18 +65,18 @@ class Parser { } private: - sonic_force_inline bool hasTrailingChars() { - while (pos_ < len_) { - if (!internal::IsSpace(json_buf_[pos_])) - return true; - pos_++; - } - return false; - } + sonic_force_inline bool hasTrailingChars() { + while (pos_ < len_) { + if (!internal::IsSpace(json_buf_[pos_])) return true; + pos_++; + } + return false; + } sonic_force_inline void setParseError(SonicError err) { err_ = err; } - template sonic_force_inline void parseNull(SAX &sax) { + template + sonic_force_inline void parseNull(SAX &sax) { const static uint32_t kNullBin = 0x6c6c756e; if (internal::EqBytes4(json_buf_ + pos_ - 1, kNullBin) && sax.Null()) { pos_ += 3; @@ -85,7 +85,8 @@ class Parser { setParseError(kParseErrorInvalidChar); } - template sonic_force_inline void parseFalse(SAX &sax) { + template + sonic_force_inline void parseFalse(SAX &sax) { const static uint32_t kFalseBin = 0x65736c61; // the binary of 'alse' in false if (internal::EqBytes4(json_buf_ + pos_, kFalseBin) && sax.Bool(false)) { @@ -95,7 +96,8 @@ class Parser { setParseError(kParseErrorInvalidChar); } - template sonic_force_inline void parseTrue(SAX &sax) { + template + sonic_force_inline void parseTrue(SAX &sax) { constexpr static uint32_t kTrueBin = 0x65757274; if (internal::EqBytes4(json_buf_ + pos_ - 1, kTrueBin) && sax.Bool(true)) { pos_ += 3; @@ -104,8 +106,9 @@ class Parser { setParseError(kParseErrorInvalidChar); } - template sonic_force_inline void parseStrInPlace(SAX &sax) { - uint8_t* src = json_buf_ + pos_; + template + sonic_force_inline void parseStrInPlace(SAX &sax) { + uint8_t *src = json_buf_ + pos_; uint8_t *sdst = src; size_t n = internal::parseStringInplace(src, err_); pos_ = src - json_buf_; @@ -116,7 +119,7 @@ class Parser { return; } - sonic_force_inline bool carry_one(char c, uint64_t& sum) const { + sonic_force_inline bool carry_one(char c, uint64_t &sum) const { uint8_t d = static_cast(c - '0'); if (d > 9) { return false; @@ -125,7 +128,7 @@ class Parser { return true; } - sonic_force_inline uint64_t str2int(const char* s, size_t& i) const { + sonic_force_inline uint64_t str2int(const char *s, size_t &i) const { uint64_t sum = 0; while (carry_one(s[i], sum)) { i++; @@ -133,7 +136,7 @@ class Parser { return sum; } - sonic_force_inline bool parseFloatingFast(double& d, int exp10, + sonic_force_inline bool parseFloatingFast(double &d, int exp10, uint64_t man) const { d = (double)man; // if man is small, but exp is large, also can parse excactly @@ -156,8 +159,8 @@ class Parser { return false; } - SonicError parseFloatEiselLemire64(double& dbl, int exp10, uint64_t man, - int sgn, bool trunc, const char* s) const { + SonicError parseFloatEiselLemire64(double &dbl, int exp10, uint64_t man, + int sgn, bool trunc, const char *s) const { union { double val = 0; uint64_t uval; @@ -185,7 +188,8 @@ class Parser { } } - template sonic_force_inline void parseNumber(SAX &sax) { + template + sonic_force_inline void parseNumber(SAX &sax) { #define FLOATING_LONGEST_DIGITS 17 #define RETURN_SET_ERROR_CODE(error_code) \ { \ @@ -198,35 +202,31 @@ class Parser { RETURN_SET_ERROR_CODE(kParseErrorInvalidChar); \ } -#define SET_INT_AND_RETURN(int_val) \ - { \ - if (!sax.Int(int_val)) \ - RETURN_SET_ERROR_CODE(kParseErrorInvalidChar); \ - RETURN_SET_ERROR_CODE(kErrorNone); \ +#define SET_INT_AND_RETURN(int_val) \ + { \ + if (!sax.Int(int_val)) RETURN_SET_ERROR_CODE(kParseErrorInvalidChar); \ + RETURN_SET_ERROR_CODE(kErrorNone); \ } -#define SET_UINT_AND_RETURN(int_val) \ - { \ - if (!sax.Uint(int_val)) \ - RETURN_SET_ERROR_CODE(kParseErrorInvalidChar); \ - RETURN_SET_ERROR_CODE(kErrorNone); \ +#define SET_UINT_AND_RETURN(int_val) \ + { \ + if (!sax.Uint(int_val)) RETURN_SET_ERROR_CODE(kParseErrorInvalidChar); \ + RETURN_SET_ERROR_CODE(kErrorNone); \ } -#define SET_DOUBLE_AND_RETURN(dbl) \ - { \ - if (!sax.Double(dbl)) \ - RETURN_SET_ERROR_CODE(kParseErrorInvalidChar); \ - RETURN_SET_ERROR_CODE(kErrorNone); \ +#define SET_DOUBLE_AND_RETURN(dbl) \ + { \ + if (!sax.Double(dbl)) RETURN_SET_ERROR_CODE(kParseErrorInvalidChar); \ + RETURN_SET_ERROR_CODE(kErrorNone); \ } -#define SET_U64_AS_DOUBLE_AND_RETURN(int_val) \ - { \ - union { \ - double d; \ - uint64_t u; \ - } du; \ - du.u = int_val; \ - if (!sax.Double(du.d)) \ - RETURN_SET_ERROR_CODE(kParseErrorInvalidChar); \ - RETURN_SET_ERROR_CODE(kErrorNone); \ +#define SET_U64_AS_DOUBLE_AND_RETURN(int_val) \ + { \ + union { \ + double d; \ + uint64_t u; \ + } du; \ + du.u = int_val; \ + if (!sax.Double(du.d)) RETURN_SET_ERROR_CODE(kParseErrorInvalidChar); \ + RETURN_SET_ERROR_CODE(kErrorNone); \ } static constexpr uint64_t kUint64Max = 0xFFFFFFFFFFFFFFFF; @@ -237,7 +237,7 @@ class Parser { uint64_t man = 0; // mantissa of float point number size_t i = pos_ - 1; size_t exp10_s = i; - const char* s = reinterpret_cast(json_buf_); + const char *s = reinterpret_cast(json_buf_); using internal::is_digit; /* check sign */ @@ -349,8 +349,7 @@ class Parser { // Is error when run here // TODO: Assert - double_fract: - { + double_fract : { int fract_len = FLOATING_LONGEST_DIGITS - man_nd; if (fract_len > 0) { uint64_t sum = internal::simd_str2int_sse(s + i, fract_len); @@ -380,7 +379,7 @@ class Parser { i++; } } - } + } exp10 -= (i - exp10_s); @@ -420,7 +419,7 @@ class Parser { if ((man >> 52) == 0 && exp10 <= (22 + 15) && exp10 >= -22) { if (parseFloatingFast(dbl, exp10, man)) { SET_DOUBLE_AND_RETURN(dbl * sgn); - } // else goto double_fasax.st_normal; + } // else goto double_fasax.st_normal; } } @@ -434,8 +433,7 @@ class Parser { double d; SonicError error_code = parseFloatEiselLemire64(d, exp10, man, sgn, trunc, s); - if (!sax.Double(d)) - RETURN_SET_ERROR_CODE(kParseErrorInvalidChar); + if (!sax.Double(d)) RETURN_SET_ERROR_CODE(kParseErrorInvalidChar); ; RETURN_SET_ERROR_CODE(error_code); } @@ -445,7 +443,8 @@ class Parser { #undef CHECK_DIGIT } - template void parsePrimitives(SAX &sax) { + template + void parsePrimitives(SAX &sax) { switch (json_buf_[pos_ - 1]) { case '0': case '1': @@ -479,9 +478,9 @@ class Parser { template sonic_force_inline void parseImpl(SAX &sax) { -#define sonic_check_err() \ - if (err_ != kErrorNone) { \ - goto err_invalid_char; \ +#define sonic_check_err() \ + if (err_ != kErrorNone) { \ + goto err_invalid_char; \ } using namespace sonic_json::internal; @@ -492,16 +491,16 @@ class Parser { uint8_t c = scan.SkipSpace(json_buf_, pos_); switch (c) { - case '[': { - sax.StartArray(); - depth.push_back(kArrMask); - c = scan.SkipSpace(json_buf_, pos_); - if (c == ']') { - sax.EndArray(0); - goto scope_end; - } - goto arr_val; - }; + case '[': { + sax.StartArray(); + depth.push_back(kArrMask); + c = scan.SkipSpace(json_buf_, pos_); + if (c == ']') { + sax.EndArray(0); + goto scope_end; + } + goto arr_val; + }; case '{': { sax.StartObject(); depth.push_back(kObjMask); @@ -515,7 +514,7 @@ class Parser { default: parsePrimitives(sax); goto doc_end; - }; + }; obj_key: if (sonic_unlikely(c != '"')) goto err_invalid_char; @@ -526,16 +525,16 @@ class Parser { c = scan.SkipSpace(json_buf_, pos_); switch (c) { - case '{': { - sax.StartObject(); - depth.push_back(kObjMask); - c = scan.SkipSpace(json_buf_, pos_); - if (c == '}') { - sax.EndObject(0); - goto scope_end; + case '{': { + sax.StartObject(); + depth.push_back(kObjMask); + c = scan.SkipSpace(json_buf_, pos_); + if (c == '}') { + sax.EndObject(0); + goto scope_end; + } + goto obj_key; } - goto obj_key; - } case '[': { sax.StartArray(); depth.push_back(kArrMask); @@ -578,7 +577,7 @@ class Parser { break; default: goto err_invalid_char; - } + } c = scan.SkipSpace(json_buf_, pos_); obj_cont: @@ -606,16 +605,16 @@ class Parser { arr_val: switch (c) { - case '{': { - sax.StartObject(); - depth.push_back(kObjMask); - c = scan.SkipSpace(json_buf_, pos_); - if (c == '}') { - sax.EndObject(0); - goto scope_end; + case '{': { + sax.StartObject(); + depth.push_back(kObjMask); + c = scan.SkipSpace(json_buf_, pos_); + if (c == '}') { + sax.EndObject(0); + goto scope_end; + } + goto obj_key; } - goto obj_key; - } case '[': { sax.StartArray(); depth.push_back(kArrMask); @@ -658,7 +657,7 @@ class Parser { break; default: goto err_invalid_char; - } + } c = scan.SkipSpace(json_buf_, pos_); arr_cont: @@ -699,34 +698,33 @@ class Parser { const uint8_t *src, *sdst; switch (c) { - case '[': { - sax.StartArray(); - c = scan.SkipSpaceSafe(data, pos, len); - if (c == ']') { - sax.EndArray(0); - return kErrorNone; + case '[': { + sax.StartArray(); + c = scan.SkipSpaceSafe(data, pos, len); + if (c == ']') { + sax.EndArray(0); + return kErrorNone; + } + pos--; + goto arr_val; + }; + case '{': { + sax.StartObject(); + c = scan.SkipSpaceSafe(data, pos, len); + if (c == '}') { + sax.EndObject(0); + return kErrorNone; + } + goto obj_key; } - pos--; - goto arr_val; - }; - case '{': { - sax.StartObject(); - c = scan.SkipSpaceSafe(data, pos, len); - if (c == '}') { - sax.EndObject(0); + default: { + // TODO: fix the abstract. + pos--; + start = scan.SkipOne(data, pos, len); + if (start < 0) goto skip_error; + sax.Raw(reinterpret_cast(data + start), pos - start); return kErrorNone; } - goto obj_key; - } - default: { - // TODO: fix the abstract. - pos--; - start = scan.SkipOne(data, pos, len); - if (start < 0) - goto skip_error; - sax.Raw(reinterpret_cast(data + start), pos - start); - return kErrorNone; - } }; obj_key: @@ -765,8 +763,7 @@ class Parser { goto err_invalid_char; } start = scan.SkipOne(data, pos, len); - if (start < 0) - goto skip_error; + if (start < 0) goto skip_error; sax.Raw(reinterpret_cast(data + start), pos - start); cnt++; c = scan.SkipSpaceSafe(data, pos, len); @@ -782,8 +779,7 @@ class Parser { arr_val: start = scan.SkipOne(data, pos, len); - if (start < 0) - goto skip_error; + if (start < 0) goto skip_error; sax.Raw(reinterpret_cast(data + start), pos - start); cnt++; c = scan.SkipSpaceSafe(data, pos, len); @@ -804,7 +800,7 @@ class Parser { #undef sonic_check_err -private: + private: sonic_force_inline void reset() { pos_ = 0; err_ = kErrorNone; @@ -812,7 +808,7 @@ class Parser { }; constexpr static size_t kJsonPaddingSize = SONICJSON_PADDING; - uint8_t* json_buf_{nullptr}; + uint8_t *json_buf_{nullptr}; size_t len_{0}; size_t pos_{0}; SonicError err_{kErrorNone}; diff --git a/include/sonic/dom/type.h b/include/sonic/dom/type.h index 991033c..6d75a2b 100644 --- a/include/sonic/dom/type.h +++ b/include/sonic/dom/type.h @@ -21,32 +21,32 @@ namespace sonic_json { enum TypeFlag { // BasicType: 3 bits - kNull = 0, // xxxxx000 - kBool = 2, // xxxxx010 - kNumber = 3, // xxxxx011 - kString = 4, // xxxxx100 - kRaw = 5, // xxxxx101 + kNull = 0, // xxxxx000 + kBool = 2, // xxxxx010 + kNumber = 3, // xxxxx011 + kString = 4, // xxxxx100 + kRaw = 5, // xxxxx101 // Container Mask 00000110, & Mask == Mask - kObject = 6, // xxxxx110 - kArray = 7, // xxxxx111 + kObject = 6, // xxxxx110 + kArray = 7, // xxxxx111 // SubType: 2 bits - kFalse = ((uint8_t)(0 << 3)) | kBool, // xxx00_010, 2 - kTrue = ((uint8_t)(1 << 3)) | kBool, // xxx01_010, 10 - kUint = ((uint8_t)(0 << 3)) | kNumber, // xxx00_011, 3 - kSint = ((uint8_t)(1 << 3)) | kNumber, // xxx01_011, 11 - kReal = ((uint8_t)(2 << 3)) | kNumber, // xxx10_011, 19 + kFalse = ((uint8_t)(0 << 3)) | kBool, // xxx00_010, 2 + kTrue = ((uint8_t)(1 << 3)) | kBool, // xxx01_010, 10 + kUint = ((uint8_t)(0 << 3)) | kNumber, // xxx00_011, 3 + kSint = ((uint8_t)(1 << 3)) | kNumber, // xxx01_011, 11 + kReal = ((uint8_t)(2 << 3)) | kNumber, // xxx10_011, 19 // kStringCopy: sv.p is copied, but not need free, e.g. node's string buffer // is dom str_ - kStringCopy = kString, // xxx00_100, 4 + kStringCopy = kString, // xxx00_100, 4 // kStringFree: sv.p is copied and need free, e.g. SetString with allocator // arg - kStringFree = ((uint8_t)(1 << 3)) | kString, // xxx01_100, 12 + kStringFree = ((uint8_t)(1 << 3)) | kString, // xxx01_100, 12 // kStringConst: sv.p is not copied, so not need free, e.g. SetString without // allocator arg - kStringConst = ((uint8_t)(2 << 3)) | kString, // xxx10_100, 20 + kStringConst = ((uint8_t)(2 << 3)) | kString, // xxx10_100, 20 -}; // 8 bits +}; // 8 bits enum TypeInfo { kTotalTypeBits = 8, diff --git a/include/sonic/experiment/lazy_update.h b/include/sonic/experiment/lazy_update.h index c76df50..0cc0a34 100644 --- a/include/sonic/experiment/lazy_update.h +++ b/include/sonic/experiment/lazy_update.h @@ -69,14 +69,13 @@ static inline SonicError UpdateNodeLazy(NodeType &target, NodeType &source, target.AddMember(key, iter->value, alloc); } else { err = UpdateNodeLazy(match->value, iter->value, alloc); - if (err) - return err; + if (err) return err; } } return err; } -} // namespace internal +} // namespace internal /** * @brief UpdateLazy will update the target json with the source json, and @@ -113,4 +112,4 @@ static inline std::string UpdateLazy(StringView target, StringView source) { return std::string(wb.ToString(), wb.Size()); } -} // namespace sonic_json \ No newline at end of file +} // namespace sonic_json \ No newline at end of file diff --git a/include/sonic/internal/atof_native.h b/include/sonic/internal/atof_native.h index 7dbbf54..34f8889 100644 --- a/include/sonic/internal/atof_native.h +++ b/include/sonic/internal/atof_native.h @@ -7,10 +7,10 @@ #pragma once -#include "sonic/internal/haswell.h" - #include +#include "sonic/internal/haswell.h" + namespace sonic_json { namespace internal { diff --git a/include/sonic/internal/haswell.h b/include/sonic/internal/haswell.h index 0391c47..e8ae66f 100644 --- a/include/sonic/internal/haswell.h +++ b/include/sonic/internal/haswell.h @@ -30,8 +30,7 @@ using namespace simd; // but the algorithms do not end up using the returned value. // Sadly, sanitizers are not smart enough to figure it out. -sonic_force_inline int -trailing_zeroes(uint64_t input_num) { +sonic_force_inline int trailing_zeroes(uint64_t input_num) { //////// // You might expect the next line to be equivalent to // return (int)_tzcnt_u64(input_num); diff --git a/include/sonic/internal/quote.h b/include/sonic/internal/quote.h index f6a410a..6d317d8 100644 --- a/include/sonic/internal/quote.h +++ b/include/sonic/internal/quote.h @@ -119,8 +119,8 @@ sonic_force_inline size_t parseStringInplace(uint8_t *&src, SonicError &err) { // Copy the next n bytes, and find the backslash and quote in them. simd256 v(src); block = StringBlock{ - static_cast((v == '\\').to_bitmask()), // bs_bits - static_cast((v == '"').to_bitmask()), // quote_bits + static_cast((v == '\\').to_bitmask()), // bs_bits + static_cast((v == '"').to_bitmask()), // quote_bits static_cast((v <= '\x1f').to_bitmask()), }; // If the next thing is the end quote, copy and return diff --git a/include/sonic/internal/skip.h b/include/sonic/internal/skip.h index 5a138f7..e85808d 100644 --- a/include/sonic/internal/skip.h +++ b/include/sonic/internal/skip.h @@ -127,8 +127,7 @@ sonic_force_inline int SkipString(const uint8_t *data, size_t &pos, break; } }; - if (pos >= len) - return kUnclosed; + if (pos >= len) return kUnclosed; return found ? kEscaped : kNormal; } @@ -140,26 +139,26 @@ sonic_force_inline bool SkipContainer(const uint8_t *data, size_t &pos, const uint8_t *p; while (pos + 64 <= len) { p = data + pos; -#define SKIP_LOOP() \ - { \ - instring = GetStringBits(p, prev_instring, prev_escaped); \ - simd::simd8x64 v(p); \ - last_lbrace_num = lbrace_num; \ - uint64_t rbrace = v.eq(right) & ~instring; \ - uint64_t lbrace = v.eq(left) & ~instring; \ - /* traverse each '}' */ \ - while (rbrace > 0) { \ - rbrace_num++; \ - lbrace_num = last_lbrace_num + count_ones((rbrace - 1) & lbrace); \ - bool is_closed = lbrace_num < rbrace_num; \ - if (is_closed) { \ - sonic_assert(rbrace_num == lbrace_num + 1); \ - pos += trailing_zeroes(rbrace) + 1; \ - return true; \ - } \ - rbrace &= (rbrace - 1); \ - } \ - lbrace_num = last_lbrace_num + count_ones(lbrace); \ +#define SKIP_LOOP() \ + { \ + instring = GetStringBits(p, prev_instring, prev_escaped); \ + simd::simd8x64 v(p); \ + last_lbrace_num = lbrace_num; \ + uint64_t rbrace = v.eq(right) & ~instring; \ + uint64_t lbrace = v.eq(left) & ~instring; \ + /* traverse each '}' */ \ + while (rbrace > 0) { \ + rbrace_num++; \ + lbrace_num = last_lbrace_num + count_ones((rbrace - 1) & lbrace); \ + bool is_closed = lbrace_num < rbrace_num; \ + if (is_closed) { \ + sonic_assert(rbrace_num == lbrace_num + 1); \ + pos += trailing_zeroes(rbrace) + 1; \ + return true; \ + } \ + rbrace &= (rbrace - 1); \ + } \ + lbrace_num = last_lbrace_num + count_ones(lbrace); \ } SKIP_LOOP(); pos += 64; @@ -186,27 +185,27 @@ sonic_force_inline bool SkipLiteral(const uint8_t *data, size_t &pos, static constexpr uint32_t kNullBin = 0x6c6c756e; static constexpr uint32_t kTrueBin = 0x65757274; static constexpr uint32_t kFalseBin = - 0x65736c61; // the binary of 'alse' in false + 0x65736c61; // the binary of 'alse' in false auto start = data + pos - 1; auto end = data + len; switch (token) { - case 't': - if (start + 4 <= end && EqBytes4(start, kTrueBin)) { - pos += 3; - return true; - }; - break; - case 'n': - if (start + 4 <= end && EqBytes4(start, kNullBin)) { - pos += 3; - return true; - }; - break; - case 'f': - if (start + 5 <= end && EqBytes4(start + 1, kFalseBin)) { - pos += 4; - return true; - } + case 't': + if (start + 4 <= end && EqBytes4(start, kTrueBin)) { + pos += 3; + return true; + }; + break; + case 'n': + if (start + 4 <= end && EqBytes4(start, kNullBin)) { + pos += 3; + return true; + }; + break; + case 'f': + if (start + 5 <= end && EqBytes4(start + 1, kFalseBin)) { + pos += 4; + return true; + } } return false; } @@ -264,10 +263,8 @@ class SkipScanner { goto tail; } // fast path for single space - if (!IsSpace(data[pos++])) - return data[pos - 1]; - if (!IsSpace(data[pos++])) - return data[pos - 1]; + if (!IsSpace(data[pos++])) return data[pos - 1]; + if (!IsSpace(data[pos++])) return data[pos - 1]; uint64_t nonspace; // current pos is out of block @@ -316,24 +313,24 @@ class SkipScanner { index--; char c = SkipSpaceSafe(data, pos, len); switch (c) { - case '{': { - if (!SkipObject(data, pos, len)) { - return kParseErrorInvalidChar; + case '{': { + if (!SkipObject(data, pos, len)) { + return kParseErrorInvalidChar; + } + break; } - break; - } - case '[': { - if (!SkipArray(data, pos, len)) { - return kParseErrorInvalidChar; + case '[': { + if (!SkipArray(data, pos, len)) { + return kParseErrorInvalidChar; + } + break; } - break; - } - case '"': { - if (!SkipString(data, pos, len)) { - return kParseErrorInvalidChar; + case '"': { + if (!SkipString(data, pos, len)) { + return kParseErrorInvalidChar; + } + break; } - break; - } } // skip space and primitives // TODO (liuq): fast path for compat json. @@ -354,44 +351,40 @@ class SkipScanner { long err = -kParseErrorInvalidChar; switch (c) { - case '"': { - if (!SkipString(data, pos, len)) - return err; - break; - } - case '{': { - if (!SkipObject(data, pos, len)) - return err; - break; - } - case '[': { - if (!SkipArray(data, pos, len)) - return err; - break; - } - case 't': - case 'n': - case 'f': { - if (!SkipLiteral(data, pos, len, c)) + case '"': { + if (!SkipString(data, pos, len)) return err; + break; + } + case '{': { + if (!SkipObject(data, pos, len)) return err; + break; + } + case '[': { + if (!SkipArray(data, pos, len)) return err; + break; + } + case 't': + case 'n': + case 'f': { + if (!SkipLiteral(data, pos, len, c)) return err; + break; + } + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '-': { + SkipNumber(data, pos, len); + break; + } + default: return err; - break; - } - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case '-': { - SkipNumber(data, pos, len); - break; - } - default: - return err; } return start; } @@ -409,7 +402,7 @@ class SkipScanner { StringView key; int skips; // TODO: use stack smallvector here. - std::vector kbuf(32); // key buffer for parsed keys + std::vector kbuf(32); // key buffer for parsed keys const uint8_t *data = reinterpret_cast(json.data()); size_t len = json.size(); SonicError err = kErrorNone; @@ -418,19 +411,15 @@ class SkipScanner { if (i++ != path.size()) { c = SkipSpaceSafe(data, pos, len); if (path[i - 1].IsStr()) { - if (c != '{') - goto err_mismatch_type; + if (c != '{') goto err_mismatch_type; c = GetNextToken(data, pos, len, "\"}"); - if (c != '"') - goto err_unknown_key; + if (c != '"') goto err_unknown_key; key = StringView(path[i - 1].GetStr()); goto obj_key; } else { - if (c != '[') - goto err_mismatch_type; + if (c != '[') goto err_mismatch_type; err = GetArrayElem(data, pos, len, path[i - 1].GetNum()); - if (err) - return -err; + if (err) return -err; goto query; } } @@ -442,8 +431,7 @@ class SkipScanner { sp = data + pos; skips = SkipString(data, pos, len); sn = data + pos - 1 - sp; - if (!skips) - goto err_invalid_char; + if (!skips) goto err_invalid_char; if (skips == 2) { // parse escaped key kbuf.resize(sn + 32); @@ -467,24 +455,24 @@ class SkipScanner { } else { c = SkipSpaceSafe(data, pos, len); switch (c) { - case '{': { - if (!SkipObject(data, pos, len)) { - goto err_invalid_char; + case '{': { + if (!SkipObject(data, pos, len)) { + goto err_invalid_char; + } + break; } - break; - } - case '[': { - if (!SkipArray(data, pos, len)) { - goto err_invalid_char; + case '[': { + if (!SkipArray(data, pos, len)) { + goto err_invalid_char; + } + break; } - break; - } - case '"': { - if (!SkipString(data, pos, len)) { - goto err_invalid_char; + case '"': { + if (!SkipString(data, pos, len)) { + goto err_invalid_char; + } + break; } - break; - } } // skip space and , find next " or } c = GetNextToken(data, pos, len, "\"}"); diff --git a/include/sonic/macro.h b/include/sonic/macro.h index 2669e09..74542ff 100644 --- a/include/sonic/macro.h +++ b/include/sonic/macro.h @@ -47,7 +47,7 @@ #endif #ifdef SONIC_NHEADONLY // Not Headonly -#define sonic_static_inline +#define sonic_static_inline #else // Headonly #define sonic_static_inline static sonic_force_inline #endif diff --git a/include/sonic/writebuffer.h b/include/sonic/writebuffer.h index 1482576..74cf095 100644 --- a/include/sonic/writebuffer.h +++ b/include/sonic/writebuffer.h @@ -108,13 +108,15 @@ class WriteBuffer { top_ += sizeof(T); } - template sonic_force_inline T *PushSize(size_t n) { + template + sonic_force_inline T* PushSize(size_t n) { Grow(n * sizeof(T)); return PushSizeUnsafe(n); } - template sonic_force_inline T *PushSizeUnsafe(size_t n) { - T *ret = reinterpret_cast(top_); + template + sonic_force_inline T* PushSizeUnsafe(size_t n) { + T* ret = reinterpret_cast(top_); top_ += n * sizeof(T); return ret; } @@ -182,14 +184,16 @@ class WriteBuffer { * @brief Get the begin of the buffer. * @return the value pointer into the begin. */ - template sonic_force_inline T *Begin() { - return reinterpret_cast(buf_); + template + sonic_force_inline T* Begin() { + return reinterpret_cast(buf_); } - template sonic_force_inline const T *Begin() const { - return reinterpret_cast(buf_); + template + sonic_force_inline const T* Begin() const { + return reinterpret_cast(buf_); } -private: + private: void setZero() { buf_ = nullptr; top_ = nullptr;