xref: /aosp_15_r20/external/cronet/third_party/libc++/src/test/support/string_literal.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #ifndef SUPPORT_TEST_STRING_LITERAL_H
10 #define SUPPORT_TEST_STRING_LITERAL_H
11 
12 #include "test_macros.h"
13 
14 #include <algorithm>
15 #include <concepts>
16 #include <string_view>
17 
18 #if TEST_STD_VER > 17
19 
20 /// Helper class to "transfer" a string literal
21 ///
22 /// The MAKE_STRING helper macros turn a string literal into a const char*.
23 /// This is an issue when testing std::format; its format-string needs a string
24 /// literal for compile-time validation. This class does the job.
25 ///
26 /// \note The class assumes a wchar_t can be initialized from a char.
27 /// \note All members are public to avoid compilation errors.
28 template <std::size_t N>
29 struct string_literal {
string_literalstring_literal30   consteval /*implicit*/ string_literal(const char (&str)[N + 1]) {
31     std::copy_n(str, N + 1, data_);
32 #  ifndef TEST_HAS_NO_WIDE_CHARACTERS
33     std::copy_n(str, N + 1, wdata_);
34 #  endif
35   }
36 
37   template <class CharT>
svstring_literal38   consteval std::basic_string_view<CharT> sv() const {
39     if constexpr (std::same_as<CharT, char>)
40       return std::basic_string_view{data_};
41 #  ifndef TEST_HAS_NO_WIDE_CHARACTERS
42     else
43       return std::basic_string_view{wdata_};
44 #  endif
45   }
46 
47   char data_[N + 1];
48 #  ifndef TEST_HAS_NO_WIDE_CHARACTERS
49   wchar_t wdata_[N + 1];
50 #  endif
51 };
52 
53 template <std::size_t N>
54 string_literal(const char (&str)[N]) -> string_literal<N - 1>;
55 
56 #endif // TEST_STD_VER > 17
57 
58 #endif // SUPPORT_TEST_STRING_LITERAL_H
59