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++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         ASSERT_SAME_TYPE(decltype(  "Hi"sv), std::string_view);
30         ASSERT_SAME_TYPE(decltype(u8"Hi"sv), u8string_view);
31         ASSERT_SAME_TYPE(decltype( L"Hi"sv), std::wstring_view);
32         ASSERT_SAME_TYPE(decltype( u"Hi"sv), std::u16string_view);
33         ASSERT_SAME_TYPE(decltype( U"Hi"sv), std::u32string_view);
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         foo  =    ""sv;     assert(  foo.size() == 0);
42         u8foo = u8""sv;     assert(u8foo.size() == 0);
43         Lfoo  =  L""sv;     assert( Lfoo.size() == 0);
44         ufoo  =  u""sv;     assert( ufoo.size() == 0);
45         Ufoo  =  U""sv;     assert( Ufoo.size() == 0);
46 
47         foo   =   " "sv;    assert(  foo.size() == 1);
48         u8foo = u8" "sv;    assert(u8foo.size() == 1);
49         Lfoo  =  L" "sv;    assert( Lfoo.size() == 1);
50         ufoo  =  u" "sv;    assert( ufoo.size() == 1);
51         Ufoo  =  U" "sv;    assert( Ufoo.size() == 1);
52 
53         foo   =   "ABC"sv;  assert(  foo ==   "ABC");   assert(  foo == std::string_view   (  "ABC"));
54         u8foo = u8"ABC"sv;  assert(u8foo == u8"ABC");   assert(u8foo == u8string_view      (u8"ABC"));
55         Lfoo  =  L"ABC"sv;  assert( Lfoo ==  L"ABC");   assert( Lfoo == std::wstring_view  ( L"ABC"));
56         ufoo  =  u"ABC"sv;  assert( ufoo ==  u"ABC");   assert( ufoo == std::u16string_view( u"ABC"));
57         Ufoo  =  U"ABC"sv;  assert( Ufoo ==  U"ABC");   assert( Ufoo == std::u32string_view( U"ABC"));
58 
59         static_assert(  "ABC"sv.size() == 3, "");
60         static_assert(u8"ABC"sv.size() == 3, "");
61         static_assert( L"ABC"sv.size() == 3, "");
62         static_assert( u"ABC"sv.size() == 3, "");
63         static_assert( U"ABC"sv.size() == 3, "");
64 
65         ASSERT_NOEXCEPT(  "ABC"sv);
66         ASSERT_NOEXCEPT(u8"ABC"sv);
67         ASSERT_NOEXCEPT( L"ABC"sv);
68         ASSERT_NOEXCEPT( u"ABC"sv);
69         ASSERT_NOEXCEPT( U"ABC"sv);
70     }
71     {
72         using namespace std::literals;
73         std::string_view foo = ""sv;
74         assert(foo.length() == 0);
75     }
76     {
77         using namespace std;
78         std::string_view foo = ""sv;
79         assert(foo.length() == 0);
80     }
81 
82     return 0;
83 }
84