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