Skip to content

Commit

Permalink
support escape map key
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbgan committed Jul 17, 2023
1 parent 5cdf52e commit cc8f6b2
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Clang Format Diff

on:
push:
branches: [ master, improve_xml]
branches: [ master, improve_json]
pull_request:
branches: [ master, improve_xml]
branches: [ master, improve_json]

jobs:
build:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/linux-clang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Ubuntu (clang)

on:
push:
branches: [ master, improve_xml]
branches: [ master, improve_json]
pull_request:
branches: [ master, improve_xml]
branches: [ master, improve_json]

jobs:
build:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/linux-gcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Ubuntu (gcc)

on:
push:
branches: [ master, improve_xml]
branches: [ master, improve_json]
pull_request:
branches: [ master, improve_xml]
branches: [ master, improve_json]

jobs:
build:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: macOS Monterey 12

on:
push:
branches: [ master, improve_xml]
branches: [ master, improve_json]
pull_request:
branches: [ master, improve_xml]
branches: [ master, improve_json]

jobs:
build:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Windows Server 2022

on:
push:
branches: [ master, improve_xml]
branches: [ master, improve_json]
pull_request:
branches: [ master, improve_xml]
branches: [ master, improve_json]

jobs:
build:
Expand Down
7 changes: 4 additions & 3 deletions benchmark/json_benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ struct venueNames_t {
REFLECTION(venueNames_t, PLEYEL_PLEYEL);

struct citm_object_t {
std::unordered_map<std::int64_t, std::string_view> areaNames;
std::unordered_map<std::int64_t, std::string_view> audienceSubCategoryNames;
std::unordered_map<std::string_view, std::string_view> areaNames;
std::unordered_map<std::string_view, std::string_view>
audienceSubCategoryNames;
apache_empty_t blockNames;
std::unordered_map<std::int64_t, events_value_t> events;
std::unordered_map<std::string_view, events_value_t> events;
std::vector<performances_element_t> performances;
std::unordered_map<std::string_view, std::string_view> seatCategoryNames;
std::unordered_map<std::string_view, std::string_view> subTopicNames;
Expand Down
66 changes: 32 additions & 34 deletions iguana/json_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,37 @@ IGUANA_INLINE void parse_item(U &value, It &&it, It &&end) {
throw std::runtime_error("Expected ]");
}

template <typename It> IGUANA_INLINE auto get_key(It &&it, It &&end) {
if constexpr (std::contiguous_iterator<std::decay_t<It>>) {
// skip white space and escape characters and find the string
skip_ws(it, end);
match<'"'>(it, end);
auto start = it;
skip_till_escape_or_qoute(it, end);
if (*it == '\\') [[unlikely]] {
// we dont' optimize this currently because it would increase binary
// size significantly with the complexity of generating escaped
// compile time versions of keys
it = start;
static thread_local std::string static_key{};
detail::parse_item<true>(static_key, it, end);
return std::string_view(static_key);
} else [[likely]] {
auto key = std::string_view{
&*start, static_cast<size_t>(std::distance(start, it))};
++it;
if (key[0] == '@') [[unlikely]] {
return key.substr(1);
}
return key;
}
} else {
static thread_local std::string static_key{};
detail::parse_item(static_key, it, end);
return std::string_view(static_key);
}
}

template <map_container U, class It>
IGUANA_INLINE void parse_item(U &value, It &&it, It &&end) {
using T = std::remove_reference_t<U>;
Expand All @@ -316,8 +347,7 @@ IGUANA_INLINE void parse_item(U &value, It &&it, It &&end) {
match<','>(it, end);
}

static thread_local std::string_view key{};
parse_item(key, it, end);
std::string_view key = get_key(it, end);

skip_ws(it, end);
match<':'>(it, end);
Expand Down Expand Up @@ -434,38 +464,6 @@ IGUANA_INLINE void skip_object_value(auto &&it, auto &&end) {
}
}

// spaces "key"
template <typename It> IGUANA_INLINE auto get_key(It &&it, It &&end) {
if constexpr (std::contiguous_iterator<std::decay_t<It>>) {
// skip white space and escape characters and find the string
skip_ws(it, end);
match<'"'>(it, end);
auto start = it;
skip_till_escape_or_qoute(it, end);
if (*it == '\\') [[unlikely]] {
// we dont' optimize this currently because it would increase binary
// size significantly with the complexity of generating escaped
// compile time versions of keys
it = start;
static thread_local std::string static_key{};
detail::parse_item<true>(static_key, it, end);
return std::string_view(static_key);
} else [[likely]] {
auto key = std::string_view{
&*start, static_cast<size_t>(std::distance(start, it))};
++it;
if (key[0] == '@') [[unlikely]] {
return key.substr(1);
}
return key;
}
} else {
static thread_local std::string static_key{};
detail::parse_item(static_key, it, end);
return std::string_view(static_key);
}
}

} // namespace detail

template <refletable T, typename It>
Expand Down

0 comments on commit cc8f6b2

Please sign in to comment.