DAW JSON Link
Loading...
Searching...
No Matches
daw_json_string_util.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_exec_modes.h"
16
17#include <daw/daw_is_constant_evaluated.h>
18#include <daw/daw_likely.h>
19#include <daw/daw_traits.h>
20
21#include <cstring>
22
23namespace daw::json {
24 inline namespace DAW_JSON_VER {
25 namespace json_details {
35 template<char c, typename ExecTag, bool expect_long, typename CharT>
36 DAW_ATTRIB_NONNULL( )
37 DAW_ATTRIB_RET_NONNULL DAW_ATTRIB_FLATINLINE
38 static inline constexpr CharT *memchr_unchecked( CharT *first,
39 CharT *last ) {
40#if DAW_HAS_BUILTIN( __builtin_char_memchr )
41 if constexpr( expect_long ) {
42 return __builtin_char_memchr(
43 first, '"', static_cast<std::size_t>( last - first ) );
44 } else
45#else
46#if defined( DAW_IS_CONSTANT_EVALUATED )
47 bool is_cxeval =
48 DAW_IS_CONSTANT_EVALUATED( ) | DAW_CAN_CONSTANT_EVAL( first );
49#else
50 bool is_cxeval = DAW_CAN_CONSTANT_EVAL( first );
51#endif
52 if constexpr( expect_long ) {
53 if( ( not is_cxeval ) |
54 daw::traits::not_same_v<ExecTag, constexpr_exec_tag> ) {
55 return static_cast<CharT *>(
56 std::memchr( static_cast<void const *>( first ), '"',
57 static_cast<std::size_t>( last - first ) ) );
58 }
59 (void)last;
60 while( *first != c ) {
61 ++first;
62 }
63 return first;
64 } else
65#endif
66 {
67 (void)last;
68 while( *first != c ) {
69 ++first;
70 }
71 return first;
72 }
73 }
74
84 template<char c, typename ExecTag, bool expect_long, typename CharT>
85 DAW_ATTRIB_NONNULL( )
86 DAW_ATTRIB_RET_NONNULL DAW_ATTRIB_FLATINLINE
87 static inline constexpr CharT *memchr_checked( CharT *first,
88 CharT *last ) {
89#if DAW_HAS_BUILTIN( __builtin_char_memchr )
90 if constexpr( expect_long ) {
91 return __builtin_char_memchr(
92 first, '"', static_cast<std::size_t>( last - first ) );
93 } else
94#elif DAW_HAS_BUILTIN( __builtin_memchr )
95 if constexpr( expect_long ) {
96 return static_cast<CharT *>( __builtin_memchr(
97 first, '"', static_cast<std::size_t>( last - first ) ) );
98 } else
99#else
100#if defined( DAW_IS_CONSTANT_EVALUATED )
101 bool is_cxeval =
102 DAW_IS_CONSTANT_EVALUATED( ) | DAW_CAN_CONSTANT_EVAL( first );
103#else
104 bool is_cxeval = DAW_CAN_CONSTANT_EVAL( first );
105#endif
106 if constexpr( expect_long ) {
107 if( ( not is_cxeval ) |
108 daw::traits::not_same_v<ExecTag, constexpr_exec_tag> ) {
109 return static_cast<CharT *>(
110 std::memchr( static_cast<void const *>( first ), '"',
111 static_cast<std::size_t>( last - first ) ) );
112 }
113 while( DAW_LIKELY( first < last ) and *first != c ) {
114 ++first;
115 }
116 return first;
117 } else
118#endif
119 {
120 while( DAW_LIKELY( first < last ) and *first != c ) {
121 ++first;
122 }
123 return first;
124 }
125 }
126
127 template<typename ExecTag, bool expect_long, char... chars,
128 typename CharT>
129 DAW_ATTRIB_NONNULL( )
130 DAW_ATTRIB_RET_NONNULL DAW_ATTRIB_FLATINLINE
131 static inline constexpr CharT *mempbrk_unchecked( CharT *first,
132 CharT * /*last*/ ) {
133#if DAW_HAS_BUILTIN( __builtin_strpbrk )
134 if constexpr( expect_long ) {
135 constexpr char const needles[]{ chars..., '\0' };
136 CharT *res = __builtin_strpbrk( first, needles );
137#if not defined( NDEBUG )
138 daw_json_ensure( res != nullptr, ErrorReason::UnexpectedEndOfData );
139#endif
140 return res;
141 } else
142#else
143#if defined( DAW_IS_CONSTANT_EVALUATED )
144 bool is_cxeval =
145 DAW_IS_CONSTANT_EVALUATED( ) | DAW_CAN_CONSTANT_EVAL( first );
146#else
147 bool is_cxeval = DAW_CAN_CONSTANT_EVAL( first );
148#endif
149 if constexpr( expect_long ) {
150 if( ( not is_cxeval ) |
151 daw::traits::not_same_v<ExecTag, constexpr_exec_tag> ) {
152 constexpr char const needles[]{ chars..., '\0' };
153 CharT *res = std::strpbrk( first, needles );
154#if not defined( NDEBUG )
155 daw_json_ensure( res != nullptr, ErrorReason::UnexpectedEndOfData );
156#endif
157 return res;
158 }
159 while( not parse_policy_details::in<chars...>( *first ) ) {
160 ++first;
161 }
162 return first;
163 } else
164#endif
165 {
166 while( not parse_policy_details::in<chars...>( *first ) ) {
167 ++first;
168 }
169 return first;
170 }
171 }
172
173 template<typename ExecTag, bool expect_long, char... chars,
174 typename CharT>
175 DAW_ATTRIB_NONNULL( )
176 DAW_ATTRIB_RET_NONNULL DAW_ATTRIB_FLATINLINE
177 static inline constexpr CharT *mempbrk_checked( CharT *first,
178 CharT *last ) {
179 if constexpr( expect_long ) {
180#if defined( DAW_IS_CONSTANT_EVALUATED )
181 bool is_cxeval =
182 DAW_IS_CONSTANT_EVALUATED( ) | DAW_CAN_CONSTANT_EVAL( first );
183#else
184 bool is_cxeval = DAW_CAN_CONSTANT_EVAL( first );
185#endif
186 if( ( not is_cxeval ) |
187 daw::traits::not_same_v<ExecTag, constexpr_exec_tag> ) {
188
189 return mem_move_to_next_of<false, chars...>( ExecTag{}, first, last );
190 }
191 while( DAW_LIKELY( first < last ) and
192 not parse_policy_details::in<chars...>( *first ) ) {
193 ++first;
194 }
195 return first;
196 } else {
197 while( DAW_LIKELY( first < last ) and
198 not parse_policy_details::in<chars...>( *first ) ) {
199 ++first;
200 }
201 return first;
202 }
203 }
204
205 template<bool is_unchecked_input, typename ExecTag, bool expect_long,
206 char... chars, typename CharT>
207 DAW_ATTRIB_NONNULL( )
208 DAW_ATTRIB_RET_NONNULL DAW_ATTRIB_FLATINLINE
209 static inline constexpr CharT *mempbrk( CharT *first, CharT *last ) {
210
211 if constexpr( is_unchecked_input ) {
212 return mempbrk_unchecked<ExecTag, expect_long, chars...>( first,
213 last );
214 } else {
215 return mempbrk_checked<ExecTag, expect_long, chars...>( first, last );
216 }
217 }
218 } // namespace json_details
219 } // namespace DAW_JSON_VER
220} // namespace daw::json
#define daw_json_ensure(Bool,...)
Ensure that Bool is true. If false pass rest of args to daw_json_error.
#define DAW_CAN_CONSTANT_EVAL(...)
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20