1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 // UNSUPPORTED: c++98, c++03, c++11
12 // UNSUPPORTED: clang-3.3, clang-3.4, clang-3.5, clang-3.6, clang-3.7, clang-3.8, clang-3.9
13 // UNSUPPORTED: apple-clang-6, apple-clang-7, apple-clang-8
14 // Note: libc++ supports string_view before C++17, but literals were introduced in C++14
15 
16 #include <string_view>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 
21 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
22     typedef std::u8string_view u8string_view;
23 #else
24     typedef std::string_view   u8string_view;
25 #endif
26 
main()27 int main()
28 {
29     using namespace std::literals::string_view_literals;
30 
31     static_assert ( std::is_same<decltype(   "Hi"sv), std::string_view>::value, "" );
32     static_assert ( std::is_same<decltype( u8"Hi"sv), u8string_view>::value, "" );
33     static_assert ( std::is_same<decltype(  L"Hi"sv), std::wstring_view>::value, "" );
34     static_assert ( std::is_same<decltype(  u"Hi"sv), std::u16string_view>::value, "" );
35     static_assert ( std::is_same<decltype(  U"Hi"sv), std::u32string_view>::value, "" );
36 
37     std::string_view foo;
38     std::wstring_view Lfoo;
39     u8string_view u8foo;
40     std::u16string_view ufoo;
41     std::u32string_view Ufoo;
42 
43 
44     foo  =    ""sv;     assert(  foo.size() == 0);
45     u8foo = u8""sv;     assert(u8foo.size() == 0);
46     Lfoo  =  L""sv;     assert( Lfoo.size() == 0);
47     ufoo  =  u""sv;     assert( ufoo.size() == 0);
48     Ufoo  =  U""sv;     assert( Ufoo.size() == 0);
49 
50     foo   =   " "sv;    assert(  foo.size() == 1);
51     u8foo = u8" "sv;    assert(u8foo.size() == 1);
52     Lfoo  =  L" "sv;    assert( Lfoo.size() == 1);
53     ufoo  =  u" "sv;    assert( ufoo.size() == 1);
54     Ufoo  =  U" "sv;    assert( Ufoo.size() == 1);
55 
56     foo   =   "ABC"sv;  assert(  foo ==   "ABC");   assert(  foo == std::string_view   (  "ABC"));
57     u8foo = u8"ABC"sv;  assert(u8foo == u8"ABC");   assert(u8foo == u8string_view      (u8"ABC"));
58     Lfoo  =  L"ABC"sv;  assert( Lfoo ==  L"ABC");   assert( Lfoo == std::wstring_view  ( L"ABC"));
59     ufoo  =  u"ABC"sv;  assert( ufoo ==  u"ABC");   assert( ufoo == std::u16string_view( u"ABC"));
60     Ufoo  =  U"ABC"sv;  assert( Ufoo ==  U"ABC");   assert( Ufoo == std::u32string_view( U"ABC"));
61 
62     static_assert(  "ABC"sv.size() == 3, "");
63     static_assert(u8"ABC"sv.size() == 3, "");
64     static_assert( L"ABC"sv.size() == 3, "");
65     static_assert( u"ABC"sv.size() == 3, "");
66     static_assert( U"ABC"sv.size() == 3, "");
67 
68     static_assert(noexcept(  "ABC"sv), "");
69     static_assert(noexcept(u8"ABC"sv), "");
70     static_assert(noexcept( L"ABC"sv), "");
71     static_assert(noexcept( u"ABC"sv), "");
72     static_assert(noexcept( U"ABC"sv), "");
73 }
74