• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <string_view>
11 
12 // constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
13 
14 // Throws: out_of_range if pos > size().
15 // Effects: Determines the effective length rlen of the string to reference as the smaller of n and size() - pos.
16 // Returns: basic_string_view(data()+pos, rlen).
17 
18 #include <string_view>
19 #include <algorithm>
20 #include <stdexcept>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 
25 template<typename CharT>
test1(std::basic_string_view<CharT> sv,size_t n,size_t pos)26 void test1(std::basic_string_view<CharT> sv, size_t n, size_t pos) {
27     std::basic_string_view<CharT> sv1;
28 #ifdef TEST_HAS_NO_EXCEPTIONS
29     if (pos > sv.size())
30         return ;  // would throw if exceptions were enabled
31     sv1 = sv.substr(pos, n);
32 #else
33     try {
34         sv1 = sv.substr(pos, n);
35         assert(pos <= sv.size());
36     }
37     catch (const std::out_of_range&) {
38         assert(pos > sv.size());
39         return ;
40     }
41 #endif
42     const size_t rlen = std::min(n, sv.size() - pos);
43     assert (sv1.size() == rlen);
44     for (size_t i = 0; i < rlen; ++i)
45         assert(sv[pos+i] == sv1[i]);
46 }
47 
48 
49 template<typename CharT>
test(const CharT * s)50 void test ( const CharT *s ) {
51     typedef std::basic_string_view<CharT> string_view_t;
52 
53     string_view_t sv1 ( s );
54 
55     test1(sv1,  0, 0);
56     test1(sv1,  1, 0);
57     test1(sv1, 20, 0);
58     test1(sv1, sv1.size(), 0);
59 
60     test1(sv1,   0, 3);
61     test1(sv1,   2, 3);
62     test1(sv1, 100, 3);
63 
64     test1(sv1, 0, string_view_t::npos);
65     test1(sv1, 2, string_view_t::npos);
66     test1(sv1, sv1.size(), string_view_t::npos);
67 
68     test1(sv1, sv1.size() + 1, 0);
69     test1(sv1, sv1.size() + 1, 1);
70     test1(sv1, sv1.size() + 1, string_view_t::npos);
71 }
72 
main()73 int main () {
74     test ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
75     test ( "ABCDE");
76     test ( "a" );
77     test ( "" );
78 
79     test ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
80     test ( L"ABCDE" );
81     test ( L"a" );
82     test ( L"" );
83 
84 #if TEST_STD_VER >= 11
85     test ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
86     test ( u"ABCDE" );
87     test ( u"a" );
88     test ( u"" );
89 
90     test ( U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
91     test ( U"ABCDE" );
92     test ( U"a" );
93     test ( U"" );
94 #endif
95 
96 #if TEST_STD_VER > 11
97     {
98     constexpr std::string_view sv1 { "ABCDE", 5 };
99 
100     {
101     constexpr std::string_view sv2 = sv1.substr ( 0, 3 );
102     static_assert ( sv2.size() == 3, "" );
103     static_assert ( sv2[0] == 'A', "" );
104     static_assert ( sv2[1] == 'B', "" );
105     static_assert ( sv2[2] == 'C', "" );
106     }
107 
108     {
109     constexpr std::string_view sv2 = sv1.substr ( 3, 0 );
110     static_assert ( sv2.size() == 0, "" );
111     }
112 
113     {
114     constexpr std::string_view sv2 = sv1.substr ( 3, 3 );
115     static_assert ( sv2.size() == 2, "" );
116     static_assert ( sv2[0] == 'D', "" );
117     static_assert ( sv2[1] == 'E', "" );
118     }
119     }
120 #endif
121 }
122