simdjson 4.2.3
Ridiculously Fast JSON
Loading...
Searching...
No Matches
constevalutil.h
1#ifndef SIMDJSON_CONSTEVALUTIL_H
2#define SIMDJSON_CONSTEVALUTIL_H
3
4#include <string>
5#include <string_view>
6#include <array>
7
8namespace simdjson {
9namespace constevalutil {
10#if SIMDJSON_CONSTEVAL
11
12constexpr static std::array<uint8_t, 256> json_quotable_character = {
13 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
14 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
17 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
24constexpr static std::array<std::string_view, 32> control_chars = {
25 "\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005", "\\u0006",
26 "\\u0007", "\\b", "\\t", "\\n", "\\u000b", "\\f", "\\r",
27 "\\u000e", "\\u000f", "\\u0010", "\\u0011", "\\u0012", "\\u0013", "\\u0014",
28 "\\u0015", "\\u0016", "\\u0017", "\\u0018", "\\u0019", "\\u001a", "\\u001b",
29 "\\u001c", "\\u001d", "\\u001e", "\\u001f"};
30// unoptimized, meant for compile-time execution
31consteval std::string consteval_to_quoted_escaped(std::string_view input) {
32 std::string out = "\"";
33 for (char c : input) {
34 if (json_quotable_character[uint8_t(c)]) {
35 if (c == '"') {
36 out.append("\\\"");
37 } else if (c == '\\') {
38 out.append("\\\\");
39 } else {
40 std::string_view v = control_chars[uint8_t(c)];
41 out.append(v);
42 }
43 } else {
44 out.push_back(c);
45 }
46 }
47 out.push_back('"');
48 return out;
49}
50#endif // SIMDJSON_CONSTEVAL
51
52
53#if SIMDJSON_SUPPORTS_CONCEPTS
54template <size_t N>
55struct fixed_string {
56 constexpr fixed_string(const char (&str)[N]) {
57 for (std::size_t i = 0; i < N; ++i) {
58 data[i] = str[i];
59 }
60 }
61 char data[N];
62 constexpr std::string_view view() const { return {data, N - 1}; }
63 constexpr size_t size() const { return N ; }
64 constexpr operator std::string_view() const { return view(); }
65 constexpr char operator[](std::size_t index) const { return data[index]; }
66 constexpr bool operator==(const fixed_string& other) const {
67 if (N != other.size()) {
68 return false;
69 }
70 for (std::size_t i = 0; i < N; ++i) {
71 if (data[i] != other.data[i]) {
72 return false;
73 }
74 }
75 return true;
76 }
77};
78template <std::size_t N>
79fixed_string(const char (&)[N]) -> fixed_string<N>;
80
81template <fixed_string str>
82struct string_constant {
83 static constexpr std::string_view value = str.view();
84};
85#endif // SIMDJSON_SUPPORTS_CONCEPTS
86
87} // namespace constevalutil
88} // namespace simdjson
89#endif // SIMDJSON_CONSTEVALUTIL_H
simdjson_unused simdjson_inline bool operator==(const raw_json_string &a, std::string_view c) noexcept
Comparisons between raw_json_string and std::string_view instances are potentially unsafe: the user i...
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8