59 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef SUPPORT_TEST_STRING_LITERAL_H10#define SUPPORT_TEST_STRING_LITERAL_H11 12#include "test_macros.h"13 14#include <algorithm>15#include <concepts>16#include <string_view>17 18#if TEST_STD_VER > 1719 20/// Helper class to "transfer" a string literal21///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 string24/// 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.28template <std::size_t N>29struct 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_CHARACTERS33 std::copy_n(str, N + 1, wdata_);34# endif35 }36 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_CHARACTERS42 else43 return std::basic_string_view{wdata_};44# endif45 }46 47 char data_[N + 1];48# ifndef TEST_HAS_NO_WIDE_CHARACTERS49 wchar_t wdata_[N + 1];50# endif51};52 53template <std::size_t N>54string_literal(const char (&str)[N]) -> string_literal<N - 1>;55 56#endif // TEST_STD_VER > 1757 58#endif // SUPPORT_TEST_STRING_LITERAL_H59