18#ifndef NUSPELL_UNICODE_HXX
19#define NUSPELL_UNICODE_HXX
24#include <unicode/utf16.h>
25#include <unicode/utf8.h>
28NUSPELL_BEGIN_INLINE_NAMESPACE
32inline constexpr auto u8_max_cp_length = U8_MAX_LENGTH;
34auto inline u8_is_cp_error(int32_t cp) ->
bool {
return cp < 0; }
37auto u8_advance_cp(
const Range& str,
size_t& i, int32_t& cp) ->
void
39 using std::size, std::data;
40#if U_ICU_VERSION_MAJOR_NUM <= 60
41 auto s_ptr = data(str);
43 int32_t len = size(str);
44 U8_NEXT(s_ptr, idx, len, cp);
48 U8_NEXT(str, i, len, cp);
53auto u8_advance_index(
const Range& str,
size_t& i) ->
void
57 U8_FWD_1(str, i, len);
61auto u8_reverse_cp(
const Range& str,
size_t& i, int32_t& cp) ->
void
63 using std::size, std::data;
66 U8_PREV(ptr, 0, idx, cp);
71auto u8_reverse_index(
const Range& str,
size_t& i) ->
void
73 using std::size, std::data;
76 U8_BACK_1(ptr, 0, idx);
81auto u8_write_cp_and_advance(Range& buf,
size_t& i, int32_t cp,
bool& error)
84 using std::size, std::data;
85#if U_ICU_VERSION_MAJOR_NUM <= 60
88 int32_t len = size(buf);
89 U8_APPEND(buf, idx, len, cp, error);
93 U8_APPEND(buf, i, len, cp, error);
100auto valid_u8_advance_cp(
const Range& str,
size_t& i,
char32_t& cp) ->
void
102 U8_NEXT_UNSAFE(str, i, cp);
105template <
class Range>
106auto valid_u8_advance_index(
const Range& str,
size_t& i) ->
void
108 U8_FWD_1_UNSAFE(str, i);
111template <
class Range>
112auto valid_u8_reverse_cp(
const Range& str,
size_t& i,
char32_t& cp) ->
void
114 U8_PREV_UNSAFE(str, i, cp);
117template <
class Range>
118auto valid_u8_reverse_index(
const Range& str,
size_t& i) ->
void
120 U8_BACK_1_UNSAFE(str, i);
123template <
class Range>
124auto valid_u8_write_cp_and_advance(Range& buf,
size_t& i,
char32_t cp) ->
void
126 U8_APPEND_UNSAFE(buf, i, cp);
131inline constexpr auto u16_max_cp_length = U16_MAX_LENGTH;
133auto inline u16_is_cp_error(int32_t cp) ->
bool {
return U_IS_SURROGATE(cp); }
135template <
class Range>
136auto u16_advance_cp(
const Range& str,
size_t& i, int32_t& cp) ->
void
139 auto len = size(str);
140 U16_NEXT(str, i, len, cp);
143template <
class Range>
144auto u16_advance_index(
const Range& str,
size_t& i) ->
void
147 auto len = size(str);
148 U16_FWD_1(str, i, len);
151template <
class Range>
152auto u16_reverse_cp(
const Range& str,
size_t& i, int32_t& cp) ->
void
154 U16_PREV(str, 0, i, cp);
157template <
class Range>
158auto u16_reverse_index(
const Range& str,
size_t& i) ->
void
160 U16_BACK_1(str, 0, i);
163template <
class Range>
164auto u16_write_cp_and_advance(Range& buf,
size_t& i, int32_t cp,
bool& error)
168 auto len = size(buf);
169 U16_APPEND(buf, i, len, cp, error);
174template <
class Range>
175auto valid_u16_advance_cp(
const Range& str,
size_t& i,
char32_t& cp) ->
void
177 U16_NEXT_UNSAFE(str, i, cp);
180template <
class Range>
181auto valid_u16_advance_index(
const Range& str,
size_t& i) ->
void
183 U16_FWD_1_UNSAFE(str, i);
186template <
class Range>
187auto valid_u16_reverse_cp(
const Range& str,
size_t& i,
char32_t& cp) ->
void
189 U16_PREV_UNSAFE(str, i, cp);
192template <
class Range>
193auto valid_u16_reverse_index(
const Range& str,
size_t& i) ->
void
195 U16_BACK_1_UNSAFE(str, i);
198template <
class Range>
199auto valid_u16_write_cp_and_advance(Range& buf,
size_t& i,
char32_t cp) ->
void
201 U16_APPEND_UNSAFE(buf, i, cp);
208 size_t end_i = begin_i;
212 char d[u8_max_cp_length];
216 explicit U8_Encoded_CP(std::string_view str, U8_CP_Pos pos)
217 : sz(pos.end_i - pos.begin_i)
224 }
while (i && --max_len);
226 U8_Encoded_CP(
char32_t cp)
229 valid_u8_write_cp_and_advance(d, z, cp);
232 auto size() const noexcept ->
size_t {
return sz; }
233 auto data() const noexcept -> const
char* {
return d; }
234 operator std::string_view() const noexcept
236 return std::string_view(data(), size());
238 auto copy_to(std::string& str,
size_t j)
const
245 }
while (i && --max_len);
249auto inline u8_swap_adjacent_cp(std::string& str,
size_t i1,
size_t i2,
252 auto cp1 = U8_Encoded_CP(str, {i1, i2});
253 auto cp2 = U8_Encoded_CP(str, {i2, i3});
254 auto new_i2 = i1 + std::size(cp2);
255 cp1.copy_to(str, new_i2);
256 cp2.copy_to(str, i1);
260auto inline u8_swap_cp(std::string& str, U8_CP_Pos pos1, U8_CP_Pos pos2)
261 -> std::pair<size_t, size_t>
264 auto cp1 = U8_Encoded_CP(str, pos1);
265 auto cp2 = U8_Encoded_CP(str, pos2);
266 auto new_p1_end_i = pos1.begin_i + size(cp2);
267 auto new_p2_begin_i = pos2.end_i - size(cp1);
268 std::char_traits<char>::move(&str[new_p1_end_i], &str[pos1.end_i],
269 pos2.begin_i - pos1.end_i);
270 cp2.copy_to(str, pos1.begin_i);
271 cp1.copy_to(str, new_p2_begin_i);
272 return {new_p1_end_i, new_p2_begin_i};
279struct Idx_And_Next_CP {
284struct Idx_And_Prev_CP {
289struct Write_CP_Idx_and_Error {
294template <
class Range>
295[[nodiscard]]
auto u8_next_cp(
const Range& str,
size_t i) -> Idx_And_Next_CP
298 u8_advance_cp(str, i, cp);
302template <
class Range>
303[[nodiscard]]
auto u8_next_index(
const Range& str,
size_t i) ->
size_t
305 u8_advance_index(str, i);
309template <
class Range>
310[[nodiscard]]
auto u8_prev_cp(
const Range& str,
size_t i) -> Idx_And_Prev_CP
313 u8_reverse_cp(str, i, cp);
317template <
class Range>
318[[nodiscard]]
auto u8_prev_index(
const Range& str,
size_t i) ->
size_t
320 u8_reverse_index(str, i);
324template <
class Range>
325[[nodiscard]]
auto u8_write_cp(Range& buf,
size_t i, int32_t cp)
326 -> Write_CP_Idx_and_Error
329 u8_write_cp_and_advance(buf, i, cp, err);
335struct Idx_And_Next_CP_Valid {
340struct Idx_And_Prev_CP_Valid {
345template <
class Range>
346[[nodiscard]]
auto valid_u8_next_cp(
const Range& str,
size_t i)
347 -> Idx_And_Next_CP_Valid
350 valid_u8_advance_cp(str, i, cp);
354template <
class Range>
355[[nodiscard]]
auto valid_u8_next_index(
const Range& str,
size_t i) ->
size_t
357 valid_u8_advance_index(str, i);
361template <
class Range>
362[[nodiscard]]
auto valid_u8_prev_cp(
const Range& str,
size_t i)
363 -> Idx_And_Prev_CP_Valid
366 valid_u8_reverse_cp(str, i, cp);
370template <
class Range>
371[[nodiscard]]
auto valid_u8_prev_index(
const Range& str,
size_t i) ->
size_t
373 valid_u8_reverse_index(str, i);
377template <
class Range>
378[[nodiscard]]
auto valid_u8_write_cp(Range& buf,
size_t i, int32_t cp) ->
size_t
380 valid_u8_write_cp_and_advance(buf, i, cp);
383NUSPELL_END_INLINE_NAMESPACE
Library main namespace.
Definition aff_data.cxx:33