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