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 // Note: libc++ supports string_view before C++17, but literals were introduced in C++14
10 // UNSUPPORTED: c++03, c++11
11 // UNSUPPORTED: !stdlib=libc++ && 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 
main(int,char **)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 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
31         ASSERT_SAME_TYPE(decltype( L"Hi"sv), std::wstring_view);
32 #endif
33         ASSERT_SAME_TYPE(decltype( u"Hi"sv), std::u16string_view);
34         ASSERT_SAME_TYPE(decltype( U"Hi"sv), std::u32string_view);
35 
36         std::string_view foo;
37 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
38         std::wstring_view Lfoo;
39 #endif
40         u8string_view u8foo;
41         std::u16string_view ufoo;
42         std::u32string_view Ufoo;
43 
44         foo  =    ""sv;     assert(  foo.size() == 0);
45         u8foo = u8""sv;     assert(u8foo.size() == 0);
46 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
47         Lfoo  =  L""sv;     assert( Lfoo.size() == 0);
48 #endif
49         ufoo  =  u""sv;     assert( ufoo.size() == 0);
50         Ufoo  =  U""sv;     assert( Ufoo.size() == 0);
51 
52         foo   =   " "sv;    assert(  foo.size() == 1);
53         u8foo = u8" "sv;    assert(u8foo.size() == 1);
54 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
55         Lfoo  =  L" "sv;    assert( Lfoo.size() == 1);
56 #endif
57         ufoo  =  u" "sv;    assert( ufoo.size() == 1);
58         Ufoo  =  U" "sv;    assert( Ufoo.size() == 1);
59 
60         foo   =   "ABC"sv;  assert(  foo ==   "ABC");   assert(  foo == std::string_view   (  "ABC"));
61         u8foo = u8"ABC"sv;  assert(u8foo == u8"ABC");   assert(u8foo == u8string_view      (u8"ABC"));
62 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
63         Lfoo  =  L"ABC"sv;  assert( Lfoo ==  L"ABC");   assert( Lfoo == std::wstring_view  ( L"ABC"));
64 #endif
65         ufoo  =  u"ABC"sv;  assert( ufoo ==  u"ABC");   assert( ufoo == std::u16string_view( u"ABC"));
66         Ufoo  =  U"ABC"sv;  assert( Ufoo ==  U"ABC");   assert( Ufoo == std::u32string_view( U"ABC"));
67 
68         static_assert(  "ABC"sv.size() == 3, "");
69         static_assert(u8"ABC"sv.size() == 3, "");
70 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
71         static_assert( L"ABC"sv.size() == 3, "");
72 #endif
73         static_assert( u"ABC"sv.size() == 3, "");
74         static_assert( U"ABC"sv.size() == 3, "");
75 
76         ASSERT_NOEXCEPT(  "ABC"sv);
77         ASSERT_NOEXCEPT(u8"ABC"sv);
78 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
79         ASSERT_NOEXCEPT( L"ABC"sv);
80 #endif
81         ASSERT_NOEXCEPT( u"ABC"sv);
82         ASSERT_NOEXCEPT( U"ABC"sv);
83     }
84     {
85         using namespace std::literals;
86         std::string_view foo = ""sv;
87         assert(foo.length() == 0);
88     }
89     {
90         using namespace std;
91         std::string_view foo = ""sv;
92         assert(foo.length() == 0);
93     }
94 
95     return 0;
96 }
97