LibUnicode: Implement to_fullwidth() in String

The implementation uses the transliterator class from icu.
This commit is contained in:
Grubre 2024-10-30 20:36:26 +01:00 committed by Tim Flynn
parent 5a6a7b7e5e
commit a8a81342bb
Notes: github-actions[bot] 2024-11-01 11:49:19 +00:00
2 changed files with 17 additions and 0 deletions

View file

@ -101,6 +101,7 @@ public:
ErrorOr<String> to_uppercase(Optional<StringView> const& locale = {}) const; ErrorOr<String> to_uppercase(Optional<StringView> const& locale = {}) const;
ErrorOr<String> to_titlecase(Optional<StringView> const& locale = {}, TrailingCodePointTransformation trailing_code_point_transformation = TrailingCodePointTransformation::Lowercase) const; ErrorOr<String> to_titlecase(Optional<StringView> const& locale = {}, TrailingCodePointTransformation trailing_code_point_transformation = TrailingCodePointTransformation::Lowercase) const;
ErrorOr<String> to_casefold() const; ErrorOr<String> to_casefold() const;
ErrorOr<String> to_fullwidth() const;
[[nodiscard]] String to_ascii_lowercase() const; [[nodiscard]] String to_ascii_lowercase() const;
[[nodiscard]] String to_ascii_uppercase() const; [[nodiscard]] String to_ascii_uppercase() const;

View file

@ -11,6 +11,7 @@
#include <unicode/bytestream.h> #include <unicode/bytestream.h>
#include <unicode/casemap.h> #include <unicode/casemap.h>
#include <unicode/stringoptions.h> #include <unicode/stringoptions.h>
#include <unicode/translit.h>
// This file contains definitions of AK::String methods which require UCD data. // This file contains definitions of AK::String methods which require UCD data.
@ -85,6 +86,21 @@ ErrorOr<String> String::to_titlecase(Optional<StringView> const& locale, Trailin
return builder.to_string_without_validation(); return builder.to_string_without_validation();
} }
ErrorOr<String> String::to_fullwidth() const
{
UErrorCode status = U_ZERO_ERROR;
auto const transliterator = adopt_own_if_nonnull(icu::Transliterator::createInstance("Halfwidth-Fullwidth", UTRANS_FORWARD, status));
if (Unicode::icu_failure(status)) {
return Error::from_string_literal("Unable to create transliterator");
}
auto icu_string = Unicode::icu_string(bytes_as_string_view());
transliterator->transliterate(icu_string);
return Unicode::icu_string_to_string(icu_string);
}
static ErrorOr<void> build_casefold_string(StringView string, StringBuilder& builder) static ErrorOr<void> build_casefold_string(StringView string, StringBuilder& builder)
{ {
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;