• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
main()19 int main()
20 {
21     using namespace std::literals::string_view_literals;
22 
23     static_assert ( std::is_same<decltype(   "Hi"sv), std::string_view>::value, "" );
24     static_assert ( std::is_same<decltype( u8"Hi"sv), std::string_view>::value, "" );
25     static_assert ( std::is_same<decltype(  L"Hi"sv), std::wstring_view>::value, "" );
26     static_assert ( std::is_same<decltype(  u"Hi"sv), std::u16string_view>::value, "" );
27     static_assert ( std::is_same<decltype(  U"Hi"sv), std::u32string_view>::value, "" );
28 
29     std::string_view foo;
30     std::wstring_view Lfoo;
31     std::u16string_view ufoo;
32     std::u32string_view Ufoo;
33 
34     foo  =   ""sv;     assert( foo.size() == 0);
35     foo  = u8""sv;     assert( foo.size() == 0);
36     Lfoo =  L""sv;     assert(Lfoo.size() == 0);
37     ufoo =  u""sv;     assert(ufoo.size() == 0);
38     Ufoo =  U""sv;     assert(Ufoo.size() == 0);
39 
40     foo  =   " "sv;     assert( foo.size() == 1);
41     foo  = u8" "sv;     assert( foo.size() == 1);
42     Lfoo =  L" "sv;     assert(Lfoo.size() == 1);
43     ufoo =  u" "sv;     assert(ufoo.size() == 1);
44     Ufoo =  U" "sv;     assert(Ufoo.size() == 1);
45 
46     foo  =   "ABC"sv;     assert( foo ==   "ABC");   assert( foo == std::string_view   (  "ABC"));
47     foo  = u8"ABC"sv;     assert( foo == u8"ABC");   assert( foo == std::string_view   (u8"ABC"));
48     Lfoo =  L"ABC"sv;     assert(Lfoo ==  L"ABC");   assert(Lfoo == std::wstring_view  ( L"ABC"));
49     ufoo =  u"ABC"sv;     assert(ufoo ==  u"ABC");   assert(ufoo == std::u16string_view( u"ABC"));
50     Ufoo =  U"ABC"sv;     assert(Ufoo ==  U"ABC");   assert(Ufoo == std::u32string_view( U"ABC"));
51 
52     static_assert(  "ABC"sv.size() == 3, "");
53     static_assert(u8"ABC"sv.size() == 3, "");
54     static_assert( L"ABC"sv.size() == 3, "");
55     static_assert( u"ABC"sv.size() == 3, "");
56     static_assert( U"ABC"sv.size() == 3, "");
57 }
58