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: c++03, c++11 10 11 // Note: libc++ supports string_view before C++17, but literals were introduced in C++14 12 13 #include <string_view> 14 #include <cassert> 15 16 #include "test_macros.h" 17 18 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L 19 typedef std::u8string_view u8string_view; 20 #else 21 typedef std::string_view u8string_view; 22 #endif 23 24 int main(int, char**) { 25 { 26 using namespace std::literals::string_view_literals; 27 28 ASSERT_SAME_TYPE(decltype( "Hi"sv), std::string_view); 29 ASSERT_SAME_TYPE(decltype(u8"Hi"sv), u8string_view); 30 ASSERT_SAME_TYPE(decltype( L"Hi"sv), std::wstring_view); 31 ASSERT_SAME_TYPE(decltype( u"Hi"sv), std::u16string_view); 32 ASSERT_SAME_TYPE(decltype( U"Hi"sv), std::u32string_view); 33 34 std::string_view foo; 35 std::wstring_view Lfoo; 36 u8string_view u8foo; 37 std::u16string_view ufoo; 38 std::u32string_view Ufoo; 39 40 foo = ""sv; assert( foo.size() == 0); 41 u8foo = u8""sv; assert(u8foo.size() == 0); 42 Lfoo = L""sv; assert( Lfoo.size() == 0); 43 ufoo = u""sv; assert( ufoo.size() == 0); 44 Ufoo = U""sv; assert( Ufoo.size() == 0); 45 46 foo = " "sv; assert( foo.size() == 1); 47 u8foo = u8" "sv; assert(u8foo.size() == 1); 48 Lfoo = L" "sv; assert( Lfoo.size() == 1); 49 ufoo = u" "sv; assert( ufoo.size() == 1); 50 Ufoo = U" "sv; assert( Ufoo.size() == 1); 51 52 foo = "ABC"sv; assert( foo == "ABC"); assert( foo == std::string_view ( "ABC")); 53 u8foo = u8"ABC"sv; assert(u8foo == u8"ABC"); assert(u8foo == u8string_view (u8"ABC")); 54 Lfoo = L"ABC"sv; assert( Lfoo == L"ABC"); assert( Lfoo == std::wstring_view ( L"ABC")); 55 ufoo = u"ABC"sv; assert( ufoo == u"ABC"); assert( ufoo == std::u16string_view( u"ABC")); 56 Ufoo = U"ABC"sv; assert( Ufoo == U"ABC"); assert( Ufoo == std::u32string_view( U"ABC")); 57 58 static_assert( "ABC"sv.size() == 3, ""); 59 static_assert(u8"ABC"sv.size() == 3, ""); 60 static_assert( L"ABC"sv.size() == 3, ""); 61 static_assert( u"ABC"sv.size() == 3, ""); 62 static_assert( U"ABC"sv.size() == 3, ""); 63 64 ASSERT_NOEXCEPT( "ABC"sv); 65 ASSERT_NOEXCEPT(u8"ABC"sv); 66 ASSERT_NOEXCEPT( L"ABC"sv); 67 ASSERT_NOEXCEPT( u"ABC"sv); 68 ASSERT_NOEXCEPT( U"ABC"sv); 69 } 70 { 71 using namespace std::literals; 72 std::string_view foo = ""sv; 73 assert(foo.length() == 0); 74 } 75 { 76 using namespace std; 77 std::string_view foo = ""sv; 78 assert(foo.length() == 0); 79 } 80 81 return 0; 82 } 83