18 #include <daw/daw_string_view.h>
21 #include <daw/stdinc/data_access.h>
22 #include <daw/stdinc/range_access.h>
27 namespace json_details {
28 namespace name::name_parser {
33 template<
typename ParseState>
34 static constexpr
void trim_end_of_name( ParseState &parse_state ) {
35 parse_state.trim_left( );
38 ErrorReason::InvalidMemberName, parse_state );
39 parse_state.remove_prefix( );
40 parse_state.trim_left( );
43 template<
typename ParseState>
44 [[nodiscard]] DAW_ATTRIB_INLINE
static constexpr daw::string_view
45 parse_nq( ParseState &parse_state ) {
46 if constexpr( ParseState::allow_escaped_names ) {
47 auto r = skip_string_nq( parse_state );
48 trim_end_of_name( parse_state );
49 return daw::string_view( std::data( r ), std::size( r ) );
51 char const *
const ptr = parse_state.first;
52 if constexpr( ParseState::is_unchecked_input ) {
53 parse_state.template move_to_next_of_unchecked<
'"'>( );
55 parse_state.template move_to_next_of_checked<
'"'>( );
58 *std::prev( parse_state.first ) !=
'\\',
59 ErrorReason::InvalidString, parse_state );
60 auto result = daw::string_view( ptr, parse_state.first );
61 parse_state.remove_prefix( );
62 trim_end_of_name( parse_state );
68 struct pop_json_path_result {
69 daw::string_view current{ };
75 [[nodiscard]]
static constexpr pop_json_path_result
76 pop_json_path( daw::string_view &path ) {
77 auto result = pop_json_path_result{ };
81 if( path.front( ) ==
'.' ) {
82 path.remove_prefix( );
85 path.pop_front_until( [&, in_escape =
false](
char c )
mutable {
97 result.found_char = c;
106 [[nodiscard]]
static constexpr
bool
107 json_path_compare( daw::string_view json_path_item,
108 daw::string_view member_name ) {
109 if( json_path_item.front( ) ==
'\\' ) {
110 json_path_item.remove_prefix( );
112 while( not json_path_item.empty( ) and not member_name.empty( ) ) {
113 if( json_path_item.front( ) != member_name.front( ) ) {
116 json_path_item.remove_prefix( );
117 if( not json_path_item.empty( ) and
118 json_path_item.front( ) ==
'\\' ) {
119 json_path_item.remove_prefix( );
121 member_name.remove_prefix( );
123 return std::size( json_path_item ) == std::size( member_name );
126 template<
typename Result,
typename ForwardIterator>
127 constexpr Result parse_unsigned_int( ForwardIterator first,
128 ForwardIterator last ) {
129 std::size_t count = std::numeric_limits<Result>::digits;
134 for( ; first != last and count > 0; ++first, --count ) {
135 result *=
static_cast<Result
>( 10 );
137 static_cast<Result
>( *first ) -
static_cast<Result
>(
'0' );
148 template<
typename ParseState>
149 [[nodiscard]] DAW_ATTRIB_FLATTEN
static inline constexpr daw::string_view
150 parse_name( ParseState &parse_state ) {
152 ErrorReason::InvalidMemberName, parse_state );
153 parse_state.remove_prefix( );
154 return name::name_parser::parse_nq( parse_state );
157 template<
typename ParseState>
158 static constexpr
bool find_range2( ParseState &parse_state,
159 daw::string_view path ) {
161 auto pop_result = pop_json_path( path );
162 while( not pop_result.current.empty( ) ) {
163 if( pop_result.found_char ==
']' ) {
166 ErrorReason::InvalidJSONPath, parse_state );
167 parse_state.remove_prefix( );
168 parse_state.trim_left_unchecked( );
169 auto idx = parse_unsigned_int<std::size_t>(
170 pop_result.current.data( ), pop_result.current.data_end( ) );
174 (void)skip_value( parse_state );
175 parse_state.trim_left_checked( );
176 if( ( idx > 0 ) & ( parse_state.has_more( ) and
177 ( parse_state.front( ) !=
',' ) ) ) {
180 parse_state.move_next_member_or_end( );
184 ErrorReason::InvalidJSONPath, parse_state );
185 parse_state.remove_prefix( );
186 parse_state.trim_left_unchecked( );
187 auto name = parse_name( parse_state );
188 while( not json_path_compare( pop_result.current, name ) ) {
189 (void)skip_value( parse_state );
190 parse_state.move_next_member_or_end( );
191 if( parse_state.empty( ) or parse_state.front( ) !=
'"' ) {
194 name = parse_name( parse_state );
197 pop_result = pop_json_path( path );
202 template<
typename ParsePolicy>
203 [[nodiscard]]
static constexpr find_result<ParsePolicy>
204 find_range( daw::string_view str, daw::string_view start_path ) {
206 ParsePolicy( std::data( str ), daw::data_end( str ) );
207 parse_state.trim_left_checked( );
209 if( parse_state.has_more( ) and not start_path.empty( ) ) {
210 found = find_range2( parse_state, start_path );
212 return find_result<ParsePolicy>{ parse_state, found };
215 template<
typename ParsePolicy,
typename Allocator>
216 [[nodiscard]]
static constexpr find_result<ParsePolicy>
217 find_range( daw::string_view str, daw::string_view start_path,
220 std::is_same_v<char const *, typename ParsePolicy::iterator>,
221 "Only char const * ranges are currently supported" );
222 auto parse_state = ParsePolicy::with_allocator(
223 std::data( str ), daw::data_end( str ), alloc );
224 parse_state.trim_left_checked( );
225 if( parse_state.has_more( ) and not start_path.empty( ) ) {
226 if( not find_range2( parse_state, start_path ) ) {
227 return find_result<ParsePolicy>{ parse_state,
false };
230 return find_result<ParsePolicy>{ parse_state,
true };
#define daw_json_assert_weak(Bool,...)
Assert that Bool is true when in Checked Input mode If false pass rest of args to daw_json_error.
#define daw_json_ensure(Bool,...)
Ensure that Bool is true. If false pass rest of args to daw_json_error.
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.