simdjson  3.7.0
Ridiculously Fast JSON
string_view.hpp
1 // Copyright 2017-2020 by Martin Moene
2 //
3 // string-view lite, a C++17-like string_view for C++98 and later.
4 // For more information see https://github.com/martinmoene/string-view-lite
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 
9 // #pragma once // We remove #pragma once here as it generates a warning in some cases. We rely on the include guard.
10 
11 #ifndef NONSTD_SV_LITE_H_INCLUDED
12 #define NONSTD_SV_LITE_H_INCLUDED
13 
14 #define string_view_lite_MAJOR 1
15 #define string_view_lite_MINOR 7
16 #define string_view_lite_PATCH 0
17 
18 #define string_view_lite_VERSION nssv_STRINGIFY(string_view_lite_MAJOR) "." nssv_STRINGIFY(string_view_lite_MINOR) "." nssv_STRINGIFY(string_view_lite_PATCH)
19 
20 #define nssv_STRINGIFY( x ) nssv_STRINGIFY_( x )
21 #define nssv_STRINGIFY_( x ) #x
22 
23 // string-view lite configuration:
24 
25 #define nssv_STRING_VIEW_DEFAULT 0
26 #define nssv_STRING_VIEW_NONSTD 1
27 #define nssv_STRING_VIEW_STD 2
28 
29 // tweak header support:
30 
31 #ifdef __has_include
32 # if __has_include(<nonstd/string_view.tweak.hpp>)
33 # include <nonstd/string_view.tweak.hpp>
34 # endif
35 #define nssv_HAVE_TWEAK_HEADER 1
36 #else
37 #define nssv_HAVE_TWEAK_HEADER 0
38 //# pragma message("string_view.hpp: Note: Tweak header not supported.")
39 #endif
40 
41 // string_view selection and configuration:
42 
43 #if !defined( nssv_CONFIG_SELECT_STRING_VIEW )
44 # define nssv_CONFIG_SELECT_STRING_VIEW ( nssv_HAVE_STD_STRING_VIEW ? nssv_STRING_VIEW_STD : nssv_STRING_VIEW_NONSTD )
45 #endif
46 
47 #ifndef nssv_CONFIG_STD_SV_OPERATOR
48 # define nssv_CONFIG_STD_SV_OPERATOR 0
49 #endif
50 
51 #ifndef nssv_CONFIG_USR_SV_OPERATOR
52 # define nssv_CONFIG_USR_SV_OPERATOR 1
53 #endif
54 
55 #ifdef nssv_CONFIG_CONVERSION_STD_STRING
56 # define nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS nssv_CONFIG_CONVERSION_STD_STRING
57 # define nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS nssv_CONFIG_CONVERSION_STD_STRING
58 #endif
59 
60 #ifndef nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS
61 # define nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS 1
62 #endif
63 
64 #ifndef nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
65 # define nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS 1
66 #endif
67 
68 #ifndef nssv_CONFIG_NO_STREAM_INSERTION
69 # define nssv_CONFIG_NO_STREAM_INSERTION 0
70 #endif
71 
72 #ifndef nssv_CONFIG_CONSTEXPR11_STD_SEARCH
73 # define nssv_CONFIG_CONSTEXPR11_STD_SEARCH 1
74 #endif
75 
76 // Control presence of exception handling (try and auto discover):
77 
78 #ifndef nssv_CONFIG_NO_EXCEPTIONS
79 # if defined(_MSC_VER)
80 # include <cstddef> // for _HAS_EXCEPTIONS
81 # endif
82 # if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (_HAS_EXCEPTIONS)
83 # define nssv_CONFIG_NO_EXCEPTIONS 0
84 # else
85 # define nssv_CONFIG_NO_EXCEPTIONS 1
86 # endif
87 #endif
88 
89 // C++ language version detection (C++23 is speculative):
90 // Note: VC14.0/1900 (VS2015) lacks too much from C++14.
91 
92 #ifndef nssv_CPLUSPLUS
93 # if defined(_MSVC_LANG ) && !defined(__clang__)
94 # define nssv_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG )
95 # else
96 # define nssv_CPLUSPLUS __cplusplus
97 # endif
98 #endif
99 
100 #define nssv_CPP98_OR_GREATER ( nssv_CPLUSPLUS >= 199711L )
101 #define nssv_CPP11_OR_GREATER ( nssv_CPLUSPLUS >= 201103L )
102 #define nssv_CPP11_OR_GREATER_ ( nssv_CPLUSPLUS >= 201103L )
103 #define nssv_CPP14_OR_GREATER ( nssv_CPLUSPLUS >= 201402L )
104 #define nssv_CPP17_OR_GREATER ( nssv_CPLUSPLUS >= 201703L )
105 #define nssv_CPP20_OR_GREATER ( nssv_CPLUSPLUS >= 202002L )
106 #define nssv_CPP23_OR_GREATER ( nssv_CPLUSPLUS >= 202300L )
107 
108 // use C++17 std::string_view if available and requested:
109 
110 #if nssv_CPP17_OR_GREATER && defined(__has_include )
111 # if __has_include( <string_view> )
112 # define nssv_HAVE_STD_STRING_VIEW 1
113 # else
114 # define nssv_HAVE_STD_STRING_VIEW 0
115 # endif
116 #else
117 # define nssv_HAVE_STD_STRING_VIEW 0
118 #endif
119 
120 #define nssv_USES_STD_STRING_VIEW ( (nssv_CONFIG_SELECT_STRING_VIEW == nssv_STRING_VIEW_STD) || ((nssv_CONFIG_SELECT_STRING_VIEW == nssv_STRING_VIEW_DEFAULT) && nssv_HAVE_STD_STRING_VIEW) )
121 
122 #define nssv_HAVE_STARTS_WITH ( nssv_CPP20_OR_GREATER || !nssv_USES_STD_STRING_VIEW )
123 #define nssv_HAVE_ENDS_WITH nssv_HAVE_STARTS_WITH
124 
125 //
126 // Use C++17 std::string_view:
127 //
128 
129 #if nssv_USES_STD_STRING_VIEW
130 
131 #include <string_view>
132 
133 // Extensions for std::string:
134 
135 #if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
136 
137 namespace nonstd {
138 
139 template< class CharT, class Traits, class Allocator = std::allocator<CharT> >
140 std::basic_string<CharT, Traits, Allocator>
141 to_string( std::basic_string_view<CharT, Traits> v, Allocator const & a = Allocator() )
142 {
143  return std::basic_string<CharT,Traits, Allocator>( v.begin(), v.end(), a );
144 }
145 
146 template< class CharT, class Traits, class Allocator >
147 std::basic_string_view<CharT, Traits>
148 to_string_view( std::basic_string<CharT, Traits, Allocator> const & s )
149 {
150  return std::basic_string_view<CharT, Traits>( s.data(), s.size() );
151 }
152 
153 // Literal operators sv and _sv:
154 
155 #if nssv_CONFIG_STD_SV_OPERATOR
156 
157 using namespace std::literals::string_view_literals;
158 
159 #endif
160 
161 #if nssv_CONFIG_USR_SV_OPERATOR
162 
163 inline namespace literals {
164 inline namespace string_view_literals {
165 
166 
167 constexpr std::string_view operator "" _sv( const char* str, size_t len ) noexcept // (1)
168 {
169  return std::string_view{ str, len };
170 }
171 
172 constexpr std::u16string_view operator "" _sv( const char16_t* str, size_t len ) noexcept // (2)
173 {
174  return std::u16string_view{ str, len };
175 }
176 
177 constexpr std::u32string_view operator "" _sv( const char32_t* str, size_t len ) noexcept // (3)
178 {
179  return std::u32string_view{ str, len };
180 }
181 
182 constexpr std::wstring_view operator "" _sv( const wchar_t* str, size_t len ) noexcept // (4)
183 {
184  return std::wstring_view{ str, len };
185 }
186 
187 }} // namespace literals::string_view_literals
188 
189 #endif // nssv_CONFIG_USR_SV_OPERATOR
190 
191 } // namespace nonstd
192 
193 #endif // nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
194 
195 namespace nonstd {
196 
197 using std::string_view;
198 using std::wstring_view;
199 using std::u16string_view;
200 using std::u32string_view;
201 using std::basic_string_view;
202 
203 // literal "sv" and "_sv", see above
204 
205 using std::operator==;
206 using std::operator!=;
207 using std::operator<;
208 using std::operator<=;
209 using std::operator>;
210 using std::operator>=;
211 
212 using std::operator<<;
213 
214 } // namespace nonstd
215 
216 #else // nssv_HAVE_STD_STRING_VIEW
217 
218 //
219 // Before C++17: use string_view lite:
220 //
221 
222 // Compiler versions:
223 //
224 // MSVC++ 6.0 _MSC_VER == 1200 nssv_COMPILER_MSVC_VERSION == 60 (Visual Studio 6.0)
225 // MSVC++ 7.0 _MSC_VER == 1300 nssv_COMPILER_MSVC_VERSION == 70 (Visual Studio .NET 2002)
226 // MSVC++ 7.1 _MSC_VER == 1310 nssv_COMPILER_MSVC_VERSION == 71 (Visual Studio .NET 2003)
227 // MSVC++ 8.0 _MSC_VER == 1400 nssv_COMPILER_MSVC_VERSION == 80 (Visual Studio 2005)
228 // MSVC++ 9.0 _MSC_VER == 1500 nssv_COMPILER_MSVC_VERSION == 90 (Visual Studio 2008)
229 // MSVC++ 10.0 _MSC_VER == 1600 nssv_COMPILER_MSVC_VERSION == 100 (Visual Studio 2010)
230 // MSVC++ 11.0 _MSC_VER == 1700 nssv_COMPILER_MSVC_VERSION == 110 (Visual Studio 2012)
231 // MSVC++ 12.0 _MSC_VER == 1800 nssv_COMPILER_MSVC_VERSION == 120 (Visual Studio 2013)
232 // MSVC++ 14.0 _MSC_VER == 1900 nssv_COMPILER_MSVC_VERSION == 140 (Visual Studio 2015)
233 // MSVC++ 14.1 _MSC_VER >= 1910 nssv_COMPILER_MSVC_VERSION == 141 (Visual Studio 2017)
234 // MSVC++ 14.2 _MSC_VER >= 1920 nssv_COMPILER_MSVC_VERSION == 142 (Visual Studio 2019)
235 
236 #if defined(_MSC_VER ) && !defined(__clang__)
237 # define nssv_COMPILER_MSVC_VER (_MSC_VER )
238 # define nssv_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) )
239 #else
240 # define nssv_COMPILER_MSVC_VER 0
241 # define nssv_COMPILER_MSVC_VERSION 0
242 #endif
243 
244 #define nssv_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) )
245 
246 #if defined( __apple_build_version__ )
247 # define nssv_COMPILER_APPLECLANG_VERSION nssv_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
248 # define nssv_COMPILER_CLANG_VERSION 0
249 #elif defined( __clang__ )
250 # define nssv_COMPILER_APPLECLANG_VERSION 0
251 # define nssv_COMPILER_CLANG_VERSION nssv_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
252 #else
253 # define nssv_COMPILER_APPLECLANG_VERSION 0
254 # define nssv_COMPILER_CLANG_VERSION 0
255 #endif
256 
257 #if defined(__GNUC__) && !defined(__clang__)
258 # define nssv_COMPILER_GNUC_VERSION nssv_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
259 #else
260 # define nssv_COMPILER_GNUC_VERSION 0
261 #endif
262 
263 // half-open range [lo..hi):
264 #define nssv_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) )
265 
266 // Presence of language and library features:
267 
268 #ifdef _HAS_CPP0X
269 # define nssv_HAS_CPP0X _HAS_CPP0X
270 #else
271 # define nssv_HAS_CPP0X 0
272 #endif
273 
274 // Unless defined otherwise below, consider VC14 as C++11 for string-view-lite:
275 
276 #if nssv_COMPILER_MSVC_VER >= 1900
277 # undef nssv_CPP11_OR_GREATER
278 # define nssv_CPP11_OR_GREATER 1
279 #endif
280 
281 #define nssv_CPP11_90 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1500)
282 #define nssv_CPP11_100 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1600)
283 #define nssv_CPP11_110 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1700)
284 #define nssv_CPP11_120 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1800)
285 #define nssv_CPP11_140 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1900)
286 #define nssv_CPP11_141 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1910)
287 
288 #define nssv_CPP14_000 (nssv_CPP14_OR_GREATER)
289 #define nssv_CPP17_000 (nssv_CPP17_OR_GREATER)
290 
291 // Presence of C++11 language features:
292 
293 #define nssv_HAVE_CONSTEXPR_11 nssv_CPP11_140
294 #define nssv_HAVE_EXPLICIT_CONVERSION nssv_CPP11_140
295 #define nssv_HAVE_INLINE_NAMESPACE nssv_CPP11_140
296 #define nssv_HAVE_IS_DEFAULT nssv_CPP11_140
297 #define nssv_HAVE_IS_DELETE nssv_CPP11_140
298 #define nssv_HAVE_NOEXCEPT nssv_CPP11_140
299 #define nssv_HAVE_NULLPTR nssv_CPP11_100
300 #define nssv_HAVE_REF_QUALIFIER nssv_CPP11_140
301 #define nssv_HAVE_UNICODE_LITERALS nssv_CPP11_140
302 #define nssv_HAVE_USER_DEFINED_LITERALS nssv_CPP11_140
303 #define nssv_HAVE_WCHAR16_T nssv_CPP11_100
304 #define nssv_HAVE_WCHAR32_T nssv_CPP11_100
305 
306 #if ! ( ( nssv_CPP11_OR_GREATER && nssv_COMPILER_CLANG_VERSION ) || nssv_BETWEEN( nssv_COMPILER_CLANG_VERSION, 300, 400 ) )
307 # define nssv_HAVE_STD_DEFINED_LITERALS nssv_CPP11_140
308 #else
309 # define nssv_HAVE_STD_DEFINED_LITERALS 0
310 #endif
311 
312 // Presence of C++14 language features:
313 
314 #define nssv_HAVE_CONSTEXPR_14 nssv_CPP14_000
315 
316 // Presence of C++17 language features:
317 
318 #define nssv_HAVE_NODISCARD nssv_CPP17_000
319 
320 // Presence of C++ library features:
321 
322 #define nssv_HAVE_STD_HASH nssv_CPP11_120
323 
324 // Presence of compiler intrinsics:
325 
326 // Providing char-type specializations for compare() and length() that
327 // use compiler intrinsics can improve compile- and run-time performance.
328 //
329 // The challenge is in using the right combinations of builtin availability
330 // and its constexpr-ness.
331 //
332 // | compiler | __builtin_memcmp (constexpr) | memcmp (constexpr) |
333 // |----------|------------------------------|---------------------|
334 // | clang | 4.0 (>= 4.0 ) | any (? ) |
335 // | clang-a | 9.0 (>= 9.0 ) | any (? ) |
336 // | gcc | any (constexpr) | any (? ) |
337 // | msvc | >= 14.2 C++17 (>= 14.2 ) | any (? ) |
338 
339 #define nssv_HAVE_BUILTIN_VER ( (nssv_CPP17_000 && nssv_COMPILER_MSVC_VERSION >= 142) || nssv_COMPILER_GNUC_VERSION > 0 || nssv_COMPILER_CLANG_VERSION >= 400 || nssv_COMPILER_APPLECLANG_VERSION >= 900 )
340 #define nssv_HAVE_BUILTIN_CE ( nssv_HAVE_BUILTIN_VER )
341 
342 #define nssv_HAVE_BUILTIN_MEMCMP ( (nssv_HAVE_CONSTEXPR_14 && nssv_HAVE_BUILTIN_CE) || !nssv_HAVE_CONSTEXPR_14 )
343 #define nssv_HAVE_BUILTIN_STRLEN ( (nssv_HAVE_CONSTEXPR_11 && nssv_HAVE_BUILTIN_CE) || !nssv_HAVE_CONSTEXPR_11 )
344 
345 #ifdef __has_builtin
346 # define nssv_HAVE_BUILTIN( x ) __has_builtin( x )
347 #else
348 # define nssv_HAVE_BUILTIN( x ) 0
349 #endif
350 
351 #if nssv_HAVE_BUILTIN(__builtin_memcmp) || nssv_HAVE_BUILTIN_VER
352 # define nssv_BUILTIN_MEMCMP __builtin_memcmp
353 #else
354 # define nssv_BUILTIN_MEMCMP memcmp
355 #endif
356 
357 #if nssv_HAVE_BUILTIN(__builtin_strlen) || nssv_HAVE_BUILTIN_VER
358 # define nssv_BUILTIN_STRLEN __builtin_strlen
359 #else
360 # define nssv_BUILTIN_STRLEN strlen
361 #endif
362 
363 // C++ feature usage:
364 
365 #if nssv_HAVE_CONSTEXPR_11
366 # define nssv_constexpr constexpr
367 #else
368 # define nssv_constexpr /*constexpr*/
369 #endif
370 
371 #if nssv_HAVE_CONSTEXPR_14
372 # define nssv_constexpr14 constexpr
373 #else
374 # define nssv_constexpr14 /*constexpr*/
375 #endif
376 
377 #if nssv_HAVE_EXPLICIT_CONVERSION
378 # define nssv_explicit explicit
379 #else
380 # define nssv_explicit /*explicit*/
381 #endif
382 
383 #if nssv_HAVE_INLINE_NAMESPACE
384 # define nssv_inline_ns inline
385 #else
386 # define nssv_inline_ns /*inline*/
387 #endif
388 
389 #if nssv_HAVE_NOEXCEPT
390 # define nssv_noexcept noexcept
391 #else
392 # define nssv_noexcept /*noexcept*/
393 #endif
394 
395 //#if nssv_HAVE_REF_QUALIFIER
396 //# define nssv_ref_qual &
397 //# define nssv_refref_qual &&
398 //#else
399 //# define nssv_ref_qual /*&*/
400 //# define nssv_refref_qual /*&&*/
401 //#endif
402 
403 #if nssv_HAVE_NULLPTR
404 # define nssv_nullptr nullptr
405 #else
406 # define nssv_nullptr NULL
407 #endif
408 
409 #if nssv_HAVE_NODISCARD
410 # define nssv_nodiscard [[nodiscard]]
411 #else
412 # define nssv_nodiscard /*[[nodiscard]]*/
413 #endif
414 
415 // Additional includes:
416 
417 #include <algorithm>
418 #include <cassert>
419 #include <iterator>
420 #include <limits>
421 #include <string> // std::char_traits<>
422 
423 #if ! nssv_CONFIG_NO_STREAM_INSERTION
424 # include <ostream>
425 #endif
426 
427 #if ! nssv_CONFIG_NO_EXCEPTIONS
428 # include <stdexcept>
429 #endif
430 
431 #if nssv_CPP11_OR_GREATER
432 # include <type_traits>
433 #endif
434 
435 // Clang, GNUC, MSVC warning suppression macros:
436 
437 #if defined(__clang__)
438 # pragma clang diagnostic ignored "-Wreserved-user-defined-literal"
439 # pragma clang diagnostic push
440 # pragma clang diagnostic ignored "-Wuser-defined-literals"
441 #elif nssv_COMPILER_GNUC_VERSION >= 480
442 # pragma GCC diagnostic push
443 # pragma GCC diagnostic ignored "-Wliteral-suffix"
444 #endif // __clang__
445 
446 #if nssv_COMPILER_MSVC_VERSION >= 140
447 # define nssv_SUPPRESS_MSGSL_WARNING(expr) [[gsl::suppress(expr)]]
448 # define nssv_SUPPRESS_MSVC_WARNING(code, descr) __pragma(warning(suppress: code) )
449 # define nssv_DISABLE_MSVC_WARNINGS(codes) __pragma(warning(push)) __pragma(warning(disable: codes))
450 #else
451 # define nssv_SUPPRESS_MSGSL_WARNING(expr)
452 # define nssv_SUPPRESS_MSVC_WARNING(code, descr)
453 # define nssv_DISABLE_MSVC_WARNINGS(codes)
454 #endif
455 
456 #if defined(__clang__)
457 # define nssv_RESTORE_WARNINGS() _Pragma("clang diagnostic pop")
458 #elif nssv_COMPILER_GNUC_VERSION >= 480
459 # define nssv_RESTORE_WARNINGS() _Pragma("GCC diagnostic pop")
460 #elif nssv_COMPILER_MSVC_VERSION >= 140
461 # define nssv_RESTORE_WARNINGS() __pragma(warning(pop ))
462 #else
463 # define nssv_RESTORE_WARNINGS()
464 #endif
465 
466 // Suppress the following MSVC (GSL) warnings:
467 // - C4455, non-gsl : 'operator ""sv': literal suffix identifiers that do not
468 // start with an underscore are reserved
469 // - C26472, gsl::t.1 : don't use a static_cast for arithmetic conversions;
470 // use brace initialization, gsl::narrow_cast or gsl::narow
471 // - C26481: gsl::b.1 : don't use pointer arithmetic. Use span instead
472 
473 nssv_DISABLE_MSVC_WARNINGS( 4455 26481 26472 )
474 //nssv_DISABLE_CLANG_WARNINGS( "-Wuser-defined-literals" )
475 //nssv_DISABLE_GNUC_WARNINGS( -Wliteral-suffix )
476 
477 namespace nonstd { namespace sv_lite {
478 
479 //
480 // basic_string_view declaration:
481 //
482 
483 template
484 <
485  class CharT,
486  class Traits = std::char_traits<CharT>
487 >
488 class basic_string_view;
489 
490 namespace detail {
491 
492 // support constexpr comparison in C++14;
493 // for C++17 and later, use provided traits:
494 
495 template< typename CharT >
496 inline nssv_constexpr14 int compare( CharT const * s1, CharT const * s2, std::size_t count )
497 {
498  while ( count-- != 0 )
499  {
500  if ( *s1 < *s2 ) return -1;
501  if ( *s1 > *s2 ) return +1;
502  ++s1; ++s2;
503  }
504  return 0;
505 }
506 
507 #if nssv_HAVE_BUILTIN_MEMCMP
508 
509 // specialization of compare() for char, see also generic compare() above:
510 
511 inline nssv_constexpr14 int compare( char const * s1, char const * s2, std::size_t count )
512 {
513  return nssv_BUILTIN_MEMCMP( s1, s2, count );
514 }
515 
516 #endif
517 
518 #if nssv_HAVE_BUILTIN_STRLEN
519 
520 // specialization of length() for char, see also generic length() further below:
521 
522 inline nssv_constexpr std::size_t length( char const * s )
523 {
524  return nssv_BUILTIN_STRLEN( s );
525 }
526 
527 #endif
528 
529 #if defined(__OPTIMIZE__)
530 
531 // gcc, clang provide __OPTIMIZE__
532 // Expect tail call optimization to make length() non-recursive:
533 
534 template< typename CharT >
535 inline nssv_constexpr std::size_t length( CharT * s, std::size_t result = 0 )
536 {
537  return *s == '\0' ? result : length( s + 1, result + 1 );
538 }
539 
540 #else // OPTIMIZE
541 
542 // non-recursive:
543 
544 template< typename CharT >
545 inline nssv_constexpr14 std::size_t length( CharT * s )
546 {
547  std::size_t result = 0;
548  while ( *s++ != '\0' )
549  {
550  ++result;
551  }
552  return result;
553 }
554 
555 #endif // OPTIMIZE
556 
557 #if nssv_CPP11_OR_GREATER && ! nssv_CPP17_OR_GREATER
558 #if defined(__OPTIMIZE__)
559 
560 // gcc, clang provide __OPTIMIZE__
561 // Expect tail call optimization to make search() non-recursive:
562 
563 template< class CharT, class Traits = std::char_traits<CharT> >
564 constexpr const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
565 {
566  return haystack.starts_with( needle ) ? haystack.begin() :
567  haystack.empty() ? haystack.end() : search( haystack.substr(1), needle );
568 }
569 
570 #else // OPTIMIZE
571 
572 // non-recursive:
573 
574 #if nssv_CONFIG_CONSTEXPR11_STD_SEARCH
575 
576 template< class CharT, class Traits = std::char_traits<CharT> >
577 constexpr const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
578 {
579  return std::search( haystack.begin(), haystack.end(), needle.begin(), needle.end() );
580 }
581 
582 #else // nssv_CONFIG_CONSTEXPR11_STD_SEARCH
583 
584 template< class CharT, class Traits = std::char_traits<CharT> >
585 nssv_constexpr14 const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
586 {
587  while ( needle.size() <= haystack.size() )
588  {
589  if ( haystack.starts_with(needle) )
590  {
591  return haystack.cbegin();
592  }
593  haystack = basic_string_view<CharT, Traits>{ haystack.begin() + 1, haystack.size() - 1U };
594  }
595  return haystack.cend();
596 }
597 #endif // nssv_CONFIG_CONSTEXPR11_STD_SEARCH
598 
599 #endif // OPTIMIZE
600 #endif // nssv_CPP11_OR_GREATER && ! nssv_CPP17_OR_GREATER
601 
602 } // namespace detail
603 
604 //
605 // basic_string_view:
606 //
607 
608 template
609 <
610  class CharT,
611  class Traits /* = std::char_traits<CharT> */
612 >
613 class basic_string_view
614 {
615 public:
616  // Member types:
617 
618  typedef Traits traits_type;
619  typedef CharT value_type;
620 
621  typedef CharT * pointer;
622  typedef CharT const * const_pointer;
623  typedef CharT & reference;
624  typedef CharT const & const_reference;
625 
626  typedef const_pointer iterator;
627  typedef const_pointer const_iterator;
628  typedef std::reverse_iterator< const_iterator > reverse_iterator;
629  typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
630 
631  typedef std::size_t size_type;
632  typedef std::ptrdiff_t difference_type;
633 
634  // 24.4.2.1 Construction and assignment:
635 
636  nssv_constexpr basic_string_view() nssv_noexcept
637  : data_( nssv_nullptr )
638  , size_( 0 )
639  {}
640 
641 #if nssv_CPP11_OR_GREATER
642  nssv_constexpr basic_string_view( basic_string_view const & other ) nssv_noexcept = default;
643 #else
644  nssv_constexpr basic_string_view( basic_string_view const & other ) nssv_noexcept
645  : data_( other.data_)
646  , size_( other.size_)
647  {}
648 #endif
649 
650  nssv_constexpr basic_string_view( CharT const * s, size_type count ) nssv_noexcept // non-standard noexcept
651  : data_( s )
652  , size_( count )
653  {}
654 
655  nssv_constexpr basic_string_view( CharT const * s) nssv_noexcept // non-standard noexcept
656  : data_( s )
657 #if nssv_CPP17_OR_GREATER
658  , size_( Traits::length(s) )
659 #elif nssv_CPP11_OR_GREATER
660  , size_( detail::length(s) )
661 #else
662  , size_( Traits::length(s) )
663 #endif
664  {}
665 
666 #if nssv_HAVE_NULLPTR
667 # if nssv_HAVE_IS_DELETE
668  nssv_constexpr basic_string_view( std::nullptr_t ) nssv_noexcept = delete;
669 # else
670  private: nssv_constexpr basic_string_view( std::nullptr_t ) nssv_noexcept; public:
671 # endif
672 #endif
673 
674  // Assignment:
675 
676 #if nssv_CPP11_OR_GREATER
677  nssv_constexpr14 basic_string_view & operator=( basic_string_view const & other ) nssv_noexcept = default;
678 #else
679  nssv_constexpr14 basic_string_view & operator=( basic_string_view const & other ) nssv_noexcept
680  {
681  data_ = other.data_;
682  size_ = other.size_;
683  return *this;
684  }
685 #endif
686 
687  // 24.4.2.2 Iterator support:
688 
689  nssv_constexpr const_iterator begin() const nssv_noexcept { return data_; }
690  nssv_constexpr const_iterator end() const nssv_noexcept { return data_ + size_; }
691 
692  nssv_constexpr const_iterator cbegin() const nssv_noexcept { return begin(); }
693  nssv_constexpr const_iterator cend() const nssv_noexcept { return end(); }
694 
695  nssv_constexpr const_reverse_iterator rbegin() const nssv_noexcept { return const_reverse_iterator( end() ); }
696  nssv_constexpr const_reverse_iterator rend() const nssv_noexcept { return const_reverse_iterator( begin() ); }
697 
698  nssv_constexpr const_reverse_iterator crbegin() const nssv_noexcept { return rbegin(); }
699  nssv_constexpr const_reverse_iterator crend() const nssv_noexcept { return rend(); }
700 
701  // 24.4.2.3 Capacity:
702 
703  nssv_constexpr size_type size() const nssv_noexcept { return size_; }
704  nssv_constexpr size_type length() const nssv_noexcept { return size_; }
705  nssv_constexpr size_type max_size() const nssv_noexcept { return (std::numeric_limits< size_type >::max)(); }
706 
707  // since C++20
708  nssv_nodiscard nssv_constexpr bool empty() const nssv_noexcept
709  {
710  return 0 == size_;
711  }
712 
713  // 24.4.2.4 Element access:
714 
715  nssv_constexpr const_reference operator[]( size_type pos ) const
716  {
717  return data_at( pos );
718  }
719 
720  nssv_constexpr14 const_reference at( size_type pos ) const
721  {
722 #if nssv_CONFIG_NO_EXCEPTIONS
723  assert( pos < size() );
724 #else
725  if ( pos >= size() )
726  {
727  throw std::out_of_range("nonstd::string_view::at()");
728  }
729 #endif
730  return data_at( pos );
731  }
732 
733  nssv_constexpr const_reference front() const { return data_at( 0 ); }
734  nssv_constexpr const_reference back() const { return data_at( size() - 1 ); }
735 
736  nssv_constexpr const_pointer data() const nssv_noexcept { return data_; }
737 
738  // 24.4.2.5 Modifiers:
739 
740  nssv_constexpr14 void remove_prefix( size_type n )
741  {
742  assert( n <= size() );
743  data_ += n;
744  size_ -= n;
745  }
746 
747  nssv_constexpr14 void remove_suffix( size_type n )
748  {
749  assert( n <= size() );
750  size_ -= n;
751  }
752 
753  nssv_constexpr14 void swap( basic_string_view & other ) nssv_noexcept
754  {
755  const basic_string_view tmp(other);
756  other = *this;
757  *this = tmp;
758  }
759 
760  // 24.4.2.6 String operations:
761 
762  size_type copy( CharT * dest, size_type n, size_type pos = 0 ) const
763  {
764 #if nssv_CONFIG_NO_EXCEPTIONS
765  assert( pos <= size() );
766 #else
767  if ( pos > size() )
768  {
769  throw std::out_of_range("nonstd::string_view::copy()");
770  }
771 #endif
772  const size_type rlen = (std::min)( n, size() - pos );
773 
774  (void) Traits::copy( dest, data() + pos, rlen );
775 
776  return rlen;
777  }
778 
779  nssv_constexpr14 basic_string_view substr( size_type pos = 0, size_type n = npos ) const
780  {
781 #if nssv_CONFIG_NO_EXCEPTIONS
782  assert( pos <= size() );
783 #else
784  if ( pos > size() )
785  {
786  throw std::out_of_range("nonstd::string_view::substr()");
787  }
788 #endif
789  return basic_string_view( data() + pos, (std::min)( n, size() - pos ) );
790  }
791 
792  // compare(), 6x:
793 
794  nssv_constexpr14 int compare( basic_string_view other ) const nssv_noexcept // (1)
795  {
796 #if nssv_CPP17_OR_GREATER
797  if ( const int result = Traits::compare( data(), other.data(), (std::min)( size(), other.size() ) ) )
798 #else
799  if ( const int result = detail::compare( data(), other.data(), (std::min)( size(), other.size() ) ) )
800 #endif
801  {
802  return result;
803  }
804 
805  return size() == other.size() ? 0 : size() < other.size() ? -1 : 1;
806  }
807 
808  nssv_constexpr int compare( size_type pos1, size_type n1, basic_string_view other ) const // (2)
809  {
810  return substr( pos1, n1 ).compare( other );
811  }
812 
813  nssv_constexpr int compare( size_type pos1, size_type n1, basic_string_view other, size_type pos2, size_type n2 ) const // (3)
814  {
815  return substr( pos1, n1 ).compare( other.substr( pos2, n2 ) );
816  }
817 
818  nssv_constexpr int compare( CharT const * s ) const // (4)
819  {
820  return compare( basic_string_view( s ) );
821  }
822 
823  nssv_constexpr int compare( size_type pos1, size_type n1, CharT const * s ) const // (5)
824  {
825  return substr( pos1, n1 ).compare( basic_string_view( s ) );
826  }
827 
828  nssv_constexpr int compare( size_type pos1, size_type n1, CharT const * s, size_type n2 ) const // (6)
829  {
830  return substr( pos1, n1 ).compare( basic_string_view( s, n2 ) );
831  }
832 
833  // 24.4.2.7 Searching:
834 
835  // starts_with(), 3x, since C++20:
836 
837  nssv_constexpr bool starts_with( basic_string_view v ) const nssv_noexcept // (1)
838  {
839  return size() >= v.size() && compare( 0, v.size(), v ) == 0;
840  }
841 
842  nssv_constexpr bool starts_with( CharT c ) const nssv_noexcept // (2)
843  {
844  return starts_with( basic_string_view( &c, 1 ) );
845  }
846 
847  nssv_constexpr bool starts_with( CharT const * s ) const // (3)
848  {
849  return starts_with( basic_string_view( s ) );
850  }
851 
852  // ends_with(), 3x, since C++20:
853 
854  nssv_constexpr bool ends_with( basic_string_view v ) const nssv_noexcept // (1)
855  {
856  return size() >= v.size() && compare( size() - v.size(), npos, v ) == 0;
857  }
858 
859  nssv_constexpr bool ends_with( CharT c ) const nssv_noexcept // (2)
860  {
861  return ends_with( basic_string_view( &c, 1 ) );
862  }
863 
864  nssv_constexpr bool ends_with( CharT const * s ) const // (3)
865  {
866  return ends_with( basic_string_view( s ) );
867  }
868 
869  // find(), 4x:
870 
871  nssv_constexpr14 size_type find( basic_string_view v, size_type pos = 0 ) const nssv_noexcept // (1)
872  {
873  return assert( v.size() == 0 || v.data() != nssv_nullptr )
874  , pos >= size()
875  ? npos : to_pos(
876 #if nssv_CPP11_OR_GREATER && ! nssv_CPP17_OR_GREATER
877  detail::search( substr(pos), v )
878 #else
879  std::search( cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq )
880 #endif
881  );
882  }
883 
884  nssv_constexpr size_type find( CharT c, size_type pos = 0 ) const nssv_noexcept // (2)
885  {
886  return find( basic_string_view( &c, 1 ), pos );
887  }
888 
889  nssv_constexpr size_type find( CharT const * s, size_type pos, size_type n ) const // (3)
890  {
891  return find( basic_string_view( s, n ), pos );
892  }
893 
894  nssv_constexpr size_type find( CharT const * s, size_type pos = 0 ) const // (4)
895  {
896  return find( basic_string_view( s ), pos );
897  }
898 
899  // rfind(), 4x:
900 
901  nssv_constexpr14 size_type rfind( basic_string_view v, size_type pos = npos ) const nssv_noexcept // (1)
902  {
903  if ( size() < v.size() )
904  {
905  return npos;
906  }
907 
908  if ( v.empty() )
909  {
910  return (std::min)( size(), pos );
911  }
912 
913  const_iterator last = cbegin() + (std::min)( size() - v.size(), pos ) + v.size();
914  const_iterator result = std::find_end( cbegin(), last, v.cbegin(), v.cend(), Traits::eq );
915 
916  return result != last ? size_type( result - cbegin() ) : npos;
917  }
918 
919  nssv_constexpr14 size_type rfind( CharT c, size_type pos = npos ) const nssv_noexcept // (2)
920  {
921  return rfind( basic_string_view( &c, 1 ), pos );
922  }
923 
924  nssv_constexpr14 size_type rfind( CharT const * s, size_type pos, size_type n ) const // (3)
925  {
926  return rfind( basic_string_view( s, n ), pos );
927  }
928 
929  nssv_constexpr14 size_type rfind( CharT const * s, size_type pos = npos ) const // (4)
930  {
931  return rfind( basic_string_view( s ), pos );
932  }
933 
934  // find_first_of(), 4x:
935 
936  nssv_constexpr size_type find_first_of( basic_string_view v, size_type pos = 0 ) const nssv_noexcept // (1)
937  {
938  return pos >= size()
939  ? npos
940  : to_pos( std::find_first_of( cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq ) );
941  }
942 
943  nssv_constexpr size_type find_first_of( CharT c, size_type pos = 0 ) const nssv_noexcept // (2)
944  {
945  return find_first_of( basic_string_view( &c, 1 ), pos );
946  }
947 
948  nssv_constexpr size_type find_first_of( CharT const * s, size_type pos, size_type n ) const // (3)
949  {
950  return find_first_of( basic_string_view( s, n ), pos );
951  }
952 
953  nssv_constexpr size_type find_first_of( CharT const * s, size_type pos = 0 ) const // (4)
954  {
955  return find_first_of( basic_string_view( s ), pos );
956  }
957 
958  // find_last_of(), 4x:
959 
960  nssv_constexpr size_type find_last_of( basic_string_view v, size_type pos = npos ) const nssv_noexcept // (1)
961  {
962  return empty()
963  ? npos
964  : pos >= size()
965  ? find_last_of( v, size() - 1 )
966  : to_pos( std::find_first_of( const_reverse_iterator( cbegin() + pos + 1 ), crend(), v.cbegin(), v.cend(), Traits::eq ) );
967  }
968 
969  nssv_constexpr size_type find_last_of( CharT c, size_type pos = npos ) const nssv_noexcept // (2)
970  {
971  return find_last_of( basic_string_view( &c, 1 ), pos );
972  }
973 
974  nssv_constexpr size_type find_last_of( CharT const * s, size_type pos, size_type count ) const // (3)
975  {
976  return find_last_of( basic_string_view( s, count ), pos );
977  }
978 
979  nssv_constexpr size_type find_last_of( CharT const * s, size_type pos = npos ) const // (4)
980  {
981  return find_last_of( basic_string_view( s ), pos );
982  }
983 
984  // find_first_not_of(), 4x:
985 
986  nssv_constexpr size_type find_first_not_of( basic_string_view v, size_type pos = 0 ) const nssv_noexcept // (1)
987  {
988  return pos >= size()
989  ? npos
990  : to_pos( std::find_if( cbegin() + pos, cend(), not_in_view( v ) ) );
991  }
992 
993  nssv_constexpr size_type find_first_not_of( CharT c, size_type pos = 0 ) const nssv_noexcept // (2)
994  {
995  return find_first_not_of( basic_string_view( &c, 1 ), pos );
996  }
997 
998  nssv_constexpr size_type find_first_not_of( CharT const * s, size_type pos, size_type count ) const // (3)
999  {
1000  return find_first_not_of( basic_string_view( s, count ), pos );
1001  }
1002 
1003  nssv_constexpr size_type find_first_not_of( CharT const * s, size_type pos = 0 ) const // (4)
1004  {
1005  return find_first_not_of( basic_string_view( s ), pos );
1006  }
1007 
1008  // find_last_not_of(), 4x:
1009 
1010  nssv_constexpr size_type find_last_not_of( basic_string_view v, size_type pos = npos ) const nssv_noexcept // (1)
1011  {
1012  return empty()
1013  ? npos
1014  : pos >= size()
1015  ? find_last_not_of( v, size() - 1 )
1016  : to_pos( std::find_if( const_reverse_iterator( cbegin() + pos + 1 ), crend(), not_in_view( v ) ) );
1017  }
1018 
1019  nssv_constexpr size_type find_last_not_of( CharT c, size_type pos = npos ) const nssv_noexcept // (2)
1020  {
1021  return find_last_not_of( basic_string_view( &c, 1 ), pos );
1022  }
1023 
1024  nssv_constexpr size_type find_last_not_of( CharT const * s, size_type pos, size_type count ) const // (3)
1025  {
1026  return find_last_not_of( basic_string_view( s, count ), pos );
1027  }
1028 
1029  nssv_constexpr size_type find_last_not_of( CharT const * s, size_type pos = npos ) const // (4)
1030  {
1031  return find_last_not_of( basic_string_view( s ), pos );
1032  }
1033 
1034  // Constants:
1035 
1036 #if nssv_CPP17_OR_GREATER
1037  static nssv_constexpr size_type npos = size_type(-1);
1038 #elif nssv_CPP11_OR_GREATER
1039  enum : size_type { npos = size_type(-1) };
1040 #else
1041  enum { npos = size_type(-1) };
1042 #endif
1043 
1044 private:
1045  struct not_in_view
1046  {
1047  const basic_string_view v;
1048 
1049  nssv_constexpr explicit not_in_view( basic_string_view v_ ) : v( v_ ) {}
1050 
1051  nssv_constexpr bool operator()( CharT c ) const
1052  {
1053  return npos == v.find_first_of( c );
1054  }
1055  };
1056 
1057  nssv_constexpr size_type to_pos( const_iterator it ) const
1058  {
1059  return it == cend() ? npos : size_type( it - cbegin() );
1060  }
1061 
1062  nssv_constexpr size_type to_pos( const_reverse_iterator it ) const
1063  {
1064  return it == crend() ? npos : size_type( crend() - it - 1 );
1065  }
1066 
1067  nssv_constexpr const_reference data_at( size_type pos ) const
1068  {
1069 #if nssv_BETWEEN( nssv_COMPILER_GNUC_VERSION, 1, 500 )
1070  return data_[pos];
1071 #else
1072  return assert( pos < size() ), data_[pos];
1073 #endif
1074  }
1075 
1076 private:
1077  const_pointer data_;
1078  size_type size_;
1079 
1080 public:
1081 #if nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS
1082 
1083  template< class Allocator >
1084  basic_string_view( std::basic_string<CharT, Traits, Allocator> const & s ) nssv_noexcept
1085  : data_( s.data() )
1086  , size_( s.size() )
1087  {}
1088 
1089 #if nssv_HAVE_EXPLICIT_CONVERSION
1090 
1091  template< class Allocator >
1092  explicit operator std::basic_string<CharT, Traits, Allocator>() const
1093  {
1094  return to_string( Allocator() );
1095  }
1096 
1097 #endif // nssv_HAVE_EXPLICIT_CONVERSION
1098 
1099 #if nssv_CPP11_OR_GREATER
1100 
1101  template< class Allocator = std::allocator<CharT> >
1102  std::basic_string<CharT, Traits, Allocator>
1103  to_string( Allocator const & a = Allocator() ) const
1104  {
1105  return std::basic_string<CharT, Traits, Allocator>( begin(), end(), a );
1106  }
1107 
1108 #else
1109 
1110  std::basic_string<CharT, Traits>
1111  to_string() const
1112  {
1113  return std::basic_string<CharT, Traits>( begin(), end() );
1114  }
1115 
1116  template< class Allocator >
1117  std::basic_string<CharT, Traits, Allocator>
1118  to_string( Allocator const & a ) const
1119  {
1120  return std::basic_string<CharT, Traits, Allocator>( begin(), end(), a );
1121  }
1122 
1123 #endif // nssv_CPP11_OR_GREATER
1124 
1125 #endif // nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS
1126 };
1127 
1128 //
1129 // Non-member functions:
1130 //
1131 
1132 // 24.4.3 Non-member comparison functions:
1133 // lexicographically compare two string views (function template):
1134 
1135 template< class CharT, class Traits >
1136 nssv_constexpr bool operator== (
1137  basic_string_view <CharT, Traits> lhs,
1138  basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1139 { return lhs.size() == rhs.size() && lhs.compare( rhs ) == 0; }
1140 
1141 template< class CharT, class Traits >
1142 nssv_constexpr bool operator!= (
1143  basic_string_view <CharT, Traits> lhs,
1144  basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1145 { return !( lhs == rhs ); }
1146 
1147 template< class CharT, class Traits >
1148 nssv_constexpr bool operator< (
1149  basic_string_view <CharT, Traits> lhs,
1150  basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1151 { return lhs.compare( rhs ) < 0; }
1152 
1153 template< class CharT, class Traits >
1154 nssv_constexpr bool operator<= (
1155  basic_string_view <CharT, Traits> lhs,
1156  basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1157 { return lhs.compare( rhs ) <= 0; }
1158 
1159 template< class CharT, class Traits >
1160 nssv_constexpr bool operator> (
1161  basic_string_view <CharT, Traits> lhs,
1162  basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1163 { return lhs.compare( rhs ) > 0; }
1164 
1165 template< class CharT, class Traits >
1166 nssv_constexpr bool operator>= (
1167  basic_string_view <CharT, Traits> lhs,
1168  basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1169 { return lhs.compare( rhs ) >= 0; }
1170 
1171 // Let S be basic_string_view<CharT, Traits>, and sv be an instance of S.
1172 // Implementations shall provide sufficient additional overloads marked
1173 // constexpr and noexcept so that an object t with an implicit conversion
1174 // to S can be compared according to Table 67.
1175 
1176 #if ! nssv_CPP11_OR_GREATER || nssv_BETWEEN( nssv_COMPILER_MSVC_VERSION, 100, 141 )
1177 
1178 // accommodate for older compilers:
1179 
1180 // ==
1181 
1182 template< class CharT, class Traits>
1183 nssv_constexpr bool operator==(
1184  basic_string_view<CharT, Traits> lhs,
1185  CharT const * rhs ) nssv_noexcept
1186 { return lhs.size() == detail::length( rhs ) && lhs.compare( rhs ) == 0; }
1187 
1188 template< class CharT, class Traits>
1189 nssv_constexpr bool operator==(
1190  CharT const * lhs,
1191  basic_string_view<CharT, Traits> rhs ) nssv_noexcept
1192 { return detail::length( lhs ) == rhs.size() && rhs.compare( lhs ) == 0; }
1193 
1194 template< class CharT, class Traits>
1195 nssv_constexpr bool operator==(
1196  basic_string_view<CharT, Traits> lhs,
1197  std::basic_string<CharT, Traits> rhs ) nssv_noexcept
1198 { return lhs.size() == rhs.size() && lhs.compare( rhs ) == 0; }
1199 
1200 template< class CharT, class Traits>
1201 nssv_constexpr bool operator==(
1202  std::basic_string<CharT, Traits> rhs,
1203  basic_string_view<CharT, Traits> lhs ) nssv_noexcept
1204 { return lhs.size() == rhs.size() && lhs.compare( rhs ) == 0; }
1205 
1206 // !=
1207 
1208 template< class CharT, class Traits>
1209 nssv_constexpr bool operator!=(
1210  basic_string_view<CharT, Traits> lhs,
1211  CharT const * rhs ) nssv_noexcept
1212 { return !( lhs == rhs ); }
1213 
1214 template< class CharT, class Traits>
1215 nssv_constexpr bool operator!=(
1216  CharT const * lhs,
1217  basic_string_view<CharT, Traits> rhs ) nssv_noexcept
1218 { return !( lhs == rhs ); }
1219 
1220 template< class CharT, class Traits>
1221 nssv_constexpr bool operator!=(
1222  basic_string_view<CharT, Traits> lhs,
1223  std::basic_string<CharT, Traits> rhs ) nssv_noexcept
1224 { return !( lhs == rhs ); }
1225 
1226 template< class CharT, class Traits>
1227 nssv_constexpr bool operator!=(
1228  std::basic_string<CharT, Traits> rhs,
1229  basic_string_view<CharT, Traits> lhs ) nssv_noexcept
1230 { return !( lhs == rhs ); }
1231 
1232 // <
1233 
1234 template< class CharT, class Traits>
1235 nssv_constexpr bool operator<(
1236  basic_string_view<CharT, Traits> lhs,
1237  CharT const * rhs ) nssv_noexcept
1238 { return lhs.compare( rhs ) < 0; }
1239 
1240 template< class CharT, class Traits>
1241 nssv_constexpr bool operator<(
1242  CharT const * lhs,
1243  basic_string_view<CharT, Traits> rhs ) nssv_noexcept
1244 { return rhs.compare( lhs ) > 0; }
1245 
1246 template< class CharT, class Traits>
1247 nssv_constexpr bool operator<(
1248  basic_string_view<CharT, Traits> lhs,
1249  std::basic_string<CharT, Traits> rhs ) nssv_noexcept
1250 { return lhs.compare( rhs ) < 0; }
1251 
1252 template< class CharT, class Traits>
1253 nssv_constexpr bool operator<(
1254  std::basic_string<CharT, Traits> rhs,
1255  basic_string_view<CharT, Traits> lhs ) nssv_noexcept
1256 { return rhs.compare( lhs ) > 0; }
1257 
1258 // <=
1259 
1260 template< class CharT, class Traits>
1261 nssv_constexpr bool operator<=(
1262  basic_string_view<CharT, Traits> lhs,
1263  CharT const * rhs ) nssv_noexcept
1264 { return lhs.compare( rhs ) <= 0; }
1265 
1266 template< class CharT, class Traits>
1267 nssv_constexpr bool operator<=(
1268  CharT const * lhs,
1269  basic_string_view<CharT, Traits> rhs ) nssv_noexcept
1270 { return rhs.compare( lhs ) >= 0; }
1271 
1272 template< class CharT, class Traits>
1273 nssv_constexpr bool operator<=(
1274  basic_string_view<CharT, Traits> lhs,
1275  std::basic_string<CharT, Traits> rhs ) nssv_noexcept
1276 { return lhs.compare( rhs ) <= 0; }
1277 
1278 template< class CharT, class Traits>
1279 nssv_constexpr bool operator<=(
1280  std::basic_string<CharT, Traits> rhs,
1281  basic_string_view<CharT, Traits> lhs ) nssv_noexcept
1282 { return rhs.compare( lhs ) >= 0; }
1283 
1284 // >
1285 
1286 template< class CharT, class Traits>
1287 nssv_constexpr bool operator>(
1288  basic_string_view<CharT, Traits> lhs,
1289  CharT const * rhs ) nssv_noexcept
1290 { return lhs.compare( rhs ) > 0; }
1291 
1292 template< class CharT, class Traits>
1293 nssv_constexpr bool operator>(
1294  CharT const * lhs,
1295  basic_string_view<CharT, Traits> rhs ) nssv_noexcept
1296 { return rhs.compare( lhs ) < 0; }
1297 
1298 template< class CharT, class Traits>
1299 nssv_constexpr bool operator>(
1300  basic_string_view<CharT, Traits> lhs,
1301  std::basic_string<CharT, Traits> rhs ) nssv_noexcept
1302 { return lhs.compare( rhs ) > 0; }
1303 
1304 template< class CharT, class Traits>
1305 nssv_constexpr bool operator>(
1306  std::basic_string<CharT, Traits> rhs,
1307  basic_string_view<CharT, Traits> lhs ) nssv_noexcept
1308 { return rhs.compare( lhs ) < 0; }
1309 
1310 // >=
1311 
1312 template< class CharT, class Traits>
1313 nssv_constexpr bool operator>=(
1314  basic_string_view<CharT, Traits> lhs,
1315  CharT const * rhs ) nssv_noexcept
1316 { return lhs.compare( rhs ) >= 0; }
1317 
1318 template< class CharT, class Traits>
1319 nssv_constexpr bool operator>=(
1320  CharT const * lhs,
1321  basic_string_view<CharT, Traits> rhs ) nssv_noexcept
1322 { return rhs.compare( lhs ) <= 0; }
1323 
1324 template< class CharT, class Traits>
1325 nssv_constexpr bool operator>=(
1326  basic_string_view<CharT, Traits> lhs,
1327  std::basic_string<CharT, Traits> rhs ) nssv_noexcept
1328 { return lhs.compare( rhs ) >= 0; }
1329 
1330 template< class CharT, class Traits>
1331 nssv_constexpr bool operator>=(
1332  std::basic_string<CharT, Traits> rhs,
1333  basic_string_view<CharT, Traits> lhs ) nssv_noexcept
1334 { return rhs.compare( lhs ) <= 0; }
1335 
1336 #else // newer compilers:
1337 
1338 #define nssv_BASIC_STRING_VIEW_I(T,U) typename std::decay< basic_string_view<T,U> >::type
1339 
1340 #if defined(_MSC_VER) // issue 40
1341 # define nssv_MSVC_ORDER(x) , int=x
1342 #else
1343 # define nssv_MSVC_ORDER(x) /*, int=x*/
1344 #endif
1345 
1346 // ==
1347 
1348 template< class CharT, class Traits nssv_MSVC_ORDER(1) >
1349 nssv_constexpr bool operator==(
1350  basic_string_view <CharT, Traits> lhs,
1351  nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs ) nssv_noexcept
1352 { return lhs.size() == rhs.size() && lhs.compare( rhs ) == 0; }
1353 
1354 template< class CharT, class Traits nssv_MSVC_ORDER(2) >
1355 nssv_constexpr bool operator==(
1356  nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs,
1357  basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1358 { return lhs.size() == rhs.size() && lhs.compare( rhs ) == 0; }
1359 
1360 // !=
1361 
1362 template< class CharT, class Traits nssv_MSVC_ORDER(1) >
1363 nssv_constexpr bool operator!= (
1364  basic_string_view < CharT, Traits > lhs,
1365  nssv_BASIC_STRING_VIEW_I( CharT, Traits ) rhs ) nssv_noexcept
1366 { return !( lhs == rhs ); }
1367 
1368 template< class CharT, class Traits nssv_MSVC_ORDER(2) >
1369 nssv_constexpr bool operator!= (
1370  nssv_BASIC_STRING_VIEW_I( CharT, Traits ) lhs,
1371  basic_string_view < CharT, Traits > rhs ) nssv_noexcept
1372 { return !( lhs == rhs ); }
1373 
1374 // <
1375 
1376 template< class CharT, class Traits nssv_MSVC_ORDER(1) >
1377 nssv_constexpr bool operator< (
1378  basic_string_view < CharT, Traits > lhs,
1379  nssv_BASIC_STRING_VIEW_I( CharT, Traits ) rhs ) nssv_noexcept
1380 { return lhs.compare( rhs ) < 0; }
1381 
1382 template< class CharT, class Traits nssv_MSVC_ORDER(2) >
1383 nssv_constexpr bool operator< (
1384  nssv_BASIC_STRING_VIEW_I( CharT, Traits ) lhs,
1385  basic_string_view < CharT, Traits > rhs ) nssv_noexcept
1386 { return lhs.compare( rhs ) < 0; }
1387 
1388 // <=
1389 
1390 template< class CharT, class Traits nssv_MSVC_ORDER(1) >
1391 nssv_constexpr bool operator<= (
1392  basic_string_view < CharT, Traits > lhs,
1393  nssv_BASIC_STRING_VIEW_I( CharT, Traits ) rhs ) nssv_noexcept
1394 { return lhs.compare( rhs ) <= 0; }
1395 
1396 template< class CharT, class Traits nssv_MSVC_ORDER(2) >
1397 nssv_constexpr bool operator<= (
1398  nssv_BASIC_STRING_VIEW_I( CharT, Traits ) lhs,
1399  basic_string_view < CharT, Traits > rhs ) nssv_noexcept
1400 { return lhs.compare( rhs ) <= 0; }
1401 
1402 // >
1403 
1404 template< class CharT, class Traits nssv_MSVC_ORDER(1) >
1405 nssv_constexpr bool operator> (
1406  basic_string_view < CharT, Traits > lhs,
1407  nssv_BASIC_STRING_VIEW_I( CharT, Traits ) rhs ) nssv_noexcept
1408 { return lhs.compare( rhs ) > 0; }
1409 
1410 template< class CharT, class Traits nssv_MSVC_ORDER(2) >
1411 nssv_constexpr bool operator> (
1412  nssv_BASIC_STRING_VIEW_I( CharT, Traits ) lhs,
1413  basic_string_view < CharT, Traits > rhs ) nssv_noexcept
1414 { return lhs.compare( rhs ) > 0; }
1415 
1416 // >=
1417 
1418 template< class CharT, class Traits nssv_MSVC_ORDER(1) >
1419 nssv_constexpr bool operator>= (
1420  basic_string_view < CharT, Traits > lhs,
1421  nssv_BASIC_STRING_VIEW_I( CharT, Traits ) rhs ) nssv_noexcept
1422 { return lhs.compare( rhs ) >= 0; }
1423 
1424 template< class CharT, class Traits nssv_MSVC_ORDER(2) >
1425 nssv_constexpr bool operator>= (
1426  nssv_BASIC_STRING_VIEW_I( CharT, Traits ) lhs,
1427  basic_string_view < CharT, Traits > rhs ) nssv_noexcept
1428 { return lhs.compare( rhs ) >= 0; }
1429 
1430 #undef nssv_MSVC_ORDER
1431 #undef nssv_BASIC_STRING_VIEW_I
1432 
1433 #endif // compiler-dependent approach to comparisons
1434 
1435 // 24.4.4 Inserters and extractors:
1436 
1437 #if ! nssv_CONFIG_NO_STREAM_INSERTION
1438 
1439 namespace detail {
1440 
1441 template< class Stream >
1442 void write_padding( Stream & os, std::streamsize n )
1443 {
1444  for ( std::streamsize i = 0; i < n; ++i )
1445  os.rdbuf()->sputc( os.fill() );
1446 }
1447 
1448 template< class Stream, class View >
1449 Stream & write_to_stream( Stream & os, View const & sv )
1450 {
1451  typename Stream::sentry sentry( os );
1452 
1453  if ( !sentry )
1454  return os;
1455 
1456  const std::streamsize length = static_cast<std::streamsize>( sv.length() );
1457 
1458  // Whether, and how, to pad:
1459  const bool pad = ( length < os.width() );
1460  const bool left_pad = pad && ( os.flags() & std::ios_base::adjustfield ) == std::ios_base::right;
1461 
1462  if ( left_pad )
1463  write_padding( os, os.width() - length );
1464 
1465  // Write span characters:
1466  os.rdbuf()->sputn( sv.begin(), length );
1467 
1468  if ( pad && !left_pad )
1469  write_padding( os, os.width() - length );
1470 
1471  // Reset output stream width:
1472  os.width( 0 );
1473 
1474  return os;
1475 }
1476 
1477 } // namespace detail
1478 
1479 template< class CharT, class Traits >
1480 std::basic_ostream<CharT, Traits> &
1481 operator<<(
1482  std::basic_ostream<CharT, Traits>& os,
1483  basic_string_view <CharT, Traits> sv )
1484 {
1485  return detail::write_to_stream( os, sv );
1486 }
1487 
1488 #endif // nssv_CONFIG_NO_STREAM_INSERTION
1489 
1490 // Several typedefs for common character types are provided:
1491 
1492 typedef basic_string_view<char> string_view;
1493 typedef basic_string_view<wchar_t> wstring_view;
1494 #if nssv_HAVE_WCHAR16_T
1495 typedef basic_string_view<char16_t> u16string_view;
1496 typedef basic_string_view<char32_t> u32string_view;
1497 #endif
1498 
1499 }} // namespace nonstd::sv_lite
1500 
1501 //
1502 // 24.4.6 Suffix for basic_string_view literals:
1503 //
1504 
1505 #if nssv_HAVE_USER_DEFINED_LITERALS
1506 
1507 namespace nonstd {
1508 nssv_inline_ns namespace literals {
1509 nssv_inline_ns namespace string_view_literals {
1510 
1511 #if nssv_CONFIG_STD_SV_OPERATOR && nssv_HAVE_STD_DEFINED_LITERALS
1512 
1513 nssv_constexpr nonstd::sv_lite::string_view operator "" sv( const char* str, size_t len ) nssv_noexcept // (1)
1514 {
1515  return nonstd::sv_lite::string_view{ str, len };
1516 }
1517 
1518 nssv_constexpr nonstd::sv_lite::u16string_view operator "" sv( const char16_t* str, size_t len ) nssv_noexcept // (2)
1519 {
1520  return nonstd::sv_lite::u16string_view{ str, len };
1521 }
1522 
1523 nssv_constexpr nonstd::sv_lite::u32string_view operator "" sv( const char32_t* str, size_t len ) nssv_noexcept // (3)
1524 {
1525  return nonstd::sv_lite::u32string_view{ str, len };
1526 }
1527 
1528 nssv_constexpr nonstd::sv_lite::wstring_view operator "" sv( const wchar_t* str, size_t len ) nssv_noexcept // (4)
1529 {
1530  return nonstd::sv_lite::wstring_view{ str, len };
1531 }
1532 
1533 #endif // nssv_CONFIG_STD_SV_OPERATOR && nssv_HAVE_STD_DEFINED_LITERALS
1534 
1535 #if nssv_CONFIG_USR_SV_OPERATOR
1536 
1537 nssv_constexpr nonstd::sv_lite::string_view operator "" _sv( const char* str, size_t len ) nssv_noexcept // (1)
1538 {
1539  return nonstd::sv_lite::string_view{ str, len };
1540 }
1541 
1542 nssv_constexpr nonstd::sv_lite::u16string_view operator "" _sv( const char16_t* str, size_t len ) nssv_noexcept // (2)
1543 {
1544  return nonstd::sv_lite::u16string_view{ str, len };
1545 }
1546 
1547 nssv_constexpr nonstd::sv_lite::u32string_view operator "" _sv( const char32_t* str, size_t len ) nssv_noexcept // (3)
1548 {
1549  return nonstd::sv_lite::u32string_view{ str, len };
1550 }
1551 
1552 nssv_constexpr nonstd::sv_lite::wstring_view operator "" _sv( const wchar_t* str, size_t len ) nssv_noexcept // (4)
1553 {
1554  return nonstd::sv_lite::wstring_view{ str, len };
1555 }
1556 
1557 #endif // nssv_CONFIG_USR_SV_OPERATOR
1558 
1559 }}} // namespace nonstd::literals::string_view_literals
1560 
1561 #endif
1562 
1563 //
1564 // Extensions for std::string:
1565 //
1566 
1567 #if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
1568 
1569 namespace nonstd {
1570 namespace sv_lite {
1571 
1572 // Exclude MSVC 14 (19.00): it yields ambiguous to_string():
1573 
1574 #if nssv_CPP11_OR_GREATER && nssv_COMPILER_MSVC_VERSION != 140
1575 
1576 template< class CharT, class Traits, class Allocator = std::allocator<CharT> >
1577 std::basic_string<CharT, Traits, Allocator>
1578 to_string( basic_string_view<CharT, Traits> v, Allocator const & a = Allocator() )
1579 {
1580  return std::basic_string<CharT,Traits, Allocator>( v.begin(), v.end(), a );
1581 }
1582 
1583 #else
1584 
1585 template< class CharT, class Traits >
1586 std::basic_string<CharT, Traits>
1587 to_string( basic_string_view<CharT, Traits> v )
1588 {
1589  return std::basic_string<CharT, Traits>( v.begin(), v.end() );
1590 }
1591 
1592 template< class CharT, class Traits, class Allocator >
1593 std::basic_string<CharT, Traits, Allocator>
1594 to_string( basic_string_view<CharT, Traits> v, Allocator const & a )
1595 {
1596  return std::basic_string<CharT, Traits, Allocator>( v.begin(), v.end(), a );
1597 }
1598 
1599 #endif // nssv_CPP11_OR_GREATER
1600 
1601 template< class CharT, class Traits, class Allocator >
1602 basic_string_view<CharT, Traits>
1603 to_string_view( std::basic_string<CharT, Traits, Allocator> const & s )
1604 {
1605  return basic_string_view<CharT, Traits>( s.data(), s.size() );
1606 }
1607 
1608 }} // namespace nonstd::sv_lite
1609 
1610 #endif // nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
1611 
1612 //
1613 // make types and algorithms available in namespace nonstd:
1614 //
1615 
1616 namespace nonstd {
1617 
1618 using sv_lite::basic_string_view;
1619 using sv_lite::string_view;
1620 using sv_lite::wstring_view;
1621 
1622 #if nssv_HAVE_WCHAR16_T
1623 using sv_lite::u16string_view;
1624 #endif
1625 #if nssv_HAVE_WCHAR32_T
1626 using sv_lite::u32string_view;
1627 #endif
1628 
1629 // literal "sv"
1630 
1631 using sv_lite::operator==;
1632 using sv_lite::operator!=;
1633 using sv_lite::operator<;
1634 using sv_lite::operator<=;
1635 using sv_lite::operator>;
1636 using sv_lite::operator>=;
1637 
1638 #if ! nssv_CONFIG_NO_STREAM_INSERTION
1639 using sv_lite::operator<<;
1640 #endif
1641 
1642 #if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
1643 using sv_lite::to_string;
1644 using sv_lite::to_string_view;
1645 #endif
1646 
1647 } // namespace nonstd
1648 
1649 // 24.4.5 Hash support (C++11):
1650 
1651 // Note: The hash value of a string view object is equal to the hash value of
1652 // the corresponding string object.
1653 
1654 #if nssv_HAVE_STD_HASH
1655 
1656 #include <functional>
1657 
1658 namespace std {
1659 
1660 template<>
1661 struct hash< nonstd::string_view >
1662 {
1663 public:
1664  std::size_t operator()( nonstd::string_view v ) const nssv_noexcept
1665  {
1666  return std::hash<std::string>()( std::string( v.data(), v.size() ) );
1667  }
1668 };
1669 
1670 template<>
1671 struct hash< nonstd::wstring_view >
1672 {
1673 public:
1674  std::size_t operator()( nonstd::wstring_view v ) const nssv_noexcept
1675  {
1676  return std::hash<std::wstring>()( std::wstring( v.data(), v.size() ) );
1677  }
1678 };
1679 
1680 template<>
1681 struct hash< nonstd::u16string_view >
1682 {
1683 public:
1684  std::size_t operator()( nonstd::u16string_view v ) const nssv_noexcept
1685  {
1686  return std::hash<std::u16string>()( std::u16string( v.data(), v.size() ) );
1687  }
1688 };
1689 
1690 template<>
1691 struct hash< nonstd::u32string_view >
1692 {
1693 public:
1694  std::size_t operator()( nonstd::u32string_view v ) const nssv_noexcept
1695  {
1696  return std::hash<std::u32string>()( std::u32string( v.data(), v.size() ) );
1697  }
1698 };
1699 
1700 } // namespace std
1701 
1702 #endif // nssv_HAVE_STD_HASH
1703 
1704 nssv_RESTORE_WARNINGS()
1705 
1706 #endif // nssv_HAVE_STD_STRING_VIEW
1707 #endif // NONSTD_SV_LITE_H_INCLUDED
simdjson_unused simdjson_inline bool operator==(const raw_json_string &a, std::string_view c) noexcept
Comparisons between raw_json_string and std::string_view instances are potentially unsafe: the user i...
std::string to_string(T x)
Converts JSON to a string.