Debian Codegraph @

source/code/Libs/include/utf8/cpp20.h

raw symbols SCIP
1 // Copyright 2022 Nemanja Trifunovic
2
3 /*
4 Permission is hereby granted, free of charge, to any person or organization
5 obtaining a copy of the software and accompanying documentation covered by
6 this license (the "Software") to use, reproduce, display, distribute,
7 execute, and transmit the Software, and to prepare derivative works of the
8 Software, and to permit third-parties to whom the Software is furnished to
9 do so, all subject to the following:
10
11 The copyright notices in the Software and this entire statement, including
12 the above license grant, this restriction and the following disclaimer,
13 must be included in all copies of the Software, in whole or in part, and
14 all derivative works of the Software, unless such copies or derivative
15 works are solely in the form of machine-executable object code generated by
16 a source language processor.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #ifndef UTF8_FOR_CPP_207e906c01_03a3_4daf_b420_ea7ea952b3c9
29 #define UTF8_FOR_CPP_207e906c01_03a3_4daf_b420_ea7ea952b3c9
30
31 #include "cpp17.h"
32
33 namespace utf8
34 {
35 inline std::u8string utf16tou8(const std::u16string& s)
36 {
37 std::u8string result;
38 utf16to8(s.begin(), s.end(), std::back_inserter(result));
39 return result;
40 }
41
42 inline std::u8string utf16tou8(std::u16string_view s)
43 {
44 std::u8string result;
45 utf16to8(s.begin(), s.end(), std::back_inserter(result));
46 return result;
47 }
48
49 inline std::u16string utf8to16(const std::u8string& s)
50 {
51 std::u16string result;
52 utf8to16(s.begin(), s.end(), std::back_inserter(result));
53 return result;
54 }
55
56 inline std::u16string utf8to16(const std::u8string_view& s)
57 {
58 std::u16string result;
59 utf8to16(s.begin(), s.end(), std::back_inserter(result));
60 return result;
61 }
62
63 inline std::u8string utf32tou8(const std::u32string& s)
64 {
65 std::u8string result;
66 utf32to8(s.begin(), s.end(), std::back_inserter(result));
67 return result;
68 }
69
70 inline std::u8string utf32tou8(const std::u32string_view& s)
71 {
72 std::u8string result;
73 utf32to8(s.begin(), s.end(), std::back_inserter(result));
74 return result;
75 }
76
77 inline std::u32string utf8to32(const std::u8string& s)
78 {
79 std::u32string result;
80 utf8to32(s.begin(), s.end(), std::back_inserter(result));
81 return result;
82 }
83
84 inline std::u32string utf8to32(const std::u8string_view& s)
85 {
86 std::u32string result;
87 utf8to32(s.begin(), s.end(), std::back_inserter(result));
88 return result;
89 }
90
91 inline std::size_t find_invalid(const std::u8string& s)
92 {
93 std::u8string::const_iterator invalid = find_invalid(s.begin(), s.end());
94 return (invalid == s.end()) ? std::string_view::npos : static_cast<std::size_t>(invalid - s.begin());
95 }
96
97 inline bool is_valid(const std::u8string& s)
98 {
99 return is_valid(s.begin(), s.end());
100 }
101
102 inline std::u8string replace_invalid(const std::u8string& s, char32_t replacement)
103 {
104 std::u8string result;
105 replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement);
106 return result;
107 }
108
109 inline std::u8string replace_invalid(const std::u8string& s)
110 {
111 std::u8string result;
112 replace_invalid(s.begin(), s.end(), std::back_inserter(result));
113 return result;
114 }
115
116 inline bool starts_with_bom(const std::u8string& s)
117 {
118 return starts_with_bom(s.begin(), s.end());
119 }
120
121 } // namespace utf8
122
123 #endif // header guard
124