string_span revision a14f27474f09e2c2b5360cb07d777b0ca6ee68b3
1///////////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4//
5// This code is licensed under the MIT License (MIT).
6//
7// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13// THE SOFTWARE.
14//
15///////////////////////////////////////////////////////////////////////////////
16
17#pragma once
18
19#ifndef GSL_STRING_SPAN_H
20#define GSL_STRING_SPAN_H
21
22#include "gsl_assert"
23#include "gsl_util"
24#include "span"
25#include <cstdint>
26#include <cstring>
27#include <string>
28
29#ifdef _MSC_VER
30
31// No MSVC does constexpr fully yet
32#pragma push_macro("constexpr")
33#define constexpr /*constexpr*/
34
35#pragma warning(push)
36
37// blanket turn off warnings from CppCoreCheck for now
38// so people aren't annoyed by them when running the tool.
39// more targeted suppressions will be added in a future update to the GSL
40#pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495)
41
42// VS 2013 workarounds
43#if _MSC_VER <= 1800
44
45#define GSL_MSVC_HAS_TYPE_DEDUCTION_BUG
46#define GSL_MSVC_HAS_SFINAE_SUBSTITUTION_ICE
47#define GSL_MSVC_NO_CPP14_STD_EQUAL
48#define GSL_MSVC_NO_DEFAULT_MOVE_CTOR
49
50// noexcept is not understood
51#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
52#pragma push_macro("noexcept")
53#define noexcept /*noexcept*/
54#endif
55
56#endif // _MSC_VER <= 1800
57#endif // _MSC_VER
58
59// In order to test the library, we need it to throw exceptions that we can catch
60#ifdef GSL_THROW_ON_CONTRACT_VIOLATION
61
62#ifdef _MSC_VER
63#pragma push_macro("noexcept")
64#endif
65
66#define noexcept /*noexcept*/
67
68#endif // GSL_THROW_ON_CONTRACT_VIOLATION
69
70namespace gsl
71{
72//
73// czstring and wzstring
74//
75// These are "tag" typedef's for C-style strings (i.e. null-terminated character arrays)
76// that allow static analysis to help find bugs.
77//
78// There are no additional features/semantics that we can find a way to add inside the
79// type system for these types that will not either incur significant runtime costs or
80// (sometimes needlessly) break existing programs when introduced.
81//
82
83template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
84using basic_zstring = CharT*;
85
86template <std::ptrdiff_t Extent = dynamic_extent>
87using czstring = basic_zstring<const char, Extent>;
88
89template <std::ptrdiff_t Extent = dynamic_extent>
90using cwzstring = basic_zstring<const wchar_t, Extent>;
91
92template <std::ptrdiff_t Extent = dynamic_extent>
93using zstring = basic_zstring<char, Extent>;
94
95template <std::ptrdiff_t Extent = dynamic_extent>
96using wzstring = basic_zstring<wchar_t, Extent>;
97
98namespace details
99{
100    inline std::ptrdiff_t string_length(const char *str, std::ptrdiff_t n)
101    {
102        if (str == nullptr || n <= 0)
103            return 0;
104
105        span<const char> str_span{str, n};
106
107        std::ptrdiff_t len = 0;
108        while (len < n && str_span[len])
109            len++;
110
111        return len;
112    }
113
114    inline std::ptrdiff_t wstring_length(const wchar_t *str, std::ptrdiff_t n)
115    {
116        if (str == nullptr || n <= 0)
117            return 0;
118
119        span<const wchar_t> str_span{str, n};
120
121        std::ptrdiff_t len = 0;
122        while (len < n && str_span[len])
123            len++;
124
125        return len;
126    }
127}
128
129//
130// ensure_sentinel()
131//
132// Provides a way to obtain an span from a contiguous sequence
133// that ends with a (non-inclusive) sentinel value.
134//
135// Will fail-fast if sentinel cannot be found before max elements are examined.
136//
137template <typename T, const T Sentinel>
138span<T, dynamic_extent> ensure_sentinel(T* seq, std::ptrdiff_t max = PTRDIFF_MAX)
139{
140    auto cur = seq;
141    while ((cur - seq) < max && *cur != Sentinel) ++cur;
142    Ensures(*cur == Sentinel);
143    return {seq, cur - seq};
144}
145
146//
147// ensure_z - creates a span for a czstring or cwzstring.
148// Will fail fast if a null-terminator cannot be found before
149// the limit of size_type.
150//
151template <typename T>
152inline span<T, dynamic_extent> ensure_z(T* const& sz, std::ptrdiff_t max = PTRDIFF_MAX)
153{
154    return ensure_sentinel<T, 0>(sz, max);
155}
156
157// TODO (neilmac) there is probably a better template-magic way to get the const and non-const
158// overloads to share an implementation
159inline span<char, dynamic_extent> ensure_z(char* const& sz, std::ptrdiff_t max)
160{
161    auto len = details::string_length(sz, max);
162    Ensures(sz[len] == 0);
163    return {sz, len};
164}
165
166inline span<const char, dynamic_extent> ensure_z(const char* const& sz, std::ptrdiff_t max)
167{
168    auto len = details::string_length(sz, max);
169    Ensures(sz[len] == 0);
170    return {sz, len};
171}
172
173inline span<wchar_t, dynamic_extent> ensure_z(wchar_t* const& sz, std::ptrdiff_t max)
174{
175    auto len = details::wstring_length(sz, max);
176    Ensures(sz[len] == 0);
177    return {sz, len};
178}
179
180inline span<const wchar_t, dynamic_extent> ensure_z(const wchar_t* const& sz, std::ptrdiff_t max)
181{
182    auto len = details::wstring_length(sz, max);
183    Ensures(sz[len] == 0);
184    return {sz, len};
185}
186
187template <typename T, size_t N>
188span<T, dynamic_extent> ensure_z(T (&sz)[N])
189{
190    return ensure_z(&sz[0], static_cast<std::ptrdiff_t>(N));
191}
192
193template <class Cont>
194span<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_extent>
195ensure_z(Cont& cont)
196{
197    return ensure_z(cont.data(), static_cast<std::ptrdiff_t>(cont.length()));
198}
199
200template <typename CharT, std::ptrdiff_t>
201class basic_string_span;
202
203namespace details
204{
205    template <typename T>
206    struct is_basic_string_span_oracle : std::false_type
207    {
208    };
209
210    template <typename CharT, std::ptrdiff_t Extent>
211    struct is_basic_string_span_oracle<basic_string_span<CharT, Extent>> : std::true_type
212    {
213    };
214
215    template <typename T>
216    struct is_basic_string_span : is_basic_string_span_oracle<std::remove_cv_t<T>>
217    {
218    };
219
220    template <typename T>
221    struct length_func
222    {
223    };
224
225    template <>
226    struct length_func<char>
227    {
228        std::ptrdiff_t operator()(char* const ptr, std::ptrdiff_t length) noexcept
229        {
230            return details::string_length(ptr, length);
231        }
232    };
233
234    template <>
235    struct length_func<wchar_t>
236    {
237        std::ptrdiff_t operator()(wchar_t* const ptr, std::ptrdiff_t length) noexcept
238        {
239            return details::wstring_length(ptr, length);
240        }
241    };
242
243    template <>
244    struct length_func<const char>
245    {
246        std::ptrdiff_t operator()(const char* const ptr, std::ptrdiff_t length) noexcept
247        {
248            return details::string_length(ptr, length);
249        }
250    };
251
252    template <>
253    struct length_func<const wchar_t>
254    {
255        std::ptrdiff_t operator()(const wchar_t* const ptr, std::ptrdiff_t length) noexcept
256        {
257            return details::wstring_length(ptr, length);
258        }
259    };
260}
261
262//
263// string_span and relatives
264//
265template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
266class basic_string_span
267{
268public:
269    using element_type = CharT;
270    using pointer = std::add_pointer_t<element_type>;
271    using reference = std::add_lvalue_reference_t<element_type>;
272    using const_reference = std::add_lvalue_reference_t<std::add_const_t<element_type>>;
273    using impl_type = span<element_type, Extent>;
274
275    using index_type = typename impl_type::index_type;
276    using iterator = typename impl_type::iterator;
277    using const_iterator = typename impl_type::const_iterator;
278    using reverse_iterator = typename impl_type::reverse_iterator;
279    using const_reverse_iterator = typename impl_type::const_reverse_iterator;
280
281    // default (empty)
282    constexpr basic_string_span() noexcept = default;
283
284    // copy
285    constexpr basic_string_span(const basic_string_span& other) noexcept = default;
286
287// move
288#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
289    constexpr basic_string_span(basic_string_span&& other) noexcept = default;
290#else
291    constexpr basic_string_span(basic_string_span&& other) : span_(std::move(other.span_)) {}
292#endif
293
294    // assign
295    constexpr basic_string_span& operator=(const basic_string_span& other) noexcept = default;
296
297// move assign
298#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
299    constexpr basic_string_span& operator=(basic_string_span&& other) noexcept = default;
300#else
301    constexpr basic_string_span& operator=(basic_string_span&& other) noexcept
302    {
303        span_ = std::move(other.span_);
304        return *this;
305    }
306#endif
307
308    // from nullptr
309    constexpr basic_string_span(std::nullptr_t ptr) noexcept : span_(ptr) {}
310
311    constexpr basic_string_span(pointer ptr, index_type length) : span_(ptr, length) {}
312    constexpr basic_string_span(pointer firstElem, pointer lastElem) : span_(firstElem, lastElem) {}
313
314    // From static arrays - if 0-terminated, remove 0 from the view
315    // All other containers allow 0s within the length, so we do not remove them
316    template <size_t N>
317    constexpr basic_string_span(element_type (&arr)[N]) : span_(remove_z(arr))
318    {
319    }
320
321    template <size_t N, class ArrayElementType = std::remove_const_t<element_type>>
322    constexpr basic_string_span(std::array<ArrayElementType, N>& arr) noexcept : span_(arr)
323    {
324    }
325
326    template <size_t N, class ArrayElementType = std::remove_const_t<element_type>>
327    constexpr basic_string_span(const std::array<ArrayElementType, N>& arr) noexcept : span_(arr)
328    {
329    }
330
331    // Container signature should work for basic_string after C++17 version exists
332    template <class Traits, class Allocator>
333    constexpr basic_string_span(std::basic_string<element_type, Traits, Allocator>& str)
334        : span_(&str[0], str.length())
335    {
336    }
337
338    template <class Traits, class Allocator>
339    constexpr basic_string_span(const std::basic_string<element_type, Traits, Allocator>& str)
340        : span_(&str[0], str.length())
341    {
342    }
343
344    // from containers. Containers must have a pointer type and data() function signatures
345    template <class Container,
346              class = std::enable_if_t<
347                  !details::is_basic_string_span<Container>::value &&
348                  std::is_convertible<typename Container::pointer, pointer>::value &&
349                  std::is_convertible<typename Container::pointer,
350                                      decltype(std::declval<Container>().data())>::value>>
351    constexpr basic_string_span(Container& cont) : span_(cont)
352    {
353    }
354
355    template <class Container,
356              class = std::enable_if_t<
357                  !details::is_basic_string_span<Container>::value &&
358                  std::is_convertible<typename Container::pointer, pointer>::value &&
359                  std::is_convertible<typename Container::pointer,
360                                      decltype(std::declval<Container>().data())>::value>>
361    constexpr basic_string_span(const Container& cont) : span_(cont)
362    {
363    }
364
365    // from string_span
366    template <
367        class OtherValueType, std::ptrdiff_t OtherExtent,
368        class = std::enable_if_t<std::is_convertible<
369            typename basic_string_span<OtherValueType, OtherExtent>::impl_type, impl_type>::value>>
370    constexpr basic_string_span(basic_string_span<OtherValueType, OtherExtent> other)
371        : span_(other.data(), other.length())
372    {
373    }
374
375    template <index_type Count>
376    constexpr basic_string_span<element_type, Count> first() const
377    {
378        return {span_.template first<Count>()};
379    }
380
381    constexpr basic_string_span<element_type, dynamic_extent> first(index_type count) const
382    {
383        return {span_.first(count)};
384    }
385
386    template <index_type Count>
387    constexpr basic_string_span<element_type, Count> last() const
388    {
389        return {span_.template last<Count>()};
390    }
391
392    constexpr basic_string_span<element_type, dynamic_extent> last(index_type count) const
393    {
394        return {span_.last(count)};
395    }
396
397    template <index_type Offset, index_type Count>
398    constexpr basic_string_span<element_type, Count> subspan() const
399    {
400        return {span_.template subspan<Offset, Count>()};
401    }
402
403    constexpr basic_string_span<element_type, dynamic_extent>
404    subspan(index_type offset, index_type count = dynamic_extent) const
405    {
406        return {span_.subspan(offset, count)};
407    }
408
409    constexpr reference operator[](index_type idx) const { return span_[idx]; }
410    constexpr reference operator()(index_type idx) const { return span_[idx]; }
411
412    constexpr pointer data() const { return span_.data(); }
413
414    constexpr index_type length() const noexcept { return span_.size(); }
415    constexpr index_type size() const noexcept { return span_.size(); }
416    constexpr index_type size_bytes() const noexcept { return span_.size_bytes(); }
417    constexpr index_type length_bytes() const noexcept { return span_.length_bytes(); }
418    constexpr bool empty() const noexcept { return size() == 0; }
419
420    constexpr iterator begin() const noexcept { return span_.begin(); }
421    constexpr iterator end() const noexcept { return span_.end(); }
422
423    constexpr const_iterator cbegin() const noexcept { return span_.cbegin(); }
424    constexpr const_iterator cend() const noexcept { return span_.cend(); }
425
426    constexpr reverse_iterator rbegin() const noexcept { return span_.rbegin(); }
427    constexpr reverse_iterator rend() const noexcept { return span_.rend(); }
428
429    constexpr const_reverse_iterator crbegin() const noexcept { return span_.crbegin(); }
430    constexpr const_reverse_iterator crend() const noexcept { return span_.crend(); }
431
432private:
433    static impl_type remove_z(pointer const& sz, std::ptrdiff_t max)
434    {
435        return {sz, details::length_func<element_type>()(sz, max)};
436    }
437
438    template <size_t N>
439    static impl_type remove_z(element_type (&sz)[N])
440    {
441        return remove_z(&sz[0], narrow_cast<std::ptrdiff_t>(N));
442    }
443
444    impl_type span_;
445};
446
447template <std::ptrdiff_t Extent = dynamic_extent>
448using string_span = basic_string_span<char, Extent>;
449
450template <std::ptrdiff_t Extent = dynamic_extent>
451using cstring_span = basic_string_span<const char, Extent>;
452
453template <std::ptrdiff_t Extent = dynamic_extent>
454using wstring_span = basic_string_span<wchar_t, Extent>;
455
456template <std::ptrdiff_t Extent = dynamic_extent>
457using cwstring_span = basic_string_span<const wchar_t, Extent>;
458
459//
460// to_string() allow (explicit) conversions from string_span to string
461//
462#ifndef GSL_MSVC_HAS_TYPE_DEDUCTION_BUG
463
464template <typename CharT, std::ptrdiff_t Extent>
465std::basic_string<typename std::remove_const<CharT>::type>
466to_string(basic_string_span<CharT, Extent> view)
467{
468    return {view.data(), static_cast<size_t>(view.length())};
469}
470
471#else
472
473inline std::string to_string(cstring_span<> view)
474{
475    return {view.data(), static_cast<size_t>(view.length())};
476}
477
478inline std::string to_string(string_span<> view)
479{
480    return {view.data(), static_cast<size_t>(view.length())};
481}
482
483inline std::wstring to_string(cwstring_span<> view)
484{
485    return {view.data(), static_cast<size_t>(view.length())};
486}
487
488inline std::wstring to_string(wstring_span<> view)
489{
490    return {view.data(), static_cast<size_t>(view.length())};
491}
492
493#endif
494
495template <typename CharT,
496	  typename Traits = typename std::char_traits<CharT>,
497	  typename Allocator = std::allocator<CharT>,
498	  typename gCharT,
499	  std::ptrdiff_t Extent>
500std::basic_string<CharT, Traits, Allocator> to_basic_string(basic_string_span<gCharT, Extent> view)
501{
502  return {view.data(), static_cast<size_t>(view.length())};
503}
504  
505// zero-terminated string span, used to convert
506// zero-terminated spans to legacy strings
507template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
508class basic_zstring_span
509{
510public:
511    using value_type = CharT;
512    using const_value_type = std::add_const_t<CharT>;
513
514    using pointer = std::add_pointer_t<value_type>;
515    using const_pointer = std::add_pointer_t<const_value_type>;
516
517    using zstring_type = basic_zstring<value_type, Extent>;
518    using const_zstring_type = basic_zstring<const_value_type, Extent>;
519
520    using impl_type = span<value_type, Extent>;
521    using string_span_type = basic_string_span<value_type, Extent>;
522
523    constexpr basic_zstring_span(impl_type s) noexcept : span_(s)
524    {
525        // expects a zero-terminated span
526        Expects(s[s.size() - 1] == '\0');
527    }
528
529    // copy
530    constexpr basic_zstring_span(const basic_zstring_span& other) = default;
531
532// move
533#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
534    constexpr basic_zstring_span(basic_zstring_span&& other) = default;
535#else
536    constexpr basic_zstring_span(basic_zstring_span&& other) : span_(std::move(other.span_)) {}
537#endif
538
539    // assign
540    constexpr basic_zstring_span& operator=(const basic_zstring_span& other) = default;
541
542// move assign
543#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
544    constexpr basic_zstring_span& operator=(basic_zstring_span&& other) = default;
545#else
546    constexpr basic_zstring_span& operator=(basic_zstring_span&& other)
547    {
548        span_ = std::move(other.span_);
549        return *this;
550    }
551#endif
552
553    constexpr bool empty() const noexcept { return span_.size() == 0; }
554
555    constexpr string_span_type as_string_span() const noexcept
556    {
557        return span_.first(span_.size() - 1);
558    }
559
560    constexpr string_span_type ensure_z() const noexcept { return gsl::ensure_z(span_); }
561
562    constexpr const_zstring_type assume_z() const noexcept { return span_.data(); }
563
564private:
565    impl_type span_;
566};
567
568template <std::ptrdiff_t Max = dynamic_extent>
569using zstring_span = basic_zstring_span<char, Max>;
570
571template <std::ptrdiff_t Max = dynamic_extent>
572using wzstring_span = basic_zstring_span<wchar_t, Max>;
573
574template <std::ptrdiff_t Max = dynamic_extent>
575using czstring_span = basic_zstring_span<const char, Max>;
576
577template <std::ptrdiff_t Max = dynamic_extent>
578using cwzstring_span = basic_zstring_span<const wchar_t, Max>;
579
580// operator ==
581template <class CharT, std::ptrdiff_t Extent, class T,
582          class = std::enable_if_t<
583              details::is_basic_string_span<T>::value ||
584              std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
585bool operator==(const gsl::basic_string_span<CharT, Extent>& one, const T& other) noexcept
586{
587    gsl::basic_string_span<std::add_const_t<CharT>> tmp(other);
588#ifdef GSL_MSVC_NO_CPP14_STD_EQUAL
589    return (one.size() == tmp.size()) && std::equal(one.begin(), one.end(), tmp.begin());
590#else
591    return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end());
592#endif
593}
594
595template <class CharT, std::ptrdiff_t Extent, class T,
596          class = std::enable_if_t<
597              !details::is_basic_string_span<T>::value &&
598              std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
599bool operator==(const T& one, const gsl::basic_string_span<CharT, Extent>& other) noexcept
600{
601    gsl::basic_string_span<std::add_const_t<CharT>> tmp(one);
602#ifdef GSL_MSVC_NO_CPP14_STD_EQUAL
603    return (tmp.size() == other.size()) && std::equal(tmp.begin(), tmp.end(), other.begin());
604#else
605    return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end());
606#endif
607}
608
609// operator !=
610template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
611          typename = std::enable_if_t<std::is_convertible<
612              T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
613bool operator!=(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
614{
615    return !(one == other);
616}
617
618template <
619    typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
620    typename Dummy = std::enable_if_t<
621        std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
622        !gsl::details::is_basic_string_span<T>::value>>
623bool operator!=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
624{
625    return !(one == other);
626}
627
628// operator<
629template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
630          typename = std::enable_if_t<std::is_convertible<
631              T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
632bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
633{
634    gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
635    return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end());
636}
637
638template <
639    typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
640    typename Dummy = std::enable_if_t<
641        std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
642        !gsl::details::is_basic_string_span<T>::value>>
643bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
644{
645    gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
646    return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end());
647}
648
649#ifndef _MSC_VER
650
651// VS treats temp and const containers as convertible to basic_string_span,
652// so the cases below are already covered by the previous operators
653
654template <
655    typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
656    typename DataType = typename T::value_type,
657    typename Dummy = std::enable_if_t<
658        !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
659        std::is_convertible<DataType*, CharT*>::value &&
660        std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
661                     DataType>::value>>
662bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
663{
664    gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
665    return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end());
666}
667
668template <
669    typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
670    typename DataType = typename T::value_type,
671    typename Dummy = std::enable_if_t<
672        !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
673        std::is_convertible<DataType*, CharT*>::value &&
674        std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
675                     DataType>::value>>
676bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
677{
678    gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
679    return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end());
680}
681#endif
682
683// operator <=
684template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
685          typename = std::enable_if_t<std::is_convertible<
686              T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
687bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
688{
689    return !(other < one);
690}
691
692template <
693    typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
694    typename Dummy = std::enable_if_t<
695        std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
696        !gsl::details::is_basic_string_span<T>::value>>
697bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
698{
699    return !(other < one);
700}
701
702#ifndef _MSC_VER
703
704// VS treats temp and const containers as convertible to basic_string_span,
705// so the cases below are already covered by the previous operators
706
707template <
708    typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
709    typename DataType = typename T::value_type,
710    typename Dummy = std::enable_if_t<
711        !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
712        std::is_convertible<DataType*, CharT*>::value &&
713        std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
714                     DataType>::value>>
715bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
716{
717    return !(other < one);
718}
719
720template <
721    typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
722    typename DataType = typename T::value_type,
723    typename Dummy = std::enable_if_t<
724        !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
725        std::is_convertible<DataType*, CharT*>::value &&
726        std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
727                     DataType>::value>>
728bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
729{
730    return !(other < one);
731}
732#endif
733
734// operator>
735template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
736          typename = std::enable_if_t<std::is_convertible<
737              T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
738bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
739{
740    return other < one;
741}
742
743template <
744    typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
745    typename Dummy = std::enable_if_t<
746        std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
747        !gsl::details::is_basic_string_span<T>::value>>
748bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
749{
750    return other < one;
751}
752
753#ifndef _MSC_VER
754
755// VS treats temp and const containers as convertible to basic_string_span,
756// so the cases below are already covered by the previous operators
757
758template <
759    typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
760    typename DataType = typename T::value_type,
761    typename Dummy = std::enable_if_t<
762        !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
763        std::is_convertible<DataType*, CharT*>::value &&
764        std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
765                     DataType>::value>>
766bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
767{
768    return other < one;
769}
770
771template <
772    typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
773    typename DataType = typename T::value_type,
774    typename Dummy = std::enable_if_t<
775        !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
776        std::is_convertible<DataType*, CharT*>::value &&
777        std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
778                     DataType>::value>>
779bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
780{
781    return other < one;
782}
783#endif
784
785// operator >=
786template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
787          typename = std::enable_if_t<std::is_convertible<
788              T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
789bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
790{
791    return !(one < other);
792}
793
794template <
795    typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
796    typename Dummy = std::enable_if_t<
797        std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
798        !gsl::details::is_basic_string_span<T>::value>>
799bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
800{
801    return !(one < other);
802}
803
804#ifndef _MSC_VER
805
806// VS treats temp and const containers as convertible to basic_string_span,
807// so the cases below are already covered by the previous operators
808
809template <
810    typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
811    typename DataType = typename T::value_type,
812    typename Dummy = std::enable_if_t<
813        !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
814        std::is_convertible<DataType*, CharT*>::value &&
815        std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
816                     DataType>::value>>
817bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
818{
819    return !(one < other);
820}
821
822template <
823    typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
824    typename DataType = typename T::value_type,
825    typename Dummy = std::enable_if_t<
826        !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
827        std::is_convertible<DataType*, CharT*>::value &&
828        std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
829                     DataType>::value>>
830bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
831{
832    return !(one < other);
833}
834#endif
835} // namespace GSL
836
837#ifdef _MSC_VER
838
839#pragma warning(pop)
840
841#undef constexpr
842#pragma pop_macro("constexpr")
843
844// VS 2013 workarounds
845#if _MSC_VER <= 1800
846
847#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
848#undef noexcept
849#pragma pop_macro("noexcept")
850#endif // GSL_THROW_ON_CONTRACT_VIOLATION
851
852#undef GSL_MSVC_HAS_TYPE_DEDUCTION_BUG
853#undef GSL_MSVC_HAS_SFINAE_SUBSTITUTION_ICE
854#undef GSL_MSVC_NO_CPP14_STD_EQUAL
855#undef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
856
857#endif // _MSC_VER <= 1800
858#endif // _MSC_VER
859
860#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
861
862#undef noexcept
863
864#ifdef _MSC_VER
865#pragma pop_macro("noexcept")
866#endif
867
868#endif // GSL_THROW_ON_CONTRACT_VIOLATION
869#endif // GSL_STRING_SPAN_H
870