simdjson 4.0.7
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 <std::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};
64template <std::size_t N>
65fixed_string(const char (&)[N]) -> fixed_string<N>;
66
67template <fixed_string str>
68struct string_constant {
69 static constexpr std::string_view value = str.view();
70};
71#endif // SIMDJSON_SUPPORTS_CONCEPTS
72
73} // namespace constevalutil
74} // namespace simdjson
75#endif // SIMDJSON_CONSTEVALUTIL_H
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8