8#include "unicode_normalization.hpp"
9#include "unicode_grapheme_cluster_break.hpp"
10#include "../utility/utility.hpp"
11#include "../i18n/i18n.hpp"
12#include "../time/time.hpp"
13#include "../macros.hpp"
25hi_export_module(hikogui.unicode.gstring);
27hi_export
namespace std {
31 using char_type = hi::grapheme;
32 using int_type = std::make_signed_t<char_type::value_type>;
36 using comparison_category = std::strong_ordering;
38 constexpr static void assign(char_type& r, char_type
const& a)
noexcept
52 [[nodiscard]]
constexpr static bool eq(char_type a, char_type b)
noexcept
57 [[nodiscard]]
constexpr static bool lt(char_type a, char_type b)
noexcept
62 constexpr static char_type *
move(char_type *dst, char_type
const *src,
std::size_t count)
noexcept
64 hi_axiom_not_null(src);
65 hi_axiom_not_null(dst);
73 dst[i - 1] = src[i - 1];
79 constexpr static char_type *
copy(char_type *dst, char_type
const *src,
std::size_t count)
noexcept
81 hi_axiom_not_null(src);
82 hi_axiom_not_null(dst);
92 hi_axiom_not_null(s1);
93 hi_axiom_not_null(s2);
97 return s1[i] < s2[i] ? -1 : 1;
105 hi_axiom_not_null(s);
108 while (s[i] !=
'\0') {
114 constexpr static char_type
const *
find(
const char_type *p,
std::size_t count,
const char_type& ch)
noexcept
116 hi_axiom_not_null(p);
126 constexpr static char_type
to_char_type(int_type c)
noexcept
128 return c < 0 ? char_type{U
'\ufffd'} : char_type{std::in_place, hi::char_cast<char_type::value_type>(c)};
131 constexpr static int_type
to_int_type(char_type c)
noexcept
133 return hi::char_cast<int_type>(c.intrinsic());
136 constexpr static bool eq_int_type(int_type c1, int_type c2)
noexcept
141 constexpr static int_type
eof()
noexcept
146 constexpr static int_type
not_eof(int_type e)
noexcept
148 return e < 0 ? 0 : e;
154hi_export
namespace hi::inline
v1 {
157using gstring_view = std::basic_string_view<grapheme>;
160[[nodiscard]]
constexpr bool operator==(gstring_view
const &lhs, std::string_view
const &rhs)
noexcept
162 if (lhs.size() != rhs.size()) {
166 auto l_it = lhs.begin();
167 auto l_last = lhs.end();
168 auto r_it = rhs.begin();
170 for (; l_it != l_last; ++l_it, ++r_it) {
171 if (*l_it != *r_it) {
186template<std::input_or_output_iterator It, std::sentinel_for<It> ItEnd>
187constexpr void set_language(It first, ItEnd last, language_tag language)
noexcept
189 language = language.expand();
191 for (
auto it = first; it != last; ++it) {
192 it->set_language_tag(language);
196[[nodiscard]]
constexpr gstring set_language(gstring str, language_tag language)
noexcept
198 set_language(str.begin(), str.end(), language);
219template<std::input_or_output_iterator It, std::sentinel_for<It> ItEnd>
220constexpr void fix_language(It first, ItEnd last, language_tag default_language_tag)
noexcept
221 requires(std::is_same_v<std::iter_value_t<It>,
grapheme>)
227 auto const first_language_it =
std::find_if(first, last, [](
auto &x) {
return x.language(); });
228 if (first_language_it != last) {
229 default_language_tag = first_language_it->language_tag().expand();
231 default_language_tag = default_language_tag.expand();
234 for (
auto it = first; it != first_language_it; ++it) {
235 it->set_language_tag(default_language_tag);
238 for (
auto it = first_language_it; it != last; ++it) {
239 if (not it->language()) {
240 it->set_language_tag(default_language_tag);
242 it->set_language_tag(it->language_tag().expand());
247[[nodiscard]]
constexpr gstring fix_language(gstring str, language_tag default_language_tag)
noexcept
249 fix_language(str.begin(), str.end(), default_language_tag);
262[[nodiscard]]
constexpr gstring
268 auto break_state = detail::grapheme_break_state{};
271 for (
auto const code_point : normalized_string) {
272 if (detail::breaks_grapheme(code_point, break_state)) {
273 if (cluster.size() > 0) {
279 cluster += code_point;
281 if (ssize(cluster) != 0) {
296[[nodiscard]]
constexpr gstring
307[[nodiscard]]
constexpr std::string to_string(gstring_view rhs)
noexcept
311 for (
auto const c : rhs) {
326 for (
auto const c : rhs) {
341 for (
auto const c : rhs) {
354 return to_string(gstring_view{rhs});
359hi_export
namespace std {
366 for (
auto const c : rhs) {
constexpr std::u32string to_u32string(std::u32string_view rhs) noexcept
Identity conversion from UTF-32 to UTF-32.
Definition to_string.hpp:28
constexpr std::wstring to_wstring(std::u32string_view rhs) noexcept
Conversion from UTF-32 to wide-string (UTF-16/32).
Definition to_string.hpp:160
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
constexpr void fix_language(It first, ItEnd last, language_tag default_language_tag) noexcept
Fix the language for the string.
Definition gstring.hpp:220
constexpr gstring to_gstring(std::u32string_view rhs, unicode_normalize_config config=unicode_normalize_config::NFC()) noexcept
Convert a UTF-32 string-view to a grapheme-string.
Definition gstring.hpp:263
constexpr void set_language(It first, ItEnd last, language_tag language) noexcept
Set the language for the string.
Definition gstring.hpp:187
constexpr std::u32string unicode_normalize(std::u32string_view text, unicode_normalize_config config=unicode_normalize_config::NFC()) noexcept
Convert text to a Unicode composed normal form.
Definition unicode_normalization.hpp:308
Definition grapheme.hpp:154
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:167
Definition unicode_normalization.hpp:24
T to_char_type(T... args)