diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-01-08 18:56:53 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-01-09 23:00:24 +0000 |
commit | 2eacc7aec1b2b1cc5a491727baa75817ca819bc5 (patch) | |
tree | c7faadb0e278d8b4909e962dc3d0b776dc636f37 /AK | |
parent | d0403ec14f5e198db3fba26ac1f868de2228d454 (diff) | |
download | serenity-2eacc7aec1b2b1cc5a491727baa75817ca819bc5.zip |
AK: Add Utf16View::to_utf8 to convert the view to a UTF-8 AK::String
Diffstat (limited to 'AK')
-rw-r--r-- | AK/Utf16View.cpp | 7 | ||||
-rw-r--r-- | AK/Utf16View.h | 2 |
2 files changed, 8 insertions, 1 deletions
diff --git a/AK/Utf16View.cpp b/AK/Utf16View.cpp index 5076d8a85e..5f6511b8f9 100644 --- a/AK/Utf16View.cpp +++ b/AK/Utf16View.cpp @@ -83,6 +83,11 @@ u32 Utf16View::decode_surrogate_pair(u16 high_surrogate, u16 low_surrogate) ErrorOr<DeprecatedString> Utf16View::to_deprecated_string(AllowInvalidCodeUnits allow_invalid_code_units) const { + return TRY(to_utf8(allow_invalid_code_units)).to_deprecated_string(); +} + +ErrorOr<String> Utf16View::to_utf8(AllowInvalidCodeUnits allow_invalid_code_units) const +{ StringBuilder builder; if (allow_invalid_code_units == AllowInvalidCodeUnits::Yes) { @@ -105,7 +110,7 @@ ErrorOr<DeprecatedString> Utf16View::to_deprecated_string(AllowInvalidCodeUnits TRY(builder.try_append_code_point(code_point)); } - return builder.build(); + return builder.to_string(); } size_t Utf16View::length_in_code_points() const diff --git a/AK/Utf16View.h b/AK/Utf16View.h index daa7bd8131..2e8cd31d7f 100644 --- a/AK/Utf16View.h +++ b/AK/Utf16View.h @@ -12,6 +12,7 @@ #include <AK/Forward.h> #include <AK/Optional.h> #include <AK/Span.h> +#include <AK/String.h> #include <AK/Types.h> #include <AK/Vector.h> @@ -76,6 +77,7 @@ public: }; ErrorOr<DeprecatedString> to_deprecated_string(AllowInvalidCodeUnits = AllowInvalidCodeUnits::No) const; + ErrorOr<String> to_utf8(AllowInvalidCodeUnits = AllowInvalidCodeUnits::No) const; bool is_null() const { return m_code_units.is_null(); } bool is_empty() const { return m_code_units.is_empty(); } |