C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
char8_t_remediation.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <array>
26#include <cstddef>
27#include <string>
28#include <string_view>
29
30#if __cpp_char8_t >= 201811L
31namespace essence::detail {
32 template <std::size_t N>
33 struct char8_t_string_literal {
34 static constexpr std::size_t size = N;
35
36 bool char_literal;
37 std::array<char8_t, N> sequence;
38
39 constexpr char8_t_string_literal(char8_t ch) noexcept // NOLINT(*-explicit-constructor)
40 : char_literal{true}, sequence{ch} {}
41
42 template <std::size_t... Is>
43 constexpr char8_t_string_literal(const char8_t (&sequence)[N], std::index_sequence<Is...>) noexcept
44 : char_literal{}, sequence{sequence[Is]...} {}
45
46 constexpr char8_t_string_literal(const char8_t (&sequence)[N]) noexcept // NOLINT(*-explicit-constructor)
47 : char8_t_string_literal{sequence, std::make_index_sequence<N>{}} {}
48 };
49
50 char8_t_string_literal(char8_t) -> char8_t_string_literal<1>;
51
52 template <std::size_t N>
53 char8_t_string_literal(const char8_t (&)[N]) -> char8_t_string_literal<N>;
54
55 template <char8_t_string_literal Literal, std::size_t... Is>
56 inline constexpr char as_char_array_v[sizeof...(Is)]{static_cast<char>(Literal.sequence[Is])...};
57
58 template <char8_t_string_literal Literal, std::size_t... Is>
59 constexpr const char (&make_as_char_array(std::index_sequence<Is...>) noexcept)[sizeof...(Is)] {
60 return as_char_array_v<Literal, Is...>;
61 }
62} // namespace essence::detail
63
64namespace essence {
65 template <detail::char8_t_string_literal Literal>
66 inline constexpr decltype(auto) as_char_v = []() -> decltype(auto) {
67 if constexpr (Literal.char_literal) {
68 return static_cast<char>(Literal.sequence.front());
69 } else {
70 return detail::make_as_char_array<Literal>(std::make_index_sequence<decltype(Literal)::size>{});
71 }
72 }();
73
74#define U8(x) (essence::as_char_v<(u8##x)>)
75
76 inline std::string from_u8string(std::u8string_view str) {
77 return {str.begin(), str.end()};
78 }
79
80 inline std::u8string to_u8string(std::string_view str) {
81 return {str.begin(), str.end()};
82 }
83} // namespace essence
84#else
85#define U8(x) u8##x
86
87namespace essence {
88 inline std::string from_u8string(std::string_view str) {
89 return std::string{str};
90 }
91
92 inline std::string from_u8string(std::string&& str) noexcept {
93 return std::move(str);
94 }
95
96 inline std::string to_u8string(std::string_view str) {
97 return std::string{str};
98 }
99
100 inline std::string to_u8string(std::string&& str) noexcept {
101 return std::move(str);
102 }
103} // namespace essence
104#endif