DAW JSON Link
Loading...
Searching...
No Matches
daw_json_parse_policy_policy_details.h
Go to the documentation of this file.
1// Copyright (c) Darrell Wright
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5//
6// Official repository: https://github.com/beached/daw_json_link
7//
8
9#pragma once
10
11#include "version.h"
12
13#include "daw_json_assert.h"
14
15#include <daw/daw_attributes.h>
16
17namespace daw::json {
18 inline namespace DAW_JSON_VER {
19 namespace parse_policy_details {
20 template<char... keys>
21 [[nodiscard]] DAW_ATTRIB_FLATINLINE static inline constexpr bool
22 in( char c ) {
23 auto const eq = [c]( char k ) {
24 return c == k;
25 };
26 return nsc_or( eq( keys )... );
27 }
28
29 [[nodiscard]] DAW_ATTRIB_FLATINLINE static inline constexpr bool
30 at_end_of_item( char c ) {
31 return static_cast<bool>( static_cast<unsigned>( c == ',' ) |
32 static_cast<unsigned>( c == '}' ) |
33 static_cast<unsigned>( c == ']' ) |
34 static_cast<unsigned>( c == ':' ) |
35 static_cast<unsigned>( c <= 0x20 ) );
36 }
37
38 [[nodiscard]] DAW_ATTRIB_FLATINLINE static inline constexpr bool
39 is_number( char c ) {
40 return static_cast<unsigned>( static_cast<unsigned char>( c ) -
41 static_cast<unsigned char>( '0' ) ) < 10U;
42 }
43
44 template<typename ParseState>
45 DAW_ATTRIB_FLATINLINE static inline constexpr void
46 validate_unsigned_first( ParseState const &parse_state ) {
47 if constexpr( not ParseState::is_unchecked_input ) {
48 switch( parse_state.front( ) ) {
49 case '1':
50 case '2':
51 case '3':
52 case '4':
53 case '5':
54 case '6':
55 case '7':
56 case '8':
57 case '9':
58 return;
59 case '0':
60 if( parse_state.size( ) > 1 ) {
61 daw_json_ensure( is_number( *( parse_state.first + 1 ) ),
62 ErrorReason::InvalidNumberStart, parse_state );
63 }
64 return;
65 default:
66 daw_json_error( ErrorReason::InvalidNumberStart, parse_state );
67 }
68 }
69 }
70
71 /***
72 * Validate that this is a valid number start(e.g. not leading zero when
73 * not zero)
74 * @return sign value
75 */
76 template<typename ParseState>
77 [[nodiscard]] DAW_ATTRIB_FLATINLINE static inline constexpr int
78 validate_signed_first( ParseState &parse_state ) {
79 daw_json_assert_weak( parse_state.has_more( ),
80 ErrorReason::UnexpectedEndOfData, parse_state );
81 auto const c = parse_state.front( );
82 if( c == '-' ) {
83 parse_state.remove_prefix( );
84 return -1;
85 }
86 if constexpr( ParseState::is_unchecked_input ) {
87 if( DAW_LIKELY( c >= '0' ) and DAW_LIKELY( c <= '9' ) ) {
88 return 1;
89 }
90 } else {
91 if( c >= '1' and c <= '9' ) {
92 return 1;
93 }
94 if( DAW_LIKELY( c == '0' ) ) {
95 if( parse_state.size( ) > 1 ) {
96 auto const next_dig = static_cast<unsigned>(
97 static_cast<unsigned char>( *( parse_state.first + 1 ) ) );
98 auto const tst = next_dig - static_cast<unsigned char>( '0' );
99 // Cannot be a digit
100 daw_json_assert_weak( tst >= 10U, ErrorReason::InvalidNumberStart,
101 parse_state );
102 }
103 return 1;
104 }
105 }
106 daw_json_error( ErrorReason::InvalidNumberStart, parse_state );
107 }
108
109 [[nodiscard]] DAW_ATTRIB_FLATINLINE static inline constexpr bool
110 is_number_start( char c ) {
111 switch( c ) {
112 case '0': // TODO: CONFORMANCE We are accepting starting with zero for
113 // now
114 case '1':
115 case '2':
116 case '3':
117 case '4':
118 case '5':
119 case '6':
120 case '7':
121 case '8':
122 case '9':
123 case '-':
124 return true;
125 }
126 return false;
127 }
128 } // namespace parse_policy_details
129 } // namespace DAW_JSON_VER
130} // namespace daw::json
#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.
DAW_ATTRIB_NOINLINE void daw_json_error(ErrorReason reason)
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20