Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BasedOnStyle: Google
14 changes: 14 additions & 0 deletions .github/workflows/clang-format-check.yml
Original file line number Diff line number Diff line change
@@ -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'
16 changes: 6 additions & 10 deletions include/sonic/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -80,11 +78,11 @@ class SimpleAllocator {
#endif

// SpinLock is copied from https://rigtorp.se/spinlock/
class SpinLock
{
private:
class SpinLock {
private:
std::atomic<bool> lock_ = {false};
public:

public:
void lock() {
for (;;) {
if (!lock_.exchange(true, std::memory_order_acquire)) {
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions include/sonic/dom/dynamicnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ class DNode : public GenericNode<DNode, Allocator> {
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);
Expand Down Expand Up @@ -582,7 +582,7 @@ class DNode : public GenericNode<DNode, Allocator> {
return const_cast<MemberIterator>(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;
Expand Down Expand Up @@ -648,8 +648,7 @@ class DNode : public GenericNode<DNode, Allocator> {
} else {
m = memberBeginUnsafe();
for (; m != memberEndUnsafe(); ++m) // {
if (m->name.GetStringView() == key)
goto find;
if (m->name.GetStringView() == key) goto find;

goto not_find;
}
Expand All @@ -667,7 +666,7 @@ class DNode : public GenericNode<DNode, Allocator> {
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);
Expand Down
27 changes: 15 additions & 12 deletions include/sonic/dom/generic_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ class GenericDocument : public NodeType {
/**
* @brief Move constructor
*/
GenericDocument(GenericDocument &&rhs)
GenericDocument(GenericDocument&& rhs)
: NodeType(std::forward<NodeType>(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();
}

Expand Down Expand Up @@ -111,12 +114,12 @@ class GenericDocument : public NodeType {
* before parsing to avoid memory overuse.
*/
template <unsigned parseFlags = kParseDefault>
GenericDocument &Parse(StringView json) {
GenericDocument& Parse(StringView json) {
return Parse<parseFlags>(json.data(), json.size());
}

template <unsigned parseFlags = kParseDefault>
GenericDocument &Parse(const char *data, size_t len) {
GenericDocument& Parse(const char* data, size_t len) {
destroyDom();
return parseImpl<parseFlags>(data, len);
}
Expand All @@ -132,15 +135,15 @@ class GenericDocument : public NodeType {
*/
template <unsigned parseFlags = kParseDefault,
typename JPStringType = SONIC_JSON_POINTER_NODE_STRING_DEFAULT_TYPE>
GenericDocument &ParseOnDemand(StringView json,
const GenericJsonPointer<JPStringType> &path) {
GenericDocument& ParseOnDemand(StringView json,
const GenericJsonPointer<JPStringType>& path) {
return ParseOnDemand(json.data(), json.size(), path);
}

template <unsigned parseFlags = kParseDefault,
typename JPStringType = SONIC_JSON_POINTER_NODE_STRING_DEFAULT_TYPE>
GenericDocument &ParseOnDemand(const char *data, size_t len,
const GenericJsonPointer<JPStringType> &path) {
GenericDocument& ParseOnDemand(const char* data, size_t len,
const GenericJsonPointer<JPStringType>& path) {
destroyDom();
return parseOnDemandImpl<parseFlags, JPStringType>(data, len, path);
}
Expand Down Expand Up @@ -218,9 +221,9 @@ class GenericDocument : public NodeType {
return parseImpl<parseFlags>(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;
}
Expand Down
22 changes: 11 additions & 11 deletions include/sonic/dom/genericnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
/**
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -731,8 +731,8 @@ class GenericNode {
* @retval others success
*/
template <typename StringType>
sonic_force_inline const NodeType *
AtPointer(const GenericJsonPointer<StringType> &pointer) const {
sonic_force_inline const NodeType* AtPointer(
const GenericJsonPointer<StringType>& pointer) const {
return atPointerImpl(pointer);
}

Expand Down Expand Up @@ -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 {
Expand All @@ -1024,7 +1024,7 @@ class GenericNode {
return std::numeric_limits<uint>::max();
}

private:
private:
friend NodeType;
friend class SAXHandler<NodeType>;
friend class LazySAXHandler<NodeType>;
Expand Down Expand Up @@ -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 {
Expand Down
36 changes: 19 additions & 17 deletions include/sonic/dom/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@

namespace sonic_json {

template <typename NodeType> class GenericDocument;
template <typename NodeType>
class GenericDocument;

template <typename NodeType> class SAXHandler {
public:
template <typename NodeType>
class SAXHandler {
public:
using Allocator = typename NodeType::AllocatorType;

SAXHandler() = default;
Expand All @@ -36,7 +38,10 @@ template <typename NodeType> 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;
Expand Down Expand Up @@ -65,31 +70,27 @@ template <typename NodeType> 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<NodeType *>(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();
}
std::free(st_);
st_ = nullptr;
};

#define SONIC_ADD_NODE() \
{ \
if (!node()) \
return false; \
#define SONIC_ADD_NODE() \
{ \
if (!node()) return false; \
}

sonic_force_inline bool Null() noexcept {
Expand Down Expand Up @@ -181,7 +182,7 @@ template <typename NodeType> class SAXHandler {
return true;
}

private:
private:
friend class GenericDocument<NodeType>;

sonic_force_inline bool stringImpl(StringView s) {
Expand Down Expand Up @@ -209,8 +210,9 @@ template <typename NodeType> class SAXHandler {
Allocator *alloc_{nullptr};
};

template <typename NodeType> class LazySAXHandler {
public:
template <typename NodeType>
class LazySAXHandler {
public:
using Allocator = typename NodeType::AllocatorType;

LazySAXHandler() = delete;
Expand Down
Loading