string_view.hpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. #pragma once
  2. #ifndef NONSTD_SV_LITE_H_INCLUDED
  3. #define NONSTD_SV_LITE_H_INCLUDED
  4. #define string_view_lite_MAJOR 1
  5. #define string_view_lite_MINOR 2
  6. #define string_view_lite_PATCH 0
  7. #define string_view_lite_VERSION nssv_STRINGIFY(string_view_lite_MAJOR) "." nssv_STRINGIFY(string_view_lite_MINOR) "." nssv_STRINGIFY(string_view_lite_PATCH)
  8. #define nssv_STRINGIFY( x ) nssv_STRINGIFY_( x )
  9. #define nssv_STRINGIFY_( x ) #x
  10. // string-view lite configuration:
  11. #define nssv_STRING_VIEW_DEFAULT 0
  12. #define nssv_STRING_VIEW_NONSTD 1
  13. #define nssv_STRING_VIEW_STD 2
  14. #if !defined( nssv_CONFIG_SELECT_STRING_VIEW )
  15. # define nssv_CONFIG_SELECT_STRING_VIEW ( nssv_HAVE_STD_STRING_VIEW ? nssv_STRING_VIEW_STD : nssv_STRING_VIEW_NONSTD )
  16. #endif
  17. #if defined( nssv_CONFIG_SELECT_STD_STRING_VIEW ) || defined( nssv_CONFIG_SELECT_NONSTD_STRING_VIEW )
  18. # error nssv_CONFIG_SELECT_STD_STRING_VIEW and nssv_CONFIG_SELECT_NONSTD_STRING_VIEW are deprecated and removed, please use nssv_CONFIG_SELECT_STRING_VIEW=nssv_STRING_VIEW_...
  19. #endif
  20. #ifndef nssv_CONFIG_STD_SV_OPERATOR
  21. # define nssv_CONFIG_STD_SV_OPERATOR 0
  22. #endif
  23. #ifndef nssv_CONFIG_USR_SV_OPERATOR
  24. # define nssv_CONFIG_USR_SV_OPERATOR 1
  25. #endif
  26. #ifdef nssv_CONFIG_CONVERSION_STD_STRING
  27. # define nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS nssv_CONFIG_CONVERSION_STD_STRING
  28. # define nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS nssv_CONFIG_CONVERSION_STD_STRING
  29. #endif
  30. #ifndef nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS
  31. # define nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS 1
  32. #endif
  33. #ifndef nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
  34. # define nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS 1
  35. #endif
  36. // Control presence of exception handling (try and auto discover):
  37. #ifndef nssv_CONFIG_NO_EXCEPTIONS
  38. # if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)
  39. # define nssv_CONFIG_NO_EXCEPTIONS 0
  40. # else
  41. # define nssv_CONFIG_NO_EXCEPTIONS 1
  42. # endif
  43. #endif
  44. // C++ language version detection (C++20 is speculative):
  45. // Note: VC14.0/1900 (VS2015) lacks too much from C++14.
  46. #ifndef nssv_CPLUSPLUS
  47. # if defined(_MSVC_LANG ) && !defined(__clang__)
  48. # define nssv_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG )
  49. # else
  50. # define nssv_CPLUSPLUS __cplusplus
  51. # endif
  52. #endif
  53. #define nssv_CPP98_OR_GREATER ( nssv_CPLUSPLUS >= 199711L )
  54. #define nssv_CPP11_OR_GREATER ( nssv_CPLUSPLUS >= 201103L )
  55. #define nssv_CPP11_OR_GREATER_ ( nssv_CPLUSPLUS >= 201103L )
  56. #define nssv_CPP14_OR_GREATER ( nssv_CPLUSPLUS >= 201402L )
  57. #define nssv_CPP17_OR_GREATER ( nssv_CPLUSPLUS >= 201703L )
  58. #define nssv_CPP20_OR_GREATER ( nssv_CPLUSPLUS >= 202000L )
  59. // use C++17 std::string_view if available and requested:
  60. #if nssv_CPP17_OR_GREATER && defined(__has_include )
  61. # if __has_include( <string_view> )
  62. # define nssv_HAVE_STD_STRING_VIEW 1
  63. # else
  64. # define nssv_HAVE_STD_STRING_VIEW 0
  65. # endif
  66. #else
  67. # define nssv_HAVE_STD_STRING_VIEW 0
  68. #endif
  69. #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) )
  70. #define nssv_HAVE_STARTS_WITH ( nssv_CPP20_OR_GREATER || !nssv_USES_STD_STRING_VIEW )
  71. #define nssv_HAVE_ENDS_WITH nssv_HAVE_STARTS_WITH
  72. //
  73. // Use C++17 std::string_view:
  74. //
  75. #if nssv_USES_STD_STRING_VIEW
  76. #include <string_view>
  77. // Extensions for std::string:
  78. #if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
  79. namespace nonstd {
  80. template< class CharT, class Traits, class Allocator = std::allocator<CharT> >
  81. std::basic_string<CharT, Traits, Allocator>
  82. to_string(std::basic_string_view<CharT, Traits> v, Allocator const& a = Allocator()) {
  83. return std::basic_string<CharT, Traits, Allocator>(v.begin(), v.end(), a);
  84. }
  85. template< class CharT, class Traits, class Allocator >
  86. std::basic_string_view<CharT, Traits>
  87. to_string_view(std::basic_string<CharT, Traits, Allocator> const& s) {
  88. return std::basic_string_view<CharT, Traits>(s.data(), s.size());
  89. }
  90. // Literal operators sv and _sv:
  91. #if nssv_CONFIG_STD_SV_OPERATOR
  92. using namespace std::literals::string_view_literals;
  93. #endif
  94. #if nssv_CONFIG_USR_SV_OPERATOR
  95. inline namespace literals {
  96. inline namespace string_view_literals {
  97. constexpr std::string_view operator "" _sv(const char* str, size_t len) noexcept // (1)
  98. {
  99. return std::string_view{ str, len };
  100. }
  101. constexpr std::u16string_view operator "" _sv(const char16_t* str, size_t len) noexcept // (2)
  102. {
  103. return std::u16string_view{ str, len };
  104. }
  105. constexpr std::u32string_view operator "" _sv(const char32_t* str, size_t len) noexcept // (3)
  106. {
  107. return std::u32string_view{ str, len };
  108. }
  109. constexpr std::wstring_view operator "" _sv(const wchar_t* str, size_t len) noexcept // (4)
  110. {
  111. return std::wstring_view{ str, len };
  112. }
  113. }
  114. } // namespace literals::string_view_literals
  115. #endif // nssv_CONFIG_USR_SV_OPERATOR
  116. } // namespace nonstd
  117. #endif // nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
  118. namespace nonstd {
  119. using std::string_view;
  120. using std::wstring_view;
  121. using std::u16string_view;
  122. using std::u32string_view;
  123. using std::basic_string_view;
  124. // literal "sv" and "_sv", see above
  125. using std::operator==;
  126. using std::operator!=;
  127. using std::operator<;
  128. using std::operator<=;
  129. using std::operator>;
  130. using std::operator>=;
  131. using std::operator<<;
  132. } // namespace nonstd
  133. #else // nssv_HAVE_STD_STRING_VIEW
  134. //
  135. // Before C++17: use string_view lite:
  136. //
  137. // Compiler versions:
  138. //
  139. // MSVC++ 6.0 _MSC_VER == 1200 (Visual Studio 6.0)
  140. // MSVC++ 7.0 _MSC_VER == 1300 (Visual Studio .NET 2002)
  141. // MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio .NET 2003)
  142. // MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005)
  143. // MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008)
  144. // MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010)
  145. // MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012)
  146. // MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013)
  147. // MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015)
  148. // MSVC++ 14.1 _MSC_VER >= 1910 (Visual Studio 2017)
  149. #if defined(_MSC_VER ) && !defined(__clang__)
  150. # define nssv_COMPILER_MSVC_VER (_MSC_VER )
  151. # define nssv_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) )
  152. #else
  153. # define nssv_COMPILER_MSVC_VER 0
  154. # define nssv_COMPILER_MSVC_VERSION 0
  155. #endif
  156. #define nssv_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) )
  157. #if defined(__clang__)
  158. # define nssv_COMPILER_CLANG_VERSION nssv_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
  159. #else
  160. # define nssv_COMPILER_CLANG_VERSION 0
  161. #endif
  162. #if defined(__GNUC__) && !defined(__clang__)
  163. # define nssv_COMPILER_GNUC_VERSION nssv_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
  164. #else
  165. # define nssv_COMPILER_GNUC_VERSION 0
  166. #endif
  167. // half-open range [lo..hi):
  168. #define nssv_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) )
  169. // Presence of language and library features:
  170. #ifdef _HAS_CPP0X
  171. # define nssv_HAS_CPP0X _HAS_CPP0X
  172. #else
  173. # define nssv_HAS_CPP0X 0
  174. #endif
  175. // Unless defined otherwise below, consider VC14 as C++11 for variant-lite:
  176. #if nssv_COMPILER_MSVC_VER >= 1900
  177. # undef nssv_CPP11_OR_GREATER
  178. # define nssv_CPP11_OR_GREATER 1
  179. #endif
  180. #define nssv_CPP11_90 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1500)
  181. #define nssv_CPP11_100 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1600)
  182. #define nssv_CPP11_110 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1700)
  183. #define nssv_CPP11_120 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1800)
  184. #define nssv_CPP11_140 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1900)
  185. #define nssv_CPP11_141 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1910)
  186. #define nssv_CPP14_000 (nssv_CPP14_OR_GREATER)
  187. #define nssv_CPP17_000 (nssv_CPP17_OR_GREATER)
  188. // Presence of C++11 language features:
  189. #define nssv_HAVE_CONSTEXPR_11 nssv_CPP11_140
  190. #define nssv_HAVE_EXPLICIT_CONVERSION nssv_CPP11_140
  191. #define nssv_HAVE_INLINE_NAMESPACE nssv_CPP11_140
  192. #define nssv_HAVE_NOEXCEPT nssv_CPP11_140
  193. #define nssv_HAVE_NULLPTR nssv_CPP11_100
  194. #define nssv_HAVE_REF_QUALIFIER nssv_CPP11_140
  195. #define nssv_HAVE_UNICODE_LITERALS nssv_CPP11_140
  196. #define nssv_HAVE_USER_DEFINED_LITERALS nssv_CPP11_140
  197. #define nssv_HAVE_WCHAR16_T nssv_CPP11_100
  198. #define nssv_HAVE_WCHAR32_T nssv_CPP11_100
  199. #if ! ( ( nssv_CPP11_OR_GREATER && nssv_COMPILER_CLANG_VERSION ) || nssv_BETWEEN( nssv_COMPILER_CLANG_VERSION, 300, 400 ) )
  200. # define nssv_HAVE_STD_DEFINED_LITERALS nssv_CPP11_140
  201. #else
  202. # define nssv_HAVE_STD_DEFINED_LITERALS 0
  203. #endif
  204. // Presence of C++14 language features:
  205. #define nssv_HAVE_CONSTEXPR_14 nssv_CPP14_000
  206. // Presence of C++17 language features:
  207. #define nssv_HAVE_NODISCARD nssv_CPP17_000
  208. // Presence of C++ library features:
  209. #define nssv_HAVE_STD_HASH nssv_CPP11_120
  210. // C++ feature usage:
  211. #if nssv_HAVE_CONSTEXPR_11
  212. # define nssv_constexpr constexpr
  213. #else
  214. # define nssv_constexpr /*constexpr*/
  215. #endif
  216. #if nssv_HAVE_CONSTEXPR_14
  217. # define nssv_constexpr14 constexpr
  218. #else
  219. # define nssv_constexpr14 /*constexpr*/
  220. #endif
  221. #if nssv_HAVE_EXPLICIT_CONVERSION
  222. # define nssv_explicit explicit
  223. #else
  224. # define nssv_explicit /*explicit*/
  225. #endif
  226. #if nssv_HAVE_INLINE_NAMESPACE
  227. # define nssv_inline_ns inline
  228. #else
  229. # define nssv_inline_ns /*inline*/
  230. #endif
  231. #if nssv_HAVE_NOEXCEPT
  232. # define nssv_noexcept noexcept
  233. #else
  234. # define nssv_noexcept /*noexcept*/
  235. #endif
  236. //#if nssv_HAVE_REF_QUALIFIER
  237. //# define nssv_ref_qual &
  238. //# define nssv_refref_qual &&
  239. //#else
  240. //# define nssv_ref_qual /*&*/
  241. //# define nssv_refref_qual /*&&*/
  242. //#endif
  243. #if nssv_HAVE_NULLPTR
  244. # define nssv_nullptr nullptr
  245. #else
  246. # define nssv_nullptr NULL
  247. #endif
  248. #if nssv_HAVE_NODISCARD
  249. # define nssv_nodiscard [[nodiscard]]
  250. #else
  251. # define nssv_nodiscard /*[[nodiscard]]*/
  252. #endif
  253. // Additional includes:
  254. #include <algorithm>
  255. #include <cassert>
  256. #include <iterator>
  257. #include <limits>
  258. #include <ostream>
  259. #include <string> // std::char_traits<>
  260. #if ! nssv_CONFIG_NO_EXCEPTIONS
  261. # include <stdexcept>
  262. #endif
  263. #if nssv_CPP11_OR_GREATER
  264. # include <type_traits>
  265. #endif
  266. // Clang, GNUC, MSVC warning suppression macros:
  267. #if defined(__clang__)
  268. # pragma clang diagnostic ignored "-Wreserved-user-defined-literal"
  269. # pragma clang diagnostic push
  270. # pragma clang diagnostic ignored "-Wuser-defined-literals"
  271. #elif defined(__GNUC__)
  272. # pragma GCC diagnostic push
  273. # pragma GCC diagnostic ignored "-Wliteral-suffix"
  274. #endif // __clang__
  275. #if nssv_COMPILER_MSVC_VERSION >= 140
  276. # define nssv_SUPPRESS_MSGSL_WARNING(expr) [[gsl::suppress(expr)]]
  277. # define nssv_SUPPRESS_MSVC_WARNING(code, descr) __pragma(warning(suppress: code) )
  278. # define nssv_DISABLE_MSVC_WARNINGS(codes) __pragma(warning(push)) __pragma(warning(disable: codes))
  279. #else
  280. # define nssv_SUPPRESS_MSGSL_WARNING(expr)
  281. # define nssv_SUPPRESS_MSVC_WARNING(code, descr)
  282. # define nssv_DISABLE_MSVC_WARNINGS(codes)
  283. #endif
  284. #if defined(__clang__)
  285. # define nssv_RESTORE_WARNINGS() _Pragma("clang diagnostic pop")
  286. #elif defined(__GNUC__)
  287. # define nssv_RESTORE_WARNINGS() _Pragma("GCC diagnostic pop")
  288. #elif nssv_COMPILER_MSVC_VERSION >= 140
  289. # define nssv_RESTORE_WARNINGS() __pragma(warning(pop ))
  290. #else
  291. # define nssv_RESTORE_WARNINGS()
  292. #endif
  293. // Suppress the following MSVC (GSL) warnings:
  294. // - C4455, non-gsl : 'operator ""sv': literal suffix identifiers that do not
  295. // start with an underscore are reserved
  296. // - C26472, gsl::t.1 : don't use a static_cast for arithmetic conversions;
  297. // use brace initialization, gsl::narrow_cast or gsl::narow
  298. // - C26481: gsl::b.1 : don't use pointer arithmetic. Use span instead
  299. nssv_DISABLE_MSVC_WARNINGS(4455 26481 26472)
  300. //nssv_DISABLE_CLANG_WARNINGS( "-Wuser-defined-literals" )
  301. //nssv_DISABLE_GNUC_WARNINGS( -Wliteral-suffix )
  302. namespace nonstd {
  303. namespace sv_lite {
  304. #if nssv_CPP11_OR_GREATER
  305. namespace detail {
  306. // Expect tail call optimization to make length() non-recursive:
  307. template< typename CharT >
  308. inline constexpr std::size_t length(CharT* s, std::size_t result = 0) {
  309. return *s == '\0' ? result : length(s + 1, result + 1);
  310. }
  311. } // namespace detail
  312. #endif // nssv_CPP11_OR_GREATER
  313. template
  314. <
  315. class CharT,
  316. class Traits = std::char_traits<CharT>
  317. >
  318. class basic_string_view;
  319. //
  320. // basic_string_view:
  321. //
  322. template
  323. <
  324. class CharT,
  325. class Traits /* = std::char_traits<CharT> */
  326. >
  327. class basic_string_view {
  328. public:
  329. // Member types:
  330. typedef Traits traits_type;
  331. typedef CharT value_type;
  332. typedef CharT* pointer;
  333. typedef CharT const* const_pointer;
  334. typedef CharT& reference;
  335. typedef CharT const& const_reference;
  336. typedef const_pointer iterator;
  337. typedef const_pointer const_iterator;
  338. typedef std::reverse_iterator< const_iterator > reverse_iterator;
  339. typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
  340. typedef std::size_t size_type;
  341. typedef std::ptrdiff_t difference_type;
  342. // 24.4.2.1 Construction and assignment:
  343. nssv_constexpr basic_string_view() nssv_noexcept
  344. : data_(nssv_nullptr)
  345. , size_(0) {
  346. }
  347. #if nssv_CPP11_OR_GREATER
  348. nssv_constexpr basic_string_view(basic_string_view const& other) nssv_noexcept = default;
  349. #else
  350. nssv_constexpr basic_string_view(basic_string_view const& other) nssv_noexcept
  351. : data_(other.data_)
  352. , size_(other.size_) {
  353. }
  354. #endif
  355. nssv_constexpr basic_string_view(CharT const* s, size_type count) nssv_noexcept // non-standard noexcept
  356. : data_(s)
  357. , size_(count) {
  358. }
  359. nssv_constexpr basic_string_view(CharT const* s) nssv_noexcept // non-standard noexcept
  360. : data_(s)
  361. #if nssv_CPP17_OR_GREATER
  362. , size_(Traits::length(s))
  363. #elif nssv_CPP11_OR_GREATER
  364. , size_(detail::length(s))
  365. #else
  366. , size_(Traits::length(s))
  367. #endif
  368. {
  369. }
  370. // Assignment:
  371. #if nssv_CPP11_OR_GREATER
  372. nssv_constexpr14 basic_string_view& operator=(basic_string_view const& other) nssv_noexcept = default;
  373. #else
  374. nssv_constexpr14 basic_string_view& operator=(basic_string_view const& other) nssv_noexcept {
  375. data_ = other.data_;
  376. size_ = other.size_;
  377. return *this;
  378. }
  379. #endif
  380. // 24.4.2.2 Iterator support:
  381. nssv_constexpr const_iterator begin() const nssv_noexcept { return data_; }
  382. nssv_constexpr const_iterator end() const nssv_noexcept { return data_ + size_; }
  383. nssv_constexpr const_iterator cbegin() const nssv_noexcept { return begin(); }
  384. nssv_constexpr const_iterator cend() const nssv_noexcept { return end(); }
  385. nssv_constexpr const_reverse_iterator rbegin() const nssv_noexcept { return const_reverse_iterator(end()); }
  386. nssv_constexpr const_reverse_iterator rend() const nssv_noexcept { return const_reverse_iterator(begin()); }
  387. nssv_constexpr const_reverse_iterator crbegin() const nssv_noexcept { return rbegin(); }
  388. nssv_constexpr const_reverse_iterator crend() const nssv_noexcept { return rend(); }
  389. // 24.4.2.3 Capacity:
  390. nssv_constexpr size_type size() const nssv_noexcept { return size_; }
  391. nssv_constexpr size_type length() const nssv_noexcept { return size_; }
  392. nssv_constexpr size_type max_size() const nssv_noexcept { return (std::numeric_limits< size_type >::max)(); }
  393. // since C++20
  394. nssv_nodiscard nssv_constexpr bool empty() const nssv_noexcept {
  395. return 0 == size_;
  396. }
  397. // 24.4.2.4 Element access:
  398. nssv_constexpr const_reference operator[](size_type pos) const {
  399. return data_at(pos);
  400. }
  401. nssv_constexpr14 const_reference at(size_type pos) const {
  402. #if nssv_CONFIG_NO_EXCEPTIONS
  403. assert(pos < size());
  404. #else
  405. if (pos >= size()) {
  406. throw std::out_of_range("nonstd::string_view::at()");
  407. }
  408. #endif
  409. return data_at(pos);
  410. }
  411. nssv_constexpr const_reference front() const { return data_at(0); }
  412. nssv_constexpr const_reference back() const { return data_at(size() - 1); }
  413. nssv_constexpr const_pointer data() const nssv_noexcept { return data_; }
  414. // 24.4.2.5 Modifiers:
  415. nssv_constexpr14 void remove_prefix(size_type n) {
  416. assert(n <= size());
  417. data_ += n;
  418. size_ -= n;
  419. }
  420. nssv_constexpr14 void remove_suffix(size_type n) {
  421. assert(n <= size());
  422. size_ -= n;
  423. }
  424. nssv_constexpr14 void swap(basic_string_view & other) nssv_noexcept {
  425. using std::swap;
  426. swap(data_, other.data_);
  427. swap(size_, other.size_);
  428. }
  429. // 24.4.2.6 String operations:
  430. size_type copy(CharT * dest, size_type n, size_type pos = 0) const {
  431. #if nssv_CONFIG_NO_EXCEPTIONS
  432. assert(pos <= size());
  433. #else
  434. if (pos > size()) {
  435. throw std::out_of_range("nonstd::string_view::copy()");
  436. }
  437. #endif
  438. const size_type rlen = (std::min)(n, size() - pos);
  439. (void)Traits::copy(dest, data() + pos, rlen);
  440. return rlen;
  441. }
  442. nssv_constexpr14 basic_string_view substr(size_type pos = 0, size_type n = npos) const {
  443. #if nssv_CONFIG_NO_EXCEPTIONS
  444. assert(pos <= size());
  445. #else
  446. if (pos > size()) {
  447. throw std::out_of_range("nonstd::string_view::substr()");
  448. }
  449. #endif
  450. return basic_string_view(data() + pos, (std::min)(n, size() - pos));
  451. }
  452. // compare(), 6x:
  453. nssv_constexpr14 int compare(basic_string_view other) const nssv_noexcept // (1)
  454. {
  455. if (const int result = Traits::compare(data(), other.data(), (std::min)(size(), other.size()))) {
  456. return result;
  457. }
  458. return size() == other.size() ? 0 : size() < other.size() ? -1 : 1;
  459. }
  460. nssv_constexpr int compare(size_type pos1, size_type n1, basic_string_view other) const // (2)
  461. {
  462. return substr(pos1, n1).compare(other);
  463. }
  464. nssv_constexpr int compare(size_type pos1, size_type n1, basic_string_view other, size_type pos2, size_type n2) const // (3)
  465. {
  466. return substr(pos1, n1).compare(other.substr(pos2, n2));
  467. }
  468. nssv_constexpr int compare(CharT const* s) const // (4)
  469. {
  470. return compare(basic_string_view(s));
  471. }
  472. nssv_constexpr int compare(size_type pos1, size_type n1, CharT const* s) const // (5)
  473. {
  474. return substr(pos1, n1).compare(basic_string_view(s));
  475. }
  476. nssv_constexpr int compare(size_type pos1, size_type n1, CharT const* s, size_type n2) const // (6)
  477. {
  478. return substr(pos1, n1).compare(basic_string_view(s, n2));
  479. }
  480. // 24.4.2.7 Searching:
  481. // starts_with(), 3x, since C++20:
  482. nssv_constexpr bool starts_with(basic_string_view v) const nssv_noexcept // (1)
  483. {
  484. return size() >= v.size() && compare(0, v.size(), v) == 0;
  485. }
  486. nssv_constexpr bool starts_with(CharT c) const nssv_noexcept // (2)
  487. {
  488. return starts_with(basic_string_view(&c, 1));
  489. }
  490. nssv_constexpr bool starts_with(CharT const* s) const // (3)
  491. {
  492. return starts_with(basic_string_view(s));
  493. }
  494. // ends_with(), 3x, since C++20:
  495. nssv_constexpr bool ends_with(basic_string_view v) const nssv_noexcept // (1)
  496. {
  497. return size() >= v.size() && compare(size() - v.size(), npos, v) == 0;
  498. }
  499. nssv_constexpr bool ends_with(CharT c) const nssv_noexcept // (2)
  500. {
  501. return ends_with(basic_string_view(&c, 1));
  502. }
  503. nssv_constexpr bool ends_with(CharT const* s) const // (3)
  504. {
  505. return ends_with(basic_string_view(s));
  506. }
  507. // find(), 4x:
  508. nssv_constexpr14 size_type find(basic_string_view v, size_type pos = 0) const nssv_noexcept // (1)
  509. {
  510. return assert(v.size() == 0 || v.data() != nssv_nullptr)
  511. , pos >= size()
  512. ? npos
  513. : to_pos(std::search(cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq));
  514. }
  515. nssv_constexpr14 size_type find(CharT c, size_type pos = 0) const nssv_noexcept // (2)
  516. {
  517. return find(basic_string_view(&c, 1), pos);
  518. }
  519. nssv_constexpr14 size_type find(CharT const* s, size_type pos, size_type n) const // (3)
  520. {
  521. return find(basic_string_view(s, n), pos);
  522. }
  523. nssv_constexpr14 size_type find(CharT const* s, size_type pos = 0) const // (4)
  524. {
  525. return find(basic_string_view(s), pos);
  526. }
  527. // rfind(), 4x:
  528. nssv_constexpr14 size_type rfind(basic_string_view v, size_type pos = npos) const nssv_noexcept // (1)
  529. {
  530. if (size() < v.size()) {
  531. return npos;
  532. }
  533. if (v.empty()) {
  534. return (std::min)(size(), pos);
  535. }
  536. const_iterator last = cbegin() + (std::min)(size() - v.size(), pos) + v.size();
  537. const_iterator result = std::find_end(cbegin(), last, v.cbegin(), v.cend(), Traits::eq);
  538. return result != last ? size_type(result - cbegin()) : npos;
  539. }
  540. nssv_constexpr14 size_type rfind(CharT c, size_type pos = npos) const nssv_noexcept // (2)
  541. {
  542. return rfind(basic_string_view(&c, 1), pos);
  543. }
  544. nssv_constexpr14 size_type rfind(CharT const* s, size_type pos, size_type n) const // (3)
  545. {
  546. return rfind(basic_string_view(s, n), pos);
  547. }
  548. nssv_constexpr14 size_type rfind(CharT const* s, size_type pos = npos) const // (4)
  549. {
  550. return rfind(basic_string_view(s), pos);
  551. }
  552. // find_first_of(), 4x:
  553. nssv_constexpr size_type find_first_of(basic_string_view v, size_type pos = 0) const nssv_noexcept // (1)
  554. {
  555. return pos >= size()
  556. ? npos
  557. : to_pos(std::find_first_of(cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq));
  558. }
  559. nssv_constexpr size_type find_first_of(CharT c, size_type pos = 0) const nssv_noexcept // (2)
  560. {
  561. return find_first_of(basic_string_view(&c, 1), pos);
  562. }
  563. nssv_constexpr size_type find_first_of(CharT const* s, size_type pos, size_type n) const // (3)
  564. {
  565. return find_first_of(basic_string_view(s, n), pos);
  566. }
  567. nssv_constexpr size_type find_first_of(CharT const* s, size_type pos = 0) const // (4)
  568. {
  569. return find_first_of(basic_string_view(s), pos);
  570. }
  571. // find_last_of(), 4x:
  572. nssv_constexpr size_type find_last_of(basic_string_view v, size_type pos = npos) const nssv_noexcept // (1)
  573. {
  574. return empty()
  575. ? npos
  576. : pos >= size()
  577. ? find_last_of(v, size() - 1)
  578. : to_pos(std::find_first_of(const_reverse_iterator(cbegin() + pos + 1), crend(), v.cbegin(), v.cend(), Traits::eq));
  579. }
  580. nssv_constexpr size_type find_last_of(CharT c, size_type pos = npos) const nssv_noexcept // (2)
  581. {
  582. return find_last_of(basic_string_view(&c, 1), pos);
  583. }
  584. nssv_constexpr size_type find_last_of(CharT const* s, size_type pos, size_type count) const // (3)
  585. {
  586. return find_last_of(basic_string_view(s, count), pos);
  587. }
  588. nssv_constexpr size_type find_last_of(CharT const* s, size_type pos = npos) const // (4)
  589. {
  590. return find_last_of(basic_string_view(s), pos);
  591. }
  592. // find_first_not_of(), 4x:
  593. nssv_constexpr size_type find_first_not_of(basic_string_view v, size_type pos = 0) const nssv_noexcept // (1)
  594. {
  595. return pos >= size()
  596. ? npos
  597. : to_pos(std::find_if(cbegin() + pos, cend(), not_in_view(v)));
  598. }
  599. nssv_constexpr size_type find_first_not_of(CharT c, size_type pos = 0) const nssv_noexcept // (2)
  600. {
  601. return find_first_not_of(basic_string_view(&c, 1), pos);
  602. }
  603. nssv_constexpr size_type find_first_not_of(CharT const* s, size_type pos, size_type count) const // (3)
  604. {
  605. return find_first_not_of(basic_string_view(s, count), pos);
  606. }
  607. nssv_constexpr size_type find_first_not_of(CharT const* s, size_type pos = 0) const // (4)
  608. {
  609. return find_first_not_of(basic_string_view(s), pos);
  610. }
  611. // find_last_not_of(), 4x:
  612. nssv_constexpr size_type find_last_not_of(basic_string_view v, size_type pos = npos) const nssv_noexcept // (1)
  613. {
  614. return empty()
  615. ? npos
  616. : pos >= size()
  617. ? find_last_not_of(v, size() - 1)
  618. : to_pos(std::find_if(const_reverse_iterator(cbegin() + pos + 1), crend(), not_in_view(v)));
  619. }
  620. nssv_constexpr size_type find_last_not_of(CharT c, size_type pos = npos) const nssv_noexcept // (2)
  621. {
  622. return find_last_not_of(basic_string_view(&c, 1), pos);
  623. }
  624. nssv_constexpr size_type find_last_not_of(CharT const* s, size_type pos, size_type count) const // (3)
  625. {
  626. return find_last_not_of(basic_string_view(s, count), pos);
  627. }
  628. nssv_constexpr size_type find_last_not_of(CharT const* s, size_type pos = npos) const // (4)
  629. {
  630. return find_last_not_of(basic_string_view(s), pos);
  631. }
  632. // Constants:
  633. #if nssv_CPP17_OR_GREATER
  634. static nssv_constexpr size_type npos = size_type(-1);
  635. #elif nssv_CPP11_OR_GREATER
  636. enum : size_type { npos = size_type(-1) };
  637. #else
  638. enum { npos = size_type(-1) };
  639. #endif
  640. private:
  641. struct not_in_view {
  642. const basic_string_view v;
  643. nssv_constexpr explicit not_in_view(basic_string_view v) : v(v) {}
  644. nssv_constexpr bool operator()(CharT c) const {
  645. return npos == v.find_first_of(c);
  646. }
  647. };
  648. nssv_constexpr size_type to_pos(const_iterator it) const {
  649. return it == cend() ? npos : size_type(it - cbegin());
  650. }
  651. nssv_constexpr size_type to_pos(const_reverse_iterator it) const {
  652. return it == crend() ? npos : size_type(crend() - it - 1);
  653. }
  654. nssv_constexpr const_reference data_at(size_type pos) const {
  655. #if nssv_BETWEEN( nssv_COMPILER_GNUC_VERSION, 1, 500 )
  656. return data_[pos];
  657. #else
  658. return assert(pos < size()), data_[pos];
  659. #endif
  660. }
  661. private:
  662. const_pointer data_;
  663. size_type size_;
  664. public:
  665. #if nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS
  666. template< class Allocator >
  667. basic_string_view(std::basic_string<CharT, Traits, Allocator> const& s) nssv_noexcept
  668. : data_(s.data())
  669. , size_(s.size()) {
  670. }
  671. #if nssv_HAVE_EXPLICIT_CONVERSION
  672. template< class Allocator >
  673. explicit operator std::basic_string<CharT, Traits, Allocator>() const {
  674. return to_string(Allocator());
  675. }
  676. #endif // nssv_HAVE_EXPLICIT_CONVERSION
  677. #if nssv_CPP11_OR_GREATER
  678. template< class Allocator = std::allocator<CharT> >
  679. std::basic_string<CharT, Traits, Allocator>
  680. to_string(Allocator const& a = Allocator()) const {
  681. return std::basic_string<CharT, Traits, Allocator>(begin(), end(), a);
  682. }
  683. #else
  684. std::basic_string<CharT, Traits>
  685. to_string() const {
  686. return std::basic_string<CharT, Traits>(begin(), end());
  687. }
  688. template< class Allocator >
  689. std::basic_string<CharT, Traits, Allocator>
  690. to_string(Allocator const& a) const {
  691. return std::basic_string<CharT, Traits, Allocator>(begin(), end(), a);
  692. }
  693. #endif // nssv_CPP11_OR_GREATER
  694. #endif // nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS
  695. };
  696. //
  697. // Non-member functions:
  698. //
  699. // 24.4.3 Non-member comparison functions:
  700. // lexicographically compare two string views (function template):
  701. template< class CharT, class Traits >
  702. nssv_constexpr bool operator== (
  703. basic_string_view <CharT, Traits> lhs,
  704. basic_string_view <CharT, Traits> rhs) nssv_noexcept {
  705. return lhs.compare(rhs) == 0;
  706. }
  707. template< class CharT, class Traits >
  708. nssv_constexpr bool operator!= (
  709. basic_string_view <CharT, Traits> lhs,
  710. basic_string_view <CharT, Traits> rhs) nssv_noexcept {
  711. return lhs.compare(rhs) != 0;
  712. }
  713. template< class CharT, class Traits >
  714. nssv_constexpr bool operator< (
  715. basic_string_view <CharT, Traits> lhs,
  716. basic_string_view <CharT, Traits> rhs) nssv_noexcept {
  717. return lhs.compare(rhs) < 0;
  718. }
  719. template< class CharT, class Traits >
  720. nssv_constexpr bool operator<= (
  721. basic_string_view <CharT, Traits> lhs,
  722. basic_string_view <CharT, Traits> rhs) nssv_noexcept {
  723. return lhs.compare(rhs) <= 0;
  724. }
  725. template< class CharT, class Traits >
  726. nssv_constexpr bool operator> (
  727. basic_string_view <CharT, Traits> lhs,
  728. basic_string_view <CharT, Traits> rhs) nssv_noexcept {
  729. return lhs.compare(rhs) > 0;
  730. }
  731. template< class CharT, class Traits >
  732. nssv_constexpr bool operator>= (
  733. basic_string_view <CharT, Traits> lhs,
  734. basic_string_view <CharT, Traits> rhs) nssv_noexcept {
  735. return lhs.compare(rhs) >= 0;
  736. }
  737. // Let S be basic_string_view<CharT, Traits>, and sv be an instance of S.
  738. // Implementations shall provide sufficient additional overloads marked
  739. // constexpr and noexcept so that an object t with an implicit conversion
  740. // to S can be compared according to Table 67.
  741. #if nssv_CPP11_OR_GREATER && ! nssv_BETWEEN( nssv_COMPILER_MSVC_VERSION, 100, 141 )
  742. #define nssv_BASIC_STRING_VIEW_I(T,U) typename std::decay< basic_string_view<T,U> >::type
  743. #if nssv_BETWEEN( nssv_COMPILER_MSVC_VERSION, 140, 150 )
  744. # define nssv_MSVC_ORDER(x) , int=x
  745. #else
  746. # define nssv_MSVC_ORDER(x) /*, int=x*/
  747. #endif
  748. // ==
  749. template< class CharT, class Traits nssv_MSVC_ORDER(1) >
  750. nssv_constexpr bool operator==(
  751. basic_string_view <CharT, Traits> lhs,
  752. nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept {
  753. return lhs.compare(rhs) == 0;
  754. }
  755. template< class CharT, class Traits nssv_MSVC_ORDER(2) >
  756. nssv_constexpr bool operator==(
  757. nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs,
  758. basic_string_view <CharT, Traits> rhs) nssv_noexcept {
  759. return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
  760. }
  761. // !=
  762. template< class CharT, class Traits nssv_MSVC_ORDER(1) >
  763. nssv_constexpr bool operator!= (
  764. basic_string_view < CharT, Traits > lhs,
  765. nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept {
  766. return lhs.size() != rhs.size() || lhs.compare(rhs) != 0;
  767. }
  768. template< class CharT, class Traits nssv_MSVC_ORDER(2) >
  769. nssv_constexpr bool operator!= (
  770. nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs,
  771. basic_string_view < CharT, Traits > rhs) nssv_noexcept {
  772. return lhs.compare(rhs) != 0;
  773. }
  774. // <
  775. template< class CharT, class Traits nssv_MSVC_ORDER(1) >
  776. nssv_constexpr bool operator< (
  777. basic_string_view < CharT, Traits > lhs,
  778. nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept {
  779. return lhs.compare(rhs) < 0;
  780. }
  781. template< class CharT, class Traits nssv_MSVC_ORDER(2) >
  782. nssv_constexpr bool operator< (
  783. nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs,
  784. basic_string_view < CharT, Traits > rhs) nssv_noexcept {
  785. return lhs.compare(rhs) < 0;
  786. }
  787. // <=
  788. template< class CharT, class Traits nssv_MSVC_ORDER(1) >
  789. nssv_constexpr bool operator<= (
  790. basic_string_view < CharT, Traits > lhs,
  791. nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept {
  792. return lhs.compare(rhs) <= 0;
  793. }
  794. template< class CharT, class Traits nssv_MSVC_ORDER(2) >
  795. nssv_constexpr bool operator<= (
  796. nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs,
  797. basic_string_view < CharT, Traits > rhs) nssv_noexcept {
  798. return lhs.compare(rhs) <= 0;
  799. }
  800. // >
  801. template< class CharT, class Traits nssv_MSVC_ORDER(1) >
  802. nssv_constexpr bool operator> (
  803. basic_string_view < CharT, Traits > lhs,
  804. nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept {
  805. return lhs.compare(rhs) > 0;
  806. }
  807. template< class CharT, class Traits nssv_MSVC_ORDER(2) >
  808. nssv_constexpr bool operator> (
  809. nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs,
  810. basic_string_view < CharT, Traits > rhs) nssv_noexcept {
  811. return lhs.compare(rhs) > 0;
  812. }
  813. // >=
  814. template< class CharT, class Traits nssv_MSVC_ORDER(1) >
  815. nssv_constexpr bool operator>= (
  816. basic_string_view < CharT, Traits > lhs,
  817. nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept {
  818. return lhs.compare(rhs) >= 0;
  819. }
  820. template< class CharT, class Traits nssv_MSVC_ORDER(2) >
  821. nssv_constexpr bool operator>= (
  822. nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs,
  823. basic_string_view < CharT, Traits > rhs) nssv_noexcept {
  824. return lhs.compare(rhs) >= 0;
  825. }
  826. #undef nssv_MSVC_ORDER
  827. #undef nssv_BASIC_STRING_VIEW_I
  828. #endif // nssv_CPP11_OR_GREATER
  829. // 24.4.4 Inserters and extractors:
  830. namespace detail {
  831. template< class Stream >
  832. void write_padding(Stream& os, std::streamsize n) {
  833. for (std::streamsize i = 0; i < n; ++i)
  834. os.rdbuf()->sputc(os.fill());
  835. }
  836. template< class Stream, class View >
  837. Stream& write_to_stream(Stream & os, View const& sv) {
  838. typename Stream::sentry sentry(os);
  839. if (!os)
  840. return os;
  841. const std::streamsize length = static_cast<std::streamsize>(sv.length());
  842. // Whether, and how, to pad:
  843. const bool pad = (length < os.width());
  844. const bool left_pad = pad && (os.flags() & std::ios_base::adjustfield) == std::ios_base::right;
  845. if (left_pad)
  846. write_padding(os, os.width() - length);
  847. // Write span characters:
  848. os.rdbuf()->sputn(sv.begin(), length);
  849. if (pad && !left_pad)
  850. write_padding(os, os.width() - length);
  851. // Reset output stream width:
  852. os.width(0);
  853. return os;
  854. }
  855. } // namespace detail
  856. template< class CharT, class Traits >
  857. std::basic_ostream<CharT, Traits>&
  858. operator<<(
  859. std::basic_ostream<CharT, Traits> & os,
  860. basic_string_view <CharT, Traits> sv) {
  861. return detail::write_to_stream(os, sv);
  862. }
  863. // Several typedefs for common character types are provided:
  864. typedef basic_string_view<char> string_view;
  865. typedef basic_string_view<wchar_t> wstring_view;
  866. #if nssv_HAVE_WCHAR16_T
  867. typedef basic_string_view<char16_t> u16string_view;
  868. typedef basic_string_view<char32_t> u32string_view;
  869. #endif
  870. }
  871. } // namespace nonstd::sv_lite
  872. //
  873. // 24.4.6 Suffix for basic_string_view literals:
  874. //
  875. #if nssv_HAVE_USER_DEFINED_LITERALS
  876. namespace nonstd {
  877. nssv_inline_ns namespace literals {
  878. nssv_inline_ns namespace string_view_literals {
  879. #if nssv_CONFIG_STD_SV_OPERATOR && nssv_HAVE_STD_DEFINED_LITERALS
  880. nssv_constexpr nonstd::sv_lite::string_view operator "" sv(const char* str, size_t len) nssv_noexcept // (1)
  881. {
  882. return nonstd::sv_lite::string_view{ str, len };
  883. }
  884. nssv_constexpr nonstd::sv_lite::u16string_view operator "" sv(const char16_t* str, size_t len) nssv_noexcept // (2)
  885. {
  886. return nonstd::sv_lite::u16string_view{ str, len };
  887. }
  888. nssv_constexpr nonstd::sv_lite::u32string_view operator "" sv(const char32_t* str, size_t len) nssv_noexcept // (3)
  889. {
  890. return nonstd::sv_lite::u32string_view{ str, len };
  891. }
  892. nssv_constexpr nonstd::sv_lite::wstring_view operator "" sv(const wchar_t* str, size_t len) nssv_noexcept // (4)
  893. {
  894. return nonstd::sv_lite::wstring_view{ str, len };
  895. }
  896. #endif // nssv_CONFIG_STD_SV_OPERATOR && nssv_HAVE_STD_DEFINED_LITERALS
  897. #if nssv_CONFIG_USR_SV_OPERATOR
  898. nssv_constexpr nonstd::sv_lite::string_view operator "" _sv(const char* str, size_t len) nssv_noexcept // (1)
  899. {
  900. return nonstd::sv_lite::string_view{ str, len };
  901. }
  902. nssv_constexpr nonstd::sv_lite::u16string_view operator "" _sv(const char16_t* str, size_t len) nssv_noexcept // (2)
  903. {
  904. return nonstd::sv_lite::u16string_view{ str, len };
  905. }
  906. nssv_constexpr nonstd::sv_lite::u32string_view operator "" _sv(const char32_t* str, size_t len) nssv_noexcept // (3)
  907. {
  908. return nonstd::sv_lite::u32string_view{ str, len };
  909. }
  910. nssv_constexpr nonstd::sv_lite::wstring_view operator "" _sv(const wchar_t* str, size_t len) nssv_noexcept // (4)
  911. {
  912. return nonstd::sv_lite::wstring_view{ str, len };
  913. }
  914. #endif // nssv_CONFIG_USR_SV_OPERATOR
  915. }
  916. }
  917. } // namespace nonstd::literals::string_view_literals
  918. #endif
  919. //
  920. // Extensions for std::string:
  921. //
  922. #if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
  923. namespace nonstd {
  924. namespace sv_lite {
  925. // Exclude MSVC 14 (19.00): it yields ambiguous to_string():
  926. #if nssv_CPP11_OR_GREATER && nssv_COMPILER_MSVC_VERSION != 140
  927. template< class CharT, class Traits, class Allocator = std::allocator<CharT> >
  928. std::basic_string<CharT, Traits, Allocator>
  929. to_string(basic_string_view<CharT, Traits> v, Allocator const& a = Allocator()) {
  930. return std::basic_string<CharT, Traits, Allocator>(v.begin(), v.end(), a);
  931. }
  932. #else
  933. template< class CharT, class Traits >
  934. std::basic_string<CharT, Traits>
  935. to_string(basic_string_view<CharT, Traits> v) {
  936. return std::basic_string<CharT, Traits>(v.begin(), v.end());
  937. }
  938. template< class CharT, class Traits, class Allocator >
  939. std::basic_string<CharT, Traits, Allocator>
  940. to_string(basic_string_view<CharT, Traits> v, Allocator const& a) {
  941. return std::basic_string<CharT, Traits, Allocator>(v.begin(), v.end(), a);
  942. }
  943. #endif // nssv_CPP11_OR_GREATER
  944. template< class CharT, class Traits, class Allocator >
  945. basic_string_view<CharT, Traits>
  946. to_string_view(std::basic_string<CharT, Traits, Allocator> const& s) {
  947. return basic_string_view<CharT, Traits>(s.data(), s.size());
  948. }
  949. }
  950. } // namespace nonstd::sv_lite
  951. #endif // nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
  952. //
  953. // make types and algorithms available in namespace nonstd:
  954. //
  955. namespace nonstd {
  956. using sv_lite::basic_string_view;
  957. using sv_lite::string_view;
  958. using sv_lite::wstring_view;
  959. #if nssv_HAVE_WCHAR16_T
  960. using sv_lite::u16string_view;
  961. #endif
  962. #if nssv_HAVE_WCHAR32_T
  963. using sv_lite::u32string_view;
  964. #endif
  965. // literal "sv"
  966. using sv_lite::operator==;
  967. using sv_lite::operator!=;
  968. using sv_lite::operator<;
  969. using sv_lite::operator<=;
  970. using sv_lite::operator>;
  971. using sv_lite::operator>=;
  972. using sv_lite::operator<<;
  973. #if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
  974. using sv_lite::to_string;
  975. using sv_lite::to_string_view;
  976. #endif
  977. } // namespace nonstd
  978. // 24.4.5 Hash support (C++11):
  979. // Note: The hash value of a string view object is equal to the hash value of
  980. // the corresponding string object.
  981. #if nssv_HAVE_STD_HASH
  982. #include <functional>
  983. namespace std {
  984. template<>
  985. struct hash< nonstd::string_view > {
  986. public:
  987. std::size_t operator()(nonstd::string_view v) const nssv_noexcept {
  988. return std::hash<std::string>()(std::string(v.data(), v.size()));
  989. }
  990. };
  991. template<>
  992. struct hash< nonstd::wstring_view > {
  993. public:
  994. std::size_t operator()(nonstd::wstring_view v) const nssv_noexcept {
  995. return std::hash<std::wstring>()(std::wstring(v.data(), v.size()));
  996. }
  997. };
  998. template<>
  999. struct hash< nonstd::u16string_view > {
  1000. public:
  1001. std::size_t operator()(nonstd::u16string_view v) const nssv_noexcept {
  1002. return std::hash<std::u16string>()(std::u16string(v.data(), v.size()));
  1003. }
  1004. };
  1005. template<>
  1006. struct hash< nonstd::u32string_view > {
  1007. public:
  1008. std::size_t operator()(nonstd::u32string_view v) const nssv_noexcept {
  1009. return std::hash<std::u32string>()(std::u32string(v.data(), v.size()));
  1010. }
  1011. };
  1012. } // namespace std
  1013. #endif // nssv_HAVE_STD_HASH
  1014. nssv_RESTORE_WARNINGS()
  1015. #endif // nssv_HAVE_STD_STRING_VIEW
  1016. #endif // NONSTD_SV_LITE_H_INCLUDED