1 // Copyright 2017-2020 by Martin Moene
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
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)
11 #ifndef NONSTD_SV_LITE_H_INCLUDED
12 #define NONSTD_SV_LITE_H_INCLUDED
14 #define string_view_lite_MAJOR 1
15 #define string_view_lite_MINOR 7
16 #define string_view_lite_PATCH 0
18 #define string_view_lite_VERSION nssv_STRINGIFY(string_view_lite_MAJOR) "." nssv_STRINGIFY(string_view_lite_MINOR) "." nssv_STRINGIFY(string_view_lite_PATCH)
20 #define nssv_STRINGIFY( x ) nssv_STRINGIFY_( x )
21 #define nssv_STRINGIFY_( x ) #x
23 // string-view lite configuration:
25 #define nssv_STRING_VIEW_DEFAULT 0
26 #define nssv_STRING_VIEW_NONSTD 1
27 #define nssv_STRING_VIEW_STD 2
29 // tweak header support:
32 # if __has_include(<nonstd/string_view.tweak.hpp>)
33 # include <nonstd/string_view.tweak.hpp>
35 #define nssv_HAVE_TWEAK_HEADER 1
37 #define nssv_HAVE_TWEAK_HEADER 0
38 //# pragma message("string_view.hpp: Note: Tweak header not supported.")
41 // string_view selection and configuration:
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 )
47 #ifndef nssv_CONFIG_STD_SV_OPERATOR
48 # define nssv_CONFIG_STD_SV_OPERATOR 0
51 #ifndef nssv_CONFIG_USR_SV_OPERATOR
52 # define nssv_CONFIG_USR_SV_OPERATOR 1
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
60 #ifndef nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS
61 # define nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS 1
64 #ifndef nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
65 # define nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS 1
68 #ifndef nssv_CONFIG_NO_STREAM_INSERTION
69 # define nssv_CONFIG_NO_STREAM_INSERTION 0
72 #ifndef nssv_CONFIG_CONSTEXPR11_STD_SEARCH
73 # define nssv_CONFIG_CONSTEXPR11_STD_SEARCH 1
76 // Control presence of exception handling (try and auto discover):
78 #ifndef nssv_CONFIG_NO_EXCEPTIONS
79 # if defined(_MSC_VER)
80 # include <cstddef> // for _HAS_EXCEPTIONS
82 # if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (_HAS_EXCEPTIONS)
83 # define nssv_CONFIG_NO_EXCEPTIONS 0
85 # define nssv_CONFIG_NO_EXCEPTIONS 1
89 // C++ language version detection (C++23 is speculative):
90 // Note: VC14.0/1900 (VS2015) lacks too much from C++14.
92 #ifndef nssv_CPLUSPLUS
93 # if defined(_MSVC_LANG ) && !defined(__clang__)
94 # define nssv_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG )
96 # define nssv_CPLUSPLUS __cplusplus
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 )
108 // use C++17 std::string_view if available and requested:
110 #if nssv_CPP17_OR_GREATER && defined(__has_include )
111 # if __has_include( <string_view> )
112 # define nssv_HAVE_STD_STRING_VIEW 1
114 # define nssv_HAVE_STD_STRING_VIEW 0
117 # define nssv_HAVE_STD_STRING_VIEW 0
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) )
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
126 // Use C++17 std::string_view:
129 #if nssv_USES_STD_STRING_VIEW
131 #include <string_view>
133 // Extensions for std::string:
135 #if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
139 template< class CharT, class Traits, class Allocator = std::allocator<CharT> >
140 std::basic_string<CharT, Traits, Allocator>
141 to_string( std::basic_string_view<CharT, Traits> v, Allocator const & a = Allocator() )
143 return std::basic_string<CharT,Traits, Allocator>( v.begin(), v.end(), a );
146 template< class CharT, class Traits, class Allocator >
147 std::basic_string_view<CharT, Traits>
148 to_string_view( std::basic_string<CharT, Traits, Allocator> const & s )
150 return std::basic_string_view<CharT, Traits>( s.data(), s.size() );
153 // Literal operators sv and _sv:
155 #if nssv_CONFIG_STD_SV_OPERATOR
157 using namespace std::literals::string_view_literals;
161 #if nssv_CONFIG_USR_SV_OPERATOR
163 inline namespace literals {
164 inline namespace string_view_literals {
167 constexpr std::string_view operator "" _sv( const char* str, size_t len ) noexcept // (1)
169 return std::string_view{ str, len };
172 constexpr std::u16string_view operator "" _sv( const char16_t* str, size_t len ) noexcept // (2)
174 return std::u16string_view{ str, len };
177 constexpr std::u32string_view operator "" _sv( const char32_t* str, size_t len ) noexcept // (3)
179 return std::u32string_view{ str, len };
182 constexpr std::wstring_view operator "" _sv( const wchar_t* str, size_t len ) noexcept // (4)
184 return std::wstring_view{ str, len };
187 }} // namespace literals::string_view_literals
189 #endif // nssv_CONFIG_USR_SV_OPERATOR
191 } // namespace nonstd
193 #endif // nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
197 using std::string_view;
198 using std::wstring_view;
199 using std::u16string_view;
200 using std::u32string_view;
201 using std::basic_string_view;
203 // literal "sv" and "_sv", see above
205 using std::operator==;
206 using std::operator!=;
207 using std::operator<;
208 using std::operator<=;
209 using std::operator>;
210 using std::operator>=;
212 using std::operator<<;
214 } // namespace nonstd
216 #else // nssv_HAVE_STD_STRING_VIEW
219 // Before C++17: use string_view lite:
222 // Compiler versions:
224 // MSVC++ 6.0 _MSC_VER == 1200 nssv_COMPILER_MSVC_VERSION == 60 (Visual Studio 6.0)
225 // MSVC++ 7.0 _MSC_VER == 1300 nssv_COMPILER_MSVC_VERSION == 70 (Visual Studio .NET 2002)
226 // MSVC++ 7.1 _MSC_VER == 1310 nssv_COMPILER_MSVC_VERSION == 71 (Visual Studio .NET 2003)
227 // MSVC++ 8.0 _MSC_VER == 1400 nssv_COMPILER_MSVC_VERSION == 80 (Visual Studio 2005)
228 // MSVC++ 9.0 _MSC_VER == 1500 nssv_COMPILER_MSVC_VERSION == 90 (Visual Studio 2008)
229 // MSVC++ 10.0 _MSC_VER == 1600 nssv_COMPILER_MSVC_VERSION == 100 (Visual Studio 2010)
230 // MSVC++ 11.0 _MSC_VER == 1700 nssv_COMPILER_MSVC_VERSION == 110 (Visual Studio 2012)
231 // MSVC++ 12.0 _MSC_VER == 1800 nssv_COMPILER_MSVC_VERSION == 120 (Visual Studio 2013)
232 // MSVC++ 14.0 _MSC_VER == 1900 nssv_COMPILER_MSVC_VERSION == 140 (Visual Studio 2015)
233 // MSVC++ 14.1 _MSC_VER >= 1910 nssv_COMPILER_MSVC_VERSION == 141 (Visual Studio 2017)
234 // MSVC++ 14.2 _MSC_VER >= 1920 nssv_COMPILER_MSVC_VERSION == 142 (Visual Studio 2019)
236 #if defined(_MSC_VER ) && !defined(__clang__)
237 # define nssv_COMPILER_MSVC_VER (_MSC_VER )
238 # define nssv_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) )
240 # define nssv_COMPILER_MSVC_VER 0
241 # define nssv_COMPILER_MSVC_VERSION 0
244 #define nssv_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) )
246 #if defined( __apple_build_version__ )
247 # define nssv_COMPILER_APPLECLANG_VERSION nssv_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
248 # define nssv_COMPILER_CLANG_VERSION 0
249 #elif defined( __clang__ )
250 # define nssv_COMPILER_APPLECLANG_VERSION 0
251 # define nssv_COMPILER_CLANG_VERSION nssv_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
253 # define nssv_COMPILER_APPLECLANG_VERSION 0
254 # define nssv_COMPILER_CLANG_VERSION 0
257 #if defined(__GNUC__) && !defined(__clang__)
258 # define nssv_COMPILER_GNUC_VERSION nssv_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
260 # define nssv_COMPILER_GNUC_VERSION 0
263 // half-open range [lo..hi):
264 #define nssv_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) )
266 // Presence of language and library features:
269 # define nssv_HAS_CPP0X _HAS_CPP0X
271 # define nssv_HAS_CPP0X 0
274 // Unless defined otherwise below, consider VC14 as C++11 for string-view-lite:
276 #if nssv_COMPILER_MSVC_VER >= 1900
277 # undef nssv_CPP11_OR_GREATER
278 # define nssv_CPP11_OR_GREATER 1
281 #define nssv_CPP11_90 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1500)
282 #define nssv_CPP11_100 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1600)
283 #define nssv_CPP11_110 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1700)
284 #define nssv_CPP11_120 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1800)
285 #define nssv_CPP11_140 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1900)
286 #define nssv_CPP11_141 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1910)
288 #define nssv_CPP14_000 (nssv_CPP14_OR_GREATER)
289 #define nssv_CPP17_000 (nssv_CPP17_OR_GREATER)
291 // Presence of C++11 language features:
293 #define nssv_HAVE_CONSTEXPR_11 nssv_CPP11_140
294 #define nssv_HAVE_EXPLICIT_CONVERSION nssv_CPP11_140
295 #define nssv_HAVE_INLINE_NAMESPACE nssv_CPP11_140
296 #define nssv_HAVE_IS_DEFAULT nssv_CPP11_140
297 #define nssv_HAVE_IS_DELETE nssv_CPP11_140
298 #define nssv_HAVE_NOEXCEPT nssv_CPP11_140
299 #define nssv_HAVE_NULLPTR nssv_CPP11_100
300 #define nssv_HAVE_REF_QUALIFIER nssv_CPP11_140
301 #define nssv_HAVE_UNICODE_LITERALS nssv_CPP11_140
302 #define nssv_HAVE_USER_DEFINED_LITERALS nssv_CPP11_140
303 #define nssv_HAVE_WCHAR16_T nssv_CPP11_100
304 #define nssv_HAVE_WCHAR32_T nssv_CPP11_100
306 #if ! ( ( nssv_CPP11_OR_GREATER && nssv_COMPILER_CLANG_VERSION ) || nssv_BETWEEN( nssv_COMPILER_CLANG_VERSION, 300, 400 ) )
307 # define nssv_HAVE_STD_DEFINED_LITERALS nssv_CPP11_140
309 # define nssv_HAVE_STD_DEFINED_LITERALS 0
312 // Presence of C++14 language features:
314 #define nssv_HAVE_CONSTEXPR_14 nssv_CPP14_000
316 // Presence of C++17 language features:
318 #define nssv_HAVE_NODISCARD nssv_CPP17_000
320 // Presence of C++ library features:
322 #define nssv_HAVE_STD_HASH nssv_CPP11_120
324 // Presence of compiler intrinsics:
326 // Providing char-type specializations for compare() and length() that
327 // use compiler intrinsics can improve compile- and run-time performance.
329 // The challenge is in using the right combinations of builtin availability
330 // and its constexpr-ness.
332 // | compiler | __builtin_memcmp (constexpr) | memcmp (constexpr) |
333 // |----------|------------------------------|---------------------|
334 // | clang | 4.0 (>= 4.0 ) | any (? ) |
335 // | clang-a | 9.0 (>= 9.0 ) | any (? ) |
336 // | gcc | any (constexpr) | any (? ) |
337 // | msvc | >= 14.2 C++17 (>= 14.2 ) | any (? ) |
339 #define nssv_HAVE_BUILTIN_VER ( (nssv_CPP17_000 && nssv_COMPILER_MSVC_VERSION >= 142) || nssv_COMPILER_GNUC_VERSION > 0 || nssv_COMPILER_CLANG_VERSION >= 400 || nssv_COMPILER_APPLECLANG_VERSION >= 900 )
340 #define nssv_HAVE_BUILTIN_CE ( nssv_HAVE_BUILTIN_VER )
342 #define nssv_HAVE_BUILTIN_MEMCMP ( (nssv_HAVE_CONSTEXPR_14 && nssv_HAVE_BUILTIN_CE) || !nssv_HAVE_CONSTEXPR_14 )
343 #define nssv_HAVE_BUILTIN_STRLEN ( (nssv_HAVE_CONSTEXPR_11 && nssv_HAVE_BUILTIN_CE) || !nssv_HAVE_CONSTEXPR_11 )
346 # define nssv_HAVE_BUILTIN( x ) __has_builtin( x )
348 # define nssv_HAVE_BUILTIN( x ) 0
351 #if nssv_HAVE_BUILTIN(__builtin_memcmp) || nssv_HAVE_BUILTIN_VER
352 # define nssv_BUILTIN_MEMCMP __builtin_memcmp
354 # define nssv_BUILTIN_MEMCMP memcmp
357 #if nssv_HAVE_BUILTIN(__builtin_strlen) || nssv_HAVE_BUILTIN_VER
358 # define nssv_BUILTIN_STRLEN __builtin_strlen
360 # define nssv_BUILTIN_STRLEN strlen
363 // C++ feature usage:
365 #if nssv_HAVE_CONSTEXPR_11
366 # define nssv_constexpr constexpr
368 # define nssv_constexpr /*constexpr*/
371 #if nssv_HAVE_CONSTEXPR_14
372 # define nssv_constexpr14 constexpr
374 # define nssv_constexpr14 /*constexpr*/
377 #if nssv_HAVE_EXPLICIT_CONVERSION
378 # define nssv_explicit explicit
380 # define nssv_explicit /*explicit*/
383 #if nssv_HAVE_INLINE_NAMESPACE
384 # define nssv_inline_ns inline
386 # define nssv_inline_ns /*inline*/
389 #if nssv_HAVE_NOEXCEPT
390 # define nssv_noexcept noexcept
392 # define nssv_noexcept /*noexcept*/
395 //#if nssv_HAVE_REF_QUALIFIER
396 //# define nssv_ref_qual &
397 //# define nssv_refref_qual &&
399 //# define nssv_ref_qual /*&*/
400 //# define nssv_refref_qual /*&&*/
403 #if nssv_HAVE_NULLPTR
404 # define nssv_nullptr nullptr
406 # define nssv_nullptr NULL
409 #if nssv_HAVE_NODISCARD
410 # define nssv_nodiscard [[nodiscard]]
412 # define nssv_nodiscard /*[[nodiscard]]*/
415 // Additional includes:
421 #include <string> // std::char_traits<>
423 #if ! nssv_CONFIG_NO_STREAM_INSERTION
427 #if ! nssv_CONFIG_NO_EXCEPTIONS
428 # include <stdexcept>
431 #if nssv_CPP11_OR_GREATER
432 # include <type_traits>
435 // Clang, GNUC, MSVC warning suppression macros:
437 #if defined(__clang__)
438 # pragma clang diagnostic ignored "-Wreserved-user-defined-literal"
439 # pragma clang diagnostic push
440 # pragma clang diagnostic ignored "-Wuser-defined-literals"
441 #elif nssv_COMPILER_GNUC_VERSION >= 480
442 # pragma GCC diagnostic push
443 # pragma GCC diagnostic ignored "-Wliteral-suffix"
446 #if nssv_COMPILER_MSVC_VERSION >= 140
447 # define nssv_SUPPRESS_MSGSL_WARNING(expr) [[gsl::suppress(expr)]]
448 # define nssv_SUPPRESS_MSVC_WARNING(code, descr) __pragma(warning(suppress: code) )
449 # define nssv_DISABLE_MSVC_WARNINGS(codes) __pragma(warning(push)) __pragma(warning(disable: codes))
451 # define nssv_SUPPRESS_MSGSL_WARNING(expr)
452 # define nssv_SUPPRESS_MSVC_WARNING(code, descr)
453 # define nssv_DISABLE_MSVC_WARNINGS(codes)
456 #if defined(__clang__)
457 # define nssv_RESTORE_WARNINGS() _Pragma("clang diagnostic pop")
458 #elif nssv_COMPILER_GNUC_VERSION >= 480
459 # define nssv_RESTORE_WARNINGS() _Pragma("GCC diagnostic pop")
460 #elif nssv_COMPILER_MSVC_VERSION >= 140
461 # define nssv_RESTORE_WARNINGS() __pragma(warning(pop ))
463 # define nssv_RESTORE_WARNINGS()
466 // Suppress the following MSVC (GSL) warnings:
467 // - C4455, non-gsl : 'operator ""sv': literal suffix identifiers that do not
468 // start with an underscore are reserved
469 // - C26472, gsl::t.1 : don't use a static_cast for arithmetic conversions;
470 // use brace initialization, gsl::narrow_cast or gsl::narow
471 // - C26481: gsl::b.1 : don't use pointer arithmetic. Use span instead
473 nssv_DISABLE_MSVC_WARNINGS( 4455 26481 26472 )
474 //nssv_DISABLE_CLANG_WARNINGS( "-Wuser-defined-literals" )
475 //nssv_DISABLE_GNUC_WARNINGS( -Wliteral-suffix )
477 namespace nonstd { namespace sv_lite {
480 // basic_string_view declaration:
486 class Traits = std::char_traits<CharT>
488 class basic_string_view;
492 // support constexpr comparison in C++14;
493 // for C++17 and later, use provided traits:
495 template< typename CharT >
496 inline nssv_constexpr14 int compare( CharT const * s1, CharT const * s2, std::size_t count )
498 while ( count-- != 0 )
500 if ( *s1 < *s2 ) return -1;
501 if ( *s1 > *s2 ) return +1;
507 #if nssv_HAVE_BUILTIN_MEMCMP
509 // specialization of compare() for char, see also generic compare() above:
511 inline nssv_constexpr14 int compare( char const * s1, char const * s2, std::size_t count )
513 return nssv_BUILTIN_MEMCMP( s1, s2, count );
518 #if nssv_HAVE_BUILTIN_STRLEN
520 // specialization of length() for char, see also generic length() further below:
522 inline nssv_constexpr std::size_t length( char const * s )
524 return nssv_BUILTIN_STRLEN( s );
529 #if defined(__OPTIMIZE__)
531 // gcc, clang provide __OPTIMIZE__
532 // Expect tail call optimization to make length() non-recursive:
534 template< typename CharT >
535 inline nssv_constexpr std::size_t length( CharT * s, std::size_t result = 0 )
537 return *s == '\0' ? result : length( s + 1, result + 1 );
544 template< typename CharT >
545 inline nssv_constexpr14 std::size_t length( CharT * s )
547 std::size_t result = 0;
548 while ( *s++ != '\0' )
557 #if nssv_CPP11_OR_GREATER && ! nssv_CPP17_OR_GREATER
558 #if defined(__OPTIMIZE__)
560 // gcc, clang provide __OPTIMIZE__
561 // Expect tail call optimization to make search() non-recursive:
563 template< class CharT, class Traits = std::char_traits<CharT> >
564 constexpr const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
566 return haystack.starts_with( needle ) ? haystack.begin() :
567 haystack.empty() ? haystack.end() : search( haystack.substr(1), needle );
574 #if nssv_CONFIG_CONSTEXPR11_STD_SEARCH
576 template< class CharT, class Traits = std::char_traits<CharT> >
577 constexpr const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
579 return std::search( haystack.begin(), haystack.end(), needle.begin(), needle.end() );
582 #else // nssv_CONFIG_CONSTEXPR11_STD_SEARCH
584 template< class CharT, class Traits = std::char_traits<CharT> >
585 nssv_constexpr14 const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
587 while ( needle.size() <= haystack.size() )
589 if ( haystack.starts_with(needle) )
591 return haystack.cbegin();
593 haystack = basic_string_view<CharT, Traits>{ haystack.begin() + 1, haystack.size() - 1U };
595 return haystack.cend();
597 #endif // nssv_CONFIG_CONSTEXPR11_STD_SEARCH
600 #endif // nssv_CPP11_OR_GREATER && ! nssv_CPP17_OR_GREATER
602 } // namespace detail
605 // basic_string_view:
611 class Traits /* = std::char_traits<CharT> */
613 class basic_string_view
618 typedef Traits traits_type;
619 typedef CharT value_type;
621 typedef CharT * pointer;
622 typedef CharT const * const_pointer;
623 typedef CharT & reference;
624 typedef CharT const & const_reference;
626 typedef const_pointer iterator;
627 typedef const_pointer const_iterator;
628 typedef std::reverse_iterator< const_iterator > reverse_iterator;
629 typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
631 typedef std::size_t size_type;
632 typedef std::ptrdiff_t difference_type;
634 // 24.4.2.1 Construction and assignment:
636 nssv_constexpr basic_string_view() nssv_noexcept
637 : data_( nssv_nullptr )
641 #if nssv_CPP11_OR_GREATER
642 nssv_constexpr basic_string_view( basic_string_view const & other ) nssv_noexcept = default;
644 nssv_constexpr basic_string_view( basic_string_view const & other ) nssv_noexcept
645 : data_( other.data_)
646 , size_( other.size_)
650 nssv_constexpr basic_string_view( CharT const * s, size_type count ) nssv_noexcept // non-standard noexcept
655 nssv_constexpr basic_string_view( CharT const * s) nssv_noexcept // non-standard noexcept
657 #if nssv_CPP17_OR_GREATER
658 , size_( Traits::length(s) )
659 #elif nssv_CPP11_OR_GREATER
660 , size_( detail::length(s) )
662 , size_( Traits::length(s) )
666 #if nssv_HAVE_NULLPTR
667 # if nssv_HAVE_IS_DELETE
668 nssv_constexpr basic_string_view( std::nullptr_t ) nssv_noexcept = delete;
670 private: nssv_constexpr basic_string_view( std::nullptr_t ) nssv_noexcept; public:
676 #if nssv_CPP11_OR_GREATER
677 nssv_constexpr14 basic_string_view & operator=( basic_string_view const & other ) nssv_noexcept = default;
679 nssv_constexpr14 basic_string_view & operator=( basic_string_view const & other ) nssv_noexcept
687 // 24.4.2.2 Iterator support:
689 nssv_constexpr const_iterator begin() const nssv_noexcept { return data_; }
690 nssv_constexpr const_iterator end() const nssv_noexcept { return data_ + size_; }
692 nssv_constexpr const_iterator cbegin() const nssv_noexcept { return begin(); }
693 nssv_constexpr const_iterator cend() const nssv_noexcept { return end(); }
695 nssv_constexpr const_reverse_iterator rbegin() const nssv_noexcept { return const_reverse_iterator( end() ); }
696 nssv_constexpr const_reverse_iterator rend() const nssv_noexcept { return const_reverse_iterator( begin() ); }
698 nssv_constexpr const_reverse_iterator crbegin() const nssv_noexcept { return rbegin(); }
699 nssv_constexpr const_reverse_iterator crend() const nssv_noexcept { return rend(); }
701 // 24.4.2.3 Capacity:
703 nssv_constexpr size_type size() const nssv_noexcept { return size_; }
704 nssv_constexpr size_type length() const nssv_noexcept { return size_; }
705 nssv_constexpr size_type max_size() const nssv_noexcept { return (std::numeric_limits< size_type >::max)(); }
708 nssv_nodiscard nssv_constexpr bool empty() const nssv_noexcept
713 // 24.4.2.4 Element access:
715 nssv_constexpr const_reference operator[]( size_type pos ) const
717 return data_at( pos );
720 nssv_constexpr14 const_reference at( size_type pos ) const
722 #if nssv_CONFIG_NO_EXCEPTIONS
723 assert( pos < size() );
727 throw std::out_of_range("nonstd::string_view::at()");
730 return data_at( pos );
733 nssv_constexpr const_reference front() const { return data_at( 0 ); }
734 nssv_constexpr const_reference back() const { return data_at( size() - 1 ); }
736 nssv_constexpr const_pointer data() const nssv_noexcept { return data_; }
738 // 24.4.2.5 Modifiers:
740 nssv_constexpr14 void remove_prefix( size_type n )
742 assert( n <= size() );
747 nssv_constexpr14 void remove_suffix( size_type n )
749 assert( n <= size() );
753 nssv_constexpr14 void swap( basic_string_view & other ) nssv_noexcept
755 const basic_string_view tmp(other);
760 // 24.4.2.6 String operations:
762 size_type copy( CharT * dest, size_type n, size_type pos = 0 ) const
764 #if nssv_CONFIG_NO_EXCEPTIONS
765 assert( pos <= size() );
769 throw std::out_of_range("nonstd::string_view::copy()");
772 const size_type rlen = (std::min)( n, size() - pos );
774 (void) Traits::copy( dest, data() + pos, rlen );
779 nssv_constexpr14 basic_string_view substr( size_type pos = 0, size_type n = npos ) const
781 #if nssv_CONFIG_NO_EXCEPTIONS
782 assert( pos <= size() );
786 throw std::out_of_range("nonstd::string_view::substr()");
789 return basic_string_view( data() + pos, (std::min)( n, size() - pos ) );
794 nssv_constexpr14 int compare( basic_string_view other ) const nssv_noexcept // (1)
796 #if nssv_CPP17_OR_GREATER
797 if ( const int result = Traits::compare( data(), other.data(), (std::min)( size(), other.size() ) ) )
799 if ( const int result = detail::compare( data(), other.data(), (std::min)( size(), other.size() ) ) )
805 return size() == other.size() ? 0 : size() < other.size() ? -1 : 1;
808 nssv_constexpr int compare( size_type pos1, size_type n1, basic_string_view other ) const // (2)
810 return substr( pos1, n1 ).compare( other );
813 nssv_constexpr int compare( size_type pos1, size_type n1, basic_string_view other, size_type pos2, size_type n2 ) const // (3)
815 return substr( pos1, n1 ).compare( other.substr( pos2, n2 ) );
818 nssv_constexpr int compare( CharT const * s ) const // (4)
820 return compare( basic_string_view( s ) );
823 nssv_constexpr int compare( size_type pos1, size_type n1, CharT const * s ) const // (5)
825 return substr( pos1, n1 ).compare( basic_string_view( s ) );
828 nssv_constexpr int compare( size_type pos1, size_type n1, CharT const * s, size_type n2 ) const // (6)
830 return substr( pos1, n1 ).compare( basic_string_view( s, n2 ) );
833 // 24.4.2.7 Searching:
835 // starts_with(), 3x, since C++20:
837 nssv_constexpr bool starts_with( basic_string_view v ) const nssv_noexcept // (1)
839 return size() >= v.size() && compare( 0, v.size(), v ) == 0;
842 nssv_constexpr bool starts_with( CharT c ) const nssv_noexcept // (2)
844 return starts_with( basic_string_view( &c, 1 ) );
847 nssv_constexpr bool starts_with( CharT const * s ) const // (3)
849 return starts_with( basic_string_view( s ) );
852 // ends_with(), 3x, since C++20:
854 nssv_constexpr bool ends_with( basic_string_view v ) const nssv_noexcept // (1)
856 return size() >= v.size() && compare( size() - v.size(), npos, v ) == 0;
859 nssv_constexpr bool ends_with( CharT c ) const nssv_noexcept // (2)
861 return ends_with( basic_string_view( &c, 1 ) );
864 nssv_constexpr bool ends_with( CharT const * s ) const // (3)
866 return ends_with( basic_string_view( s ) );
871 nssv_constexpr14 size_type find( basic_string_view v, size_type pos = 0 ) const nssv_noexcept // (1)
873 return assert( v.size() == 0 || v.data() != nssv_nullptr )
876 #if nssv_CPP11_OR_GREATER && ! nssv_CPP17_OR_GREATER
877 detail::search( substr(pos), v )
879 std::search( cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq )
884 nssv_constexpr size_type find( CharT c, size_type pos = 0 ) const nssv_noexcept // (2)
886 return find( basic_string_view( &c, 1 ), pos );
889 nssv_constexpr size_type find( CharT const * s, size_type pos, size_type n ) const // (3)
891 return find( basic_string_view( s, n ), pos );
894 nssv_constexpr size_type find( CharT const * s, size_type pos = 0 ) const // (4)
896 return find( basic_string_view( s ), pos );
901 nssv_constexpr14 size_type rfind( basic_string_view v, size_type pos = npos ) const nssv_noexcept // (1)
903 if ( size() < v.size() )
910 return (std::min)( size(), pos );
913 const_iterator last = cbegin() + (std::min)( size() - v.size(), pos ) + v.size();
914 const_iterator result = std::find_end( cbegin(), last, v.cbegin(), v.cend(), Traits::eq );
916 return result != last ? size_type( result - cbegin() ) : npos;
919 nssv_constexpr14 size_type rfind( CharT c, size_type pos = npos ) const nssv_noexcept // (2)
921 return rfind( basic_string_view( &c, 1 ), pos );
924 nssv_constexpr14 size_type rfind( CharT const * s, size_type pos, size_type n ) const // (3)
926 return rfind( basic_string_view( s, n ), pos );
929 nssv_constexpr14 size_type rfind( CharT const * s, size_type pos = npos ) const // (4)
931 return rfind( basic_string_view( s ), pos );
934 // find_first_of(), 4x:
936 nssv_constexpr size_type find_first_of( basic_string_view v, size_type pos = 0 ) const nssv_noexcept // (1)
940 : to_pos( std::find_first_of( cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq ) );
943 nssv_constexpr size_type find_first_of( CharT c, size_type pos = 0 ) const nssv_noexcept // (2)
945 return find_first_of( basic_string_view( &c, 1 ), pos );
948 nssv_constexpr size_type find_first_of( CharT const * s, size_type pos, size_type n ) const // (3)
950 return find_first_of( basic_string_view( s, n ), pos );
953 nssv_constexpr size_type find_first_of( CharT const * s, size_type pos = 0 ) const // (4)
955 return find_first_of( basic_string_view( s ), pos );
958 // find_last_of(), 4x:
960 nssv_constexpr size_type find_last_of( basic_string_view v, size_type pos = npos ) const nssv_noexcept // (1)
965 ? find_last_of( v, size() - 1 )
966 : to_pos( std::find_first_of( const_reverse_iterator( cbegin() + pos + 1 ), crend(), v.cbegin(), v.cend(), Traits::eq ) );
969 nssv_constexpr size_type find_last_of( CharT c, size_type pos = npos ) const nssv_noexcept // (2)
971 return find_last_of( basic_string_view( &c, 1 ), pos );
974 nssv_constexpr size_type find_last_of( CharT const * s, size_type pos, size_type count ) const // (3)
976 return find_last_of( basic_string_view( s, count ), pos );
979 nssv_constexpr size_type find_last_of( CharT const * s, size_type pos = npos ) const // (4)
981 return find_last_of( basic_string_view( s ), pos );
984 // find_first_not_of(), 4x:
986 nssv_constexpr size_type find_first_not_of( basic_string_view v, size_type pos = 0 ) const nssv_noexcept // (1)
990 : to_pos( std::find_if( cbegin() + pos, cend(), not_in_view( v ) ) );
993 nssv_constexpr size_type find_first_not_of( CharT c, size_type pos = 0 ) const nssv_noexcept // (2)
995 return find_first_not_of( basic_string_view( &c, 1 ), pos );
998 nssv_constexpr size_type find_first_not_of( CharT const * s, size_type pos, size_type count ) const // (3)
1000 return find_first_not_of( basic_string_view( s, count ), pos );
1003 nssv_constexpr size_type find_first_not_of( CharT const * s, size_type pos = 0 ) const // (4)
1005 return find_first_not_of( basic_string_view( s ), pos );
1008 // find_last_not_of(), 4x:
1010 nssv_constexpr size_type find_last_not_of( basic_string_view v, size_type pos = npos ) const nssv_noexcept // (1)
1015 ? find_last_not_of( v, size() - 1 )
1016 : to_pos( std::find_if( const_reverse_iterator( cbegin() + pos + 1 ), crend(), not_in_view( v ) ) );
1019 nssv_constexpr size_type find_last_not_of( CharT c, size_type pos = npos ) const nssv_noexcept // (2)
1021 return find_last_not_of( basic_string_view( &c, 1 ), pos );
1024 nssv_constexpr size_type find_last_not_of( CharT const * s, size_type pos, size_type count ) const // (3)
1026 return find_last_not_of( basic_string_view( s, count ), pos );
1029 nssv_constexpr size_type find_last_not_of( CharT const * s, size_type pos = npos ) const // (4)
1031 return find_last_not_of( basic_string_view( s ), pos );
1036 #if nssv_CPP17_OR_GREATER
1037 static nssv_constexpr size_type npos = size_type(-1);
1038 #elif nssv_CPP11_OR_GREATER
1039 enum : size_type { npos = size_type(-1) };
1041 enum { npos = size_type(-1) };
1047 const basic_string_view v;
1049 nssv_constexpr explicit not_in_view( basic_string_view v_ ) : v( v_ ) {}
1051 nssv_constexpr bool operator()( CharT c ) const
1053 return npos == v.find_first_of( c );
1057 nssv_constexpr size_type to_pos( const_iterator it ) const
1059 return it == cend() ? npos : size_type( it - cbegin() );
1062 nssv_constexpr size_type to_pos( const_reverse_iterator it ) const
1064 return it == crend() ? npos : size_type( crend() - it - 1 );
1067 nssv_constexpr const_reference data_at( size_type pos ) const
1069 #if nssv_BETWEEN( nssv_COMPILER_GNUC_VERSION, 1, 500 )
1072 return assert( pos < size() ), data_[pos];
1077 const_pointer data_;
1081 #if nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS
1083 template< class Allocator >
1084 basic_string_view( std::basic_string<CharT, Traits, Allocator> const & s ) nssv_noexcept
1089 #if nssv_HAVE_EXPLICIT_CONVERSION
1091 template< class Allocator >
1092 explicit operator std::basic_string<CharT, Traits, Allocator>() const
1094 return to_string( Allocator() );
1097 #endif // nssv_HAVE_EXPLICIT_CONVERSION
1099 #if nssv_CPP11_OR_GREATER
1101 template< class Allocator = std::allocator<CharT> >
1102 std::basic_string<CharT, Traits, Allocator>
1103 to_string( Allocator const & a = Allocator() ) const
1105 return std::basic_string<CharT, Traits, Allocator>( begin(), end(), a );
1110 std::basic_string<CharT, Traits>
1113 return std::basic_string<CharT, Traits>( begin(), end() );
1116 template< class Allocator >
1117 std::basic_string<CharT, Traits, Allocator>
1118 to_string( Allocator const & a ) const
1120 return std::basic_string<CharT, Traits, Allocator>( begin(), end(), a );
1123 #endif // nssv_CPP11_OR_GREATER
1125 #endif // nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS
1129 // Non-member functions:
1132 // 24.4.3 Non-member comparison functions:
1133 // lexicographically compare two string views (function template):
1135 template< class CharT, class Traits >
1136 nssv_constexpr bool operator== (
1137 basic_string_view <CharT, Traits> lhs,
1138 basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1139 { return lhs.size() == rhs.size() && lhs.compare( rhs ) == 0; }
1141 template< class CharT, class Traits >
1142 nssv_constexpr bool operator!= (
1143 basic_string_view <CharT, Traits> lhs,
1144 basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1145 { return !( lhs == rhs ); }
1147 template< class CharT, class Traits >
1148 nssv_constexpr bool operator< (
1149 basic_string_view <CharT, Traits> lhs,
1150 basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1151 { return lhs.compare( rhs ) < 0; }
1153 template< class CharT, class Traits >
1154 nssv_constexpr bool operator<= (
1155 basic_string_view <CharT, Traits> lhs,
1156 basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1157 { return lhs.compare( rhs ) <= 0; }
1159 template< class CharT, class Traits >
1160 nssv_constexpr bool operator> (
1161 basic_string_view <CharT, Traits> lhs,
1162 basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1163 { return lhs.compare( rhs ) > 0; }
1165 template< class CharT, class Traits >
1166 nssv_constexpr bool operator>= (
1167 basic_string_view <CharT, Traits> lhs,
1168 basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1169 { return lhs.compare( rhs ) >= 0; }
1171 // Let S be basic_string_view<CharT, Traits>, and sv be an instance of S.
1172 // Implementations shall provide sufficient additional overloads marked
1173 // constexpr and noexcept so that an object t with an implicit conversion
1174 // to S can be compared according to Table 67.
1176 #if ! nssv_CPP11_OR_GREATER || nssv_BETWEEN( nssv_COMPILER_MSVC_VERSION, 100, 141 )
1178 // accommodate for older compilers:
1182 template< class CharT, class Traits>
1183 nssv_constexpr bool operator==(
1184 basic_string_view<CharT, Traits> lhs,
1185 CharT const * rhs ) nssv_noexcept
1186 { return lhs.size() == detail::length( rhs ) && lhs.compare( rhs ) == 0; }
1188 template< class CharT, class Traits>
1189 nssv_constexpr bool operator==(
1191 basic_string_view<CharT, Traits> rhs ) nssv_noexcept
1192 { return detail::length( lhs ) == rhs.size() && rhs.compare( lhs ) == 0; }
1194 template< class CharT, class Traits>
1195 nssv_constexpr bool operator==(
1196 basic_string_view<CharT, Traits> lhs,
1197 std::basic_string<CharT, Traits> rhs ) nssv_noexcept
1198 { return lhs.size() == rhs.size() && lhs.compare( rhs ) == 0; }
1200 template< class CharT, class Traits>
1201 nssv_constexpr bool operator==(
1202 std::basic_string<CharT, Traits> rhs,
1203 basic_string_view<CharT, Traits> lhs ) nssv_noexcept
1204 { return lhs.size() == rhs.size() && lhs.compare( rhs ) == 0; }
1208 template< class CharT, class Traits>
1209 nssv_constexpr bool operator!=(
1210 basic_string_view<CharT, Traits> lhs,
1211 CharT const * rhs ) nssv_noexcept
1212 { return !( lhs == rhs ); }
1214 template< class CharT, class Traits>
1215 nssv_constexpr bool operator!=(
1217 basic_string_view<CharT, Traits> rhs ) nssv_noexcept
1218 { return !( lhs == rhs ); }
1220 template< class CharT, class Traits>
1221 nssv_constexpr bool operator!=(
1222 basic_string_view<CharT, Traits> lhs,
1223 std::basic_string<CharT, Traits> rhs ) nssv_noexcept
1224 { return !( lhs == rhs ); }
1226 template< class CharT, class Traits>
1227 nssv_constexpr bool operator!=(
1228 std::basic_string<CharT, Traits> rhs,
1229 basic_string_view<CharT, Traits> lhs ) nssv_noexcept
1230 { return !( lhs == rhs ); }
1234 template< class CharT, class Traits>
1235 nssv_constexpr bool operator<(
1236 basic_string_view<CharT, Traits> lhs,
1237 CharT const * rhs ) nssv_noexcept
1238 { return lhs.compare( rhs ) < 0; }
1240 template< class CharT, class Traits>
1241 nssv_constexpr bool operator<(
1243 basic_string_view<CharT, Traits> rhs ) nssv_noexcept
1244 { return rhs.compare( lhs ) > 0; }
1246 template< class CharT, class Traits>
1247 nssv_constexpr bool operator<(
1248 basic_string_view<CharT, Traits> lhs,
1249 std::basic_string<CharT, Traits> rhs ) nssv_noexcept
1250 { return lhs.compare( rhs ) < 0; }
1252 template< class CharT, class Traits>
1253 nssv_constexpr bool operator<(
1254 std::basic_string<CharT, Traits> rhs,
1255 basic_string_view<CharT, Traits> lhs ) nssv_noexcept
1256 { return rhs.compare( lhs ) > 0; }
1260 template< class CharT, class Traits>
1261 nssv_constexpr bool operator<=(
1262 basic_string_view<CharT, Traits> lhs,
1263 CharT const * rhs ) nssv_noexcept
1264 { return lhs.compare( rhs ) <= 0; }
1266 template< class CharT, class Traits>
1267 nssv_constexpr bool operator<=(
1269 basic_string_view<CharT, Traits> rhs ) nssv_noexcept
1270 { return rhs.compare( lhs ) >= 0; }
1272 template< class CharT, class Traits>
1273 nssv_constexpr bool operator<=(
1274 basic_string_view<CharT, Traits> lhs,
1275 std::basic_string<CharT, Traits> rhs ) nssv_noexcept
1276 { return lhs.compare( rhs ) <= 0; }
1278 template< class CharT, class Traits>
1279 nssv_constexpr bool operator<=(
1280 std::basic_string<CharT, Traits> rhs,
1281 basic_string_view<CharT, Traits> lhs ) nssv_noexcept
1282 { return rhs.compare( lhs ) >= 0; }
1286 template< class CharT, class Traits>
1287 nssv_constexpr bool operator>(
1288 basic_string_view<CharT, Traits> lhs,
1289 CharT const * rhs ) nssv_noexcept
1290 { return lhs.compare( rhs ) > 0; }
1292 template< class CharT, class Traits>
1293 nssv_constexpr bool operator>(
1295 basic_string_view<CharT, Traits> rhs ) nssv_noexcept
1296 { return rhs.compare( lhs ) < 0; }
1298 template< class CharT, class Traits>
1299 nssv_constexpr bool operator>(
1300 basic_string_view<CharT, Traits> lhs,
1301 std::basic_string<CharT, Traits> rhs ) nssv_noexcept
1302 { return lhs.compare( rhs ) > 0; }
1304 template< class CharT, class Traits>
1305 nssv_constexpr bool operator>(
1306 std::basic_string<CharT, Traits> rhs,
1307 basic_string_view<CharT, Traits> lhs ) nssv_noexcept
1308 { return rhs.compare( lhs ) < 0; }
1312 template< class CharT, class Traits>
1313 nssv_constexpr bool operator>=(
1314 basic_string_view<CharT, Traits> lhs,
1315 CharT const * rhs ) nssv_noexcept
1316 { return lhs.compare( rhs ) >= 0; }
1318 template< class CharT, class Traits>
1319 nssv_constexpr bool operator>=(
1321 basic_string_view<CharT, Traits> rhs ) nssv_noexcept
1322 { return rhs.compare( lhs ) <= 0; }
1324 template< class CharT, class Traits>
1325 nssv_constexpr bool operator>=(
1326 basic_string_view<CharT, Traits> lhs,
1327 std::basic_string<CharT, Traits> rhs ) nssv_noexcept
1328 { return lhs.compare( rhs ) >= 0; }
1330 template< class CharT, class Traits>
1331 nssv_constexpr bool operator>=(
1332 std::basic_string<CharT, Traits> rhs,
1333 basic_string_view<CharT, Traits> lhs ) nssv_noexcept
1334 { return rhs.compare( lhs ) <= 0; }
1336 #else // newer compilers:
1338 #define nssv_BASIC_STRING_VIEW_I(T,U) typename std::decay< basic_string_view<T,U> >::type
1340 #if defined(_MSC_VER) // issue 40
1341 # define nssv_MSVC_ORDER(x) , int=x
1343 # define nssv_MSVC_ORDER(x) /*, int=x*/
1348 template< class CharT, class Traits nssv_MSVC_ORDER(1) >
1349 nssv_constexpr bool operator==(
1350 basic_string_view <CharT, Traits> lhs,
1351 nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs ) nssv_noexcept
1352 { return lhs.size() == rhs.size() && lhs.compare( rhs ) == 0; }
1354 template< class CharT, class Traits nssv_MSVC_ORDER(2) >
1355 nssv_constexpr bool operator==(
1356 nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs,
1357 basic_string_view <CharT, Traits> rhs ) nssv_noexcept
1358 { return lhs.size() == rhs.size() && lhs.compare( rhs ) == 0; }
1362 template< class CharT, class Traits nssv_MSVC_ORDER(1) >
1363 nssv_constexpr bool operator!= (
1364 basic_string_view < CharT, Traits > lhs,
1365 nssv_BASIC_STRING_VIEW_I( CharT, Traits ) rhs ) nssv_noexcept
1366 { return !( lhs == rhs ); }
1368 template< class CharT, class Traits nssv_MSVC_ORDER(2) >
1369 nssv_constexpr bool operator!= (
1370 nssv_BASIC_STRING_VIEW_I( CharT, Traits ) lhs,
1371 basic_string_view < CharT, Traits > rhs ) nssv_noexcept
1372 { return !( lhs == rhs ); }
1376 template< class CharT, class Traits nssv_MSVC_ORDER(1) >
1377 nssv_constexpr bool operator< (
1378 basic_string_view < CharT, Traits > lhs,
1379 nssv_BASIC_STRING_VIEW_I( CharT, Traits ) rhs ) nssv_noexcept
1380 { return lhs.compare( rhs ) < 0; }
1382 template< class CharT, class Traits nssv_MSVC_ORDER(2) >
1383 nssv_constexpr bool operator< (
1384 nssv_BASIC_STRING_VIEW_I( CharT, Traits ) lhs,
1385 basic_string_view < CharT, Traits > rhs ) nssv_noexcept
1386 { return lhs.compare( rhs ) < 0; }
1390 template< class CharT, class Traits nssv_MSVC_ORDER(1) >
1391 nssv_constexpr bool operator<= (
1392 basic_string_view < CharT, Traits > lhs,
1393 nssv_BASIC_STRING_VIEW_I( CharT, Traits ) rhs ) nssv_noexcept
1394 { return lhs.compare( rhs ) <= 0; }
1396 template< class CharT, class Traits nssv_MSVC_ORDER(2) >
1397 nssv_constexpr bool operator<= (
1398 nssv_BASIC_STRING_VIEW_I( CharT, Traits ) lhs,
1399 basic_string_view < CharT, Traits > rhs ) nssv_noexcept
1400 { return lhs.compare( rhs ) <= 0; }
1404 template< class CharT, class Traits nssv_MSVC_ORDER(1) >
1405 nssv_constexpr bool operator> (
1406 basic_string_view < CharT, Traits > lhs,
1407 nssv_BASIC_STRING_VIEW_I( CharT, Traits ) rhs ) nssv_noexcept
1408 { return lhs.compare( rhs ) > 0; }
1410 template< class CharT, class Traits nssv_MSVC_ORDER(2) >
1411 nssv_constexpr bool operator> (
1412 nssv_BASIC_STRING_VIEW_I( CharT, Traits ) lhs,
1413 basic_string_view < CharT, Traits > rhs ) nssv_noexcept
1414 { return lhs.compare( rhs ) > 0; }
1418 template< class CharT, class Traits nssv_MSVC_ORDER(1) >
1419 nssv_constexpr bool operator>= (
1420 basic_string_view < CharT, Traits > lhs,
1421 nssv_BASIC_STRING_VIEW_I( CharT, Traits ) rhs ) nssv_noexcept
1422 { return lhs.compare( rhs ) >= 0; }
1424 template< class CharT, class Traits nssv_MSVC_ORDER(2) >
1425 nssv_constexpr bool operator>= (
1426 nssv_BASIC_STRING_VIEW_I( CharT, Traits ) lhs,
1427 basic_string_view < CharT, Traits > rhs ) nssv_noexcept
1428 { return lhs.compare( rhs ) >= 0; }
1430 #undef nssv_MSVC_ORDER
1431 #undef nssv_BASIC_STRING_VIEW_I
1433 #endif // compiler-dependent approach to comparisons
1435 // 24.4.4 Inserters and extractors:
1437 #if ! nssv_CONFIG_NO_STREAM_INSERTION
1441 template< class Stream >
1442 void write_padding( Stream & os, std::streamsize n )
1444 for ( std::streamsize i = 0; i < n; ++i )
1445 os.rdbuf()->sputc( os.fill() );
1448 template< class Stream, class View >
1449 Stream & write_to_stream( Stream & os, View const & sv )
1451 typename Stream::sentry sentry( os );
1456 const std::streamsize length = static_cast<std::streamsize>( sv.length() );
1458 // Whether, and how, to pad:
1459 const bool pad = ( length < os.width() );
1460 const bool left_pad = pad && ( os.flags() & std::ios_base::adjustfield ) == std::ios_base::right;
1463 write_padding( os, os.width() - length );
1465 // Write span characters:
1466 os.rdbuf()->sputn( sv.begin(), length );
1468 if ( pad && !left_pad )
1469 write_padding( os, os.width() - length );
1471 // Reset output stream width:
1477 } // namespace detail
1479 template< class CharT, class Traits >
1480 std::basic_ostream<CharT, Traits> &
1482 std::basic_ostream<CharT, Traits>& os,
1483 basic_string_view <CharT, Traits> sv )
1485 return detail::write_to_stream( os, sv );
1488 #endif // nssv_CONFIG_NO_STREAM_INSERTION
1490 // Several typedefs for common character types are provided:
1492 typedef basic_string_view<char> string_view;
1493 typedef basic_string_view<wchar_t> wstring_view;
1494 #if nssv_HAVE_WCHAR16_T
1495 typedef basic_string_view<char16_t> u16string_view;
1496 typedef basic_string_view<char32_t> u32string_view;
1499 }} // namespace nonstd::sv_lite
1502 // 24.4.6 Suffix for basic_string_view literals:
1505 #if nssv_HAVE_USER_DEFINED_LITERALS
1508 nssv_inline_ns namespace literals {
1509 nssv_inline_ns namespace string_view_literals {
1511 #if nssv_CONFIG_STD_SV_OPERATOR && nssv_HAVE_STD_DEFINED_LITERALS
1513 nssv_constexpr nonstd::sv_lite::string_view operator "" sv( const char* str, size_t len ) nssv_noexcept // (1)
1515 return nonstd::sv_lite::string_view{ str, len };
1518 nssv_constexpr nonstd::sv_lite::u16string_view operator "" sv( const char16_t* str, size_t len ) nssv_noexcept // (2)
1520 return nonstd::sv_lite::u16string_view{ str, len };
1523 nssv_constexpr nonstd::sv_lite::u32string_view operator "" sv( const char32_t* str, size_t len ) nssv_noexcept // (3)
1525 return nonstd::sv_lite::u32string_view{ str, len };
1528 nssv_constexpr nonstd::sv_lite::wstring_view operator "" sv( const wchar_t* str, size_t len ) nssv_noexcept // (4)
1530 return nonstd::sv_lite::wstring_view{ str, len };
1533 #endif // nssv_CONFIG_STD_SV_OPERATOR && nssv_HAVE_STD_DEFINED_LITERALS
1535 #if nssv_CONFIG_USR_SV_OPERATOR
1537 nssv_constexpr nonstd::sv_lite::string_view operator "" _sv( const char* str, size_t len ) nssv_noexcept // (1)
1539 return nonstd::sv_lite::string_view{ str, len };
1542 nssv_constexpr nonstd::sv_lite::u16string_view operator "" _sv( const char16_t* str, size_t len ) nssv_noexcept // (2)
1544 return nonstd::sv_lite::u16string_view{ str, len };
1547 nssv_constexpr nonstd::sv_lite::u32string_view operator "" _sv( const char32_t* str, size_t len ) nssv_noexcept // (3)
1549 return nonstd::sv_lite::u32string_view{ str, len };
1552 nssv_constexpr nonstd::sv_lite::wstring_view operator "" _sv( const wchar_t* str, size_t len ) nssv_noexcept // (4)
1554 return nonstd::sv_lite::wstring_view{ str, len };
1557 #endif // nssv_CONFIG_USR_SV_OPERATOR
1559 }}} // namespace nonstd::literals::string_view_literals
1564 // Extensions for std::string:
1567 #if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
1572 // Exclude MSVC 14 (19.00): it yields ambiguous to_string():
1574 #if nssv_CPP11_OR_GREATER && nssv_COMPILER_MSVC_VERSION != 140
1576 template< class CharT, class Traits, class Allocator = std::allocator<CharT> >
1577 std::basic_string<CharT, Traits, Allocator>
1578 to_string( basic_string_view<CharT, Traits> v, Allocator const & a = Allocator() )
1580 return std::basic_string<CharT,Traits, Allocator>( v.begin(), v.end(), a );
1585 template< class CharT, class Traits >
1586 std::basic_string<CharT, Traits>
1587 to_string( basic_string_view<CharT, Traits> v )
1589 return std::basic_string<CharT, Traits>( v.begin(), v.end() );
1592 template< class CharT, class Traits, class Allocator >
1593 std::basic_string<CharT, Traits, Allocator>
1594 to_string( basic_string_view<CharT, Traits> v, Allocator const & a )
1596 return std::basic_string<CharT, Traits, Allocator>( v.begin(), v.end(), a );
1599 #endif // nssv_CPP11_OR_GREATER
1601 template< class CharT, class Traits, class Allocator >
1602 basic_string_view<CharT, Traits>
1603 to_string_view( std::basic_string<CharT, Traits, Allocator> const & s )
1605 return basic_string_view<CharT, Traits>( s.data(), s.size() );
1608 }} // namespace nonstd::sv_lite
1610 #endif // nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
1613 // make types and algorithms available in namespace nonstd:
1618 using sv_lite::basic_string_view;
1619 using sv_lite::string_view;
1620 using sv_lite::wstring_view;
1622 #if nssv_HAVE_WCHAR16_T
1623 using sv_lite::u16string_view;
1625 #if nssv_HAVE_WCHAR32_T
1626 using sv_lite::u32string_view;
1631 using sv_lite::operator==;
1632 using sv_lite::operator!=;
1633 using sv_lite::operator<;
1634 using sv_lite::operator<=;
1635 using sv_lite::operator>;
1636 using sv_lite::operator>=;
1638 #if ! nssv_CONFIG_NO_STREAM_INSERTION
1639 using sv_lite::operator<<;
1642 #if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
1643 using sv_lite::to_string;
1644 using sv_lite::to_string_view;
1647 } // namespace nonstd
1649 // 24.4.5 Hash support (C++11):
1651 // Note: The hash value of a string view object is equal to the hash value of
1652 // the corresponding string object.
1654 #if nssv_HAVE_STD_HASH
1656 #include <functional>
1661 struct hash< nonstd::string_view >
1664 std::size_t operator()( nonstd::string_view v ) const nssv_noexcept
1666 return std::hash<std::string>()( std::string( v.data(), v.size() ) );
1671 struct hash< nonstd::wstring_view >
1674 std::size_t operator()( nonstd::wstring_view v ) const nssv_noexcept
1676 return std::hash<std::wstring>()( std::wstring( v.data(), v.size() ) );
1681 struct hash< nonstd::u16string_view >
1684 std::size_t operator()( nonstd::u16string_view v ) const nssv_noexcept
1686 return std::hash<std::u16string>()( std::u16string( v.data(), v.size() ) );
1691 struct hash< nonstd::u32string_view >
1694 std::size_t operator()( nonstd::u32string_view v ) const nssv_noexcept
1696 return std::hash<std::u32string>()( std::u32string( v.data(), v.size() ) );
1702 #endif // nssv_HAVE_STD_HASH
1704 nssv_RESTORE_WARNINGS()
1706 #endif // nssv_HAVE_STD_STRING_VIEW
1707 #endif // NONSTD_SV_LITE_H_INCLUDED