simdjson  3.7.0
Ridiculously Fast JSON
json_path_to_pointer_conversion-inl.h
1 #pragma once
2 
3 #ifndef SIMDJSON_ONDEMAND_GENERIC_JSON_PATH_TO_POINTER_CONVERSION_INL_H
4 #define SIMDJSON_ONDEMAND_GENERIC_JSON_PATH_TO_POINTER_CONVERSION_INL_H
5 
6 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
7 #define SIMDJSON_GENERIC_ONDEMAND_JSON_PATH_TO_POINTER_CONVERSION_INL_H
8 #include "simdjson/generic/ondemand/json_path_to_pointer_conversion.h"
9 #endif // SIMDJSON_CONDITIONAL_INCLUDE
10 
11 namespace simdjson {
12 namespace SIMDJSON_IMPLEMENTATION {}
13 namespace ondemand {
14 
15 simdjson_inline std::string json_path_to_pointer_conversion(std::string_view json_path) {
16  if (json_path.empty() || (json_path.front() != '.' && json_path.front() != '[') {
17  return "-1"; // Sentinel value to be handled as an error by the caller.
18  }
19 
20  std::string result;
21  // Reserve space to reduce allocations, adjusting for potential increases due
22  // to escaping.
23  result.reserve(json_path.size() * 2);
24 
25  // Skip the initial '.' as it's assumed every path starts with it.
26  size_t i = 0;
27 
28  while (i < json_path.length()) {
29  if (json_path[i] == '.') {
30  result += '/';
31  } else if (json_path[i] == '[') {
32  result += '/';
33  ++i; // Move past the '['
34  while (i < json_path.length() && json_path[i] != ']') {
35  if (json_path[i] == '~') {
36  result += "~0";
37  } else if (json_path[i] == '/') {
38  result += "~1";
39  } else {
40  result += json_path[i];
41  }
42  ++i;
43  }
44  if (i == json_path.length() || json_path[i] != ']') {
45  return "-1"; // Returning sentinel value that will be handled as an error by the caller
46  }
47  } else {
48  if (json_path[i] == '~') {
49  result += "~0";
50  } else if (json_path[i] == '/') {
51  result += "~1";
52  } else {
53  result += json_path[i];
54  }
55  }
56  ++i;
57  }
58 
59  return simdjson_result<std::string>(result);
60 }
61 
62 
63 } // namespace ondemand
64 } // namespace SIMDJSON_IMPLEMENTATION
65 } // namespace simdjson
66 
67 #endif // SIMDJSON_ONDEMAND_GENERIC_JSON_PATH_TO_POINTER_CONVERSION_INL_H
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8