1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14) 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 <string_view> 20 #include <algorithm> 21 #include <stdexcept> 22 #include <cassert> 23 24 #include "test_macros.h" 25 26 template<typename CharT> 27 void test1(std::basic_string_view<CharT> sv, size_t n, size_t pos) { 28 std::basic_string_view<CharT> sv1; 29 #ifdef TEST_HAS_NO_EXCEPTIONS 30 if (pos > sv.size()) 31 return ; // would throw if exceptions were enabled 32 sv1 = sv.substr(pos, n); 33 #else 34 try { 35 sv1 = sv.substr(pos, n); 36 assert(pos <= sv.size()); 37 } 38 catch (const std::out_of_range&) { 39 assert(pos > sv.size()); 40 return ; 41 } 42 #endif 43 const size_t rlen = std::min(n, sv.size() - pos); 44 assert (sv1.size() == rlen); 45 for (size_t i = 0; i < rlen; ++i) 46 assert(sv[pos+i] == sv1[i]); 47 } 48 49 50 template<typename CharT> 51 void test ( const CharT *s ) { 52 typedef std::basic_string_view<CharT> string_view_t; 53 54 string_view_t sv1 ( s ); 55 56 test1(sv1, 0, 0); 57 test1(sv1, 1, 0); 58 test1(sv1, 20, 0); 59 test1(sv1, sv1.size(), 0); 60 61 test1(sv1, 0, 3); 62 test1(sv1, 2, 3); 63 test1(sv1, 100, 3); 64 65 test1(sv1, 0, string_view_t::npos); 66 test1(sv1, 2, string_view_t::npos); 67 test1(sv1, sv1.size(), string_view_t::npos); 68 69 test1(sv1, sv1.size() + 1, 0); 70 test1(sv1, sv1.size() + 1, 1); 71 test1(sv1, sv1.size() + 1, string_view_t::npos); 72 } 73 74 int main(int, char**) { 75 test ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); 76 test ( "ABCDE"); 77 test ( "a" ); 78 test ( "" ); 79 80 test ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); 81 test ( L"ABCDE" ); 82 test ( L"a" ); 83 test ( L"" ); 84 85 #if TEST_STD_VER >= 11 86 test ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); 87 test ( u"ABCDE" ); 88 test ( u"a" ); 89 test ( u"" ); 90 91 test ( U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); 92 test ( U"ABCDE" ); 93 test ( U"a" ); 94 test ( U"" ); 95 #endif 96 97 #if TEST_STD_VER > 11 98 { 99 constexpr std::string_view sv1 { "ABCDE", 5 }; 100 101 { 102 constexpr std::string_view sv2 = sv1.substr ( 0, 3 ); 103 static_assert ( sv2.size() == 3, "" ); 104 static_assert ( sv2[0] == 'A', "" ); 105 static_assert ( sv2[1] == 'B', "" ); 106 static_assert ( sv2[2] == 'C', "" ); 107 } 108 109 { 110 constexpr std::string_view sv2 = sv1.substr ( 3, 0 ); 111 static_assert ( sv2.size() == 0, "" ); 112 } 113 114 { 115 constexpr std::string_view sv2 = sv1.substr ( 3, 3 ); 116 static_assert ( sv2.size() == 2, "" ); 117 static_assert ( sv2[0] == 'D', "" ); 118 static_assert ( sv2[1] == 'E', "" ); 119 } 120 } 121 #endif 122 123 return 0; 124 } 125