1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03, c++11
11 
12 // Note: libc++ supports string_view before C++17, but literals were introduced in C++14
13 
14 #include <string_view>
15 #include <cassert>
16 
17 #include "test_macros.h"
18 
19 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
20     typedef std::u8string_view u8string_view;
21 #else
22     typedef std::string_view   u8string_view;
23 #endif
24 
25 int main(int, char**)
26 {
27     using namespace std::literals::string_view_literals;
28 
29     static_assert ( std::is_same<decltype(   "Hi"sv), std::string_view>::value, "" );
30     static_assert ( std::is_same<decltype( u8"Hi"sv), u8string_view>::value, "" );
31     static_assert ( std::is_same<decltype(  L"Hi"sv), std::wstring_view>::value, "" );
32     static_assert ( std::is_same<decltype(  u"Hi"sv), std::u16string_view>::value, "" );
33     static_assert ( std::is_same<decltype(  U"Hi"sv), std::u32string_view>::value, "" );
34 
35     std::string_view foo;
36     std::wstring_view Lfoo;
37     u8string_view u8foo;
38     std::u16string_view ufoo;
39     std::u32string_view Ufoo;
40 
41 
42     foo  =    ""sv;     assert(  foo.size() == 0);
43     u8foo = u8""sv;     assert(u8foo.size() == 0);
44     Lfoo  =  L""sv;     assert( Lfoo.size() == 0);
45     ufoo  =  u""sv;     assert( ufoo.size() == 0);
46     Ufoo  =  U""sv;     assert( Ufoo.size() == 0);
47 
48     foo   =   " "sv;    assert(  foo.size() == 1);
49     u8foo = u8" "sv;    assert(u8foo.size() == 1);
50     Lfoo  =  L" "sv;    assert( Lfoo.size() == 1);
51     ufoo  =  u" "sv;    assert( ufoo.size() == 1);
52     Ufoo  =  U" "sv;    assert( Ufoo.size() == 1);
53 
54     foo   =   "ABC"sv;  assert(  foo ==   "ABC");   assert(  foo == std::string_view   (  "ABC"));
55     u8foo = u8"ABC"sv;  assert(u8foo == u8"ABC");   assert(u8foo == u8string_view      (u8"ABC"));
56     Lfoo  =  L"ABC"sv;  assert( Lfoo ==  L"ABC");   assert( Lfoo == std::wstring_view  ( L"ABC"));
57     ufoo  =  u"ABC"sv;  assert( ufoo ==  u"ABC");   assert( ufoo == std::u16string_view( u"ABC"));
58     Ufoo  =  U"ABC"sv;  assert( Ufoo ==  U"ABC");   assert( Ufoo == std::u32string_view( U"ABC"));
59 
60     static_assert(  "ABC"sv.size() == 3, "");
61     static_assert(u8"ABC"sv.size() == 3, "");
62     static_assert( L"ABC"sv.size() == 3, "");
63     static_assert( u"ABC"sv.size() == 3, "");
64     static_assert( U"ABC"sv.size() == 3, "");
65 
66     static_assert(noexcept(  "ABC"sv), "");
67     static_assert(noexcept(u8"ABC"sv), "");
68     static_assert(noexcept( L"ABC"sv), "");
69     static_assert(noexcept( u"ABC"sv), "");
70     static_assert(noexcept( U"ABC"sv), "");
71 
72   return 0;
73 }
74