diff --git a/AK/String.h b/AK/String.h index 9461d16e875..c10b3fbf296 100644 --- a/AK/String.h +++ b/AK/String.h @@ -101,6 +101,7 @@ public: ErrorOr to_uppercase(Optional const& locale = {}) const; ErrorOr to_titlecase(Optional const& locale = {}, TrailingCodePointTransformation trailing_code_point_transformation = TrailingCodePointTransformation::Lowercase) const; ErrorOr to_casefold() const; + ErrorOr to_fullwidth() const; [[nodiscard]] String to_ascii_lowercase() const; [[nodiscard]] String to_ascii_uppercase() const; diff --git a/Userland/Libraries/LibUnicode/String.cpp b/Userland/Libraries/LibUnicode/String.cpp index 2e37675ca28..c9810cd8d32 100644 --- a/Userland/Libraries/LibUnicode/String.cpp +++ b/Userland/Libraries/LibUnicode/String.cpp @@ -11,6 +11,7 @@ #include #include #include +#include // This file contains definitions of AK::String methods which require UCD data. @@ -85,6 +86,21 @@ ErrorOr String::to_titlecase(Optional const& locale, Trailin return builder.to_string_without_validation(); } +ErrorOr 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 build_casefold_string(StringView string, StringBuilder& builder) { UErrorCode status = U_ZERO_ERROR;