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
11 // <string_view>
12
13 // constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
14
15 // Throws: out_of_range if pos > size().
16 // Effects: Determines the effective length rlen of the string to reference as the smaller of n and size() - pos.
17 // Returns: basic_string_view(data()+pos, rlen).
18
19 #include <experimental/string_view>
20 #include <cassert>
21
22 template<typename CharT>
test1(std::experimental::basic_string_view<CharT> sv,size_t n,size_t pos)23 void test1 ( std::experimental::basic_string_view<CharT> sv, size_t n, size_t pos ) {
24 try {
25 std::experimental::basic_string_view<CharT> sv1 = sv.substr(pos, n);
26 const size_t rlen = std::min ( n, sv.size() - pos );
27 assert ( sv1.size() == rlen );
28 for ( size_t i = 0; i <= rlen; ++i )
29 assert ( sv[pos+i] == sv1[i] );
30 }
31 catch ( const std::out_of_range & ) { assert ( pos > sv.size()); }
32 }
33
34
35 template<typename CharT>
test(const CharT * s)36 void test ( const CharT *s ) {
37 typedef std::experimental::basic_string_view<CharT> string_view_t;
38
39 string_view_t sv1 ( s );
40
41 test1(sv1, 0, 0);
42 test1(sv1, 1, 0);
43 test1(sv1, 20, 0);
44 test1(sv1, sv1.size(), 0);
45
46 test1(sv1, 0, 3);
47 test1(sv1, 2, 3);
48 test1(sv1, 100, 3);
49
50 test1(sv1, 0, string_view_t::npos);
51 test1(sv1, 2, string_view_t::npos);
52 test1(sv1, sv1.size(), string_view_t::npos);
53
54 test1(sv1, sv1.size() + 1, 0);
55 test1(sv1, sv1.size() + 1, 1);
56 test1(sv1, sv1.size() + 1, string_view_t::npos);
57 }
58
main()59 int main () {
60 test ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
61 test ( "ABCDE");
62 test ( "a" );
63 test ( "" );
64
65 test ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
66 test ( L"ABCDE" );
67 test ( L"a" );
68 test ( L"" );
69
70 #if __cplusplus >= 201103L
71 test ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
72 test ( u"ABCDE" );
73 test ( u"a" );
74 test ( u"" );
75
76 test ( U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
77 test ( U"ABCDE" );
78 test ( U"a" );
79 test ( U"" );
80 #endif
81
82 #if _LIBCPP_STD_VER > 11
83 {
84 constexpr std::experimental::string_view sv1 { "ABCDE", 5 };
85
86 {
87 constexpr std::experimental::string_view sv2 = sv1.substr ( 0, 3 );
88 static_assert ( sv2.size() == 3, "" );
89 static_assert ( sv2[0] == 'A', "" );
90 static_assert ( sv2[1] == 'B', "" );
91 static_assert ( sv2[2] == 'C', "" );
92 }
93
94 {
95 constexpr std::experimental::string_view sv2 = sv1.substr ( 3, 0 );
96 static_assert ( sv2.size() == 0, "" );
97 }
98
99 {
100 constexpr std::experimental::string_view sv2 = sv1.substr ( 3, 3 );
101 static_assert ( sv2.size() == 2, "" );
102 static_assert ( sv2[0] == 'D', "" );
103 static_assert ( sv2[1] == 'E', "" );
104 }
105 }
106 #endif
107 }
108