1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef SUPPORT_TEST_STRING_LITERAL_H
10 #define SUPPORT_TEST_STRING_LITERAL_H
12 #include "test_macros.h"
16 #include <string_view>
20 /// Helper class to "transfer" a string literal
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.
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
{
30 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_
);
37 template <class CharT
>
38 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
43 return std::basic_string_view
{wdata_
};
48 # ifndef TEST_HAS_NO_WIDE_CHARACTERS
49 wchar_t wdata_
[N
+ 1];
53 template <std::size_t N
>
54 string_literal(const char (&str
)[N
]) -> string_literal
<N
- 1>;
56 #endif // TEST_STD_VER > 17
58 #endif // SUPPORT_TEST_STRING_LITERAL_H