AK+Kernel: Add StringBuilder::append overload for UTF-16 views

Currently, to append a UTF-16 view to a StringBuilder, callers must
first convert the view to UTF-8 and then append the copy. Add a UTF-16
overload so callers do not need to hold an entire copy in memory.
This commit is contained in:
Timothy Flynn 2021-08-09 11:53:28 -04:00 committed by Andreas Kling
parent 5978caf96b
commit c16aca7abf
3 changed files with 14 additions and 0 deletions

View file

@ -12,6 +12,7 @@
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <AK/Utf16View.h>
#include <AK/Utf32View.h>
namespace AK {
@ -112,6 +113,16 @@ void StringBuilder::append_code_point(u32 code_point)
}
}
void StringBuilder::append(Utf16View const& utf16_view)
{
for (size_t i = 0; i < utf16_view.length_in_code_units();) {
auto code_point = utf16_view.code_point_at(i);
append_code_point(code_point);
i += (code_point > 0xffff ? 2 : 1);
}
}
void StringBuilder::append(Utf32View const& utf32_view)
{
for (size_t i = 0; i < utf32_view.length(); ++i) {

View file

@ -22,6 +22,7 @@ public:
~StringBuilder() = default;
void append(StringView const&);
void append(Utf16View const&);
void append(Utf32View const&);
void append(char);
void append_code_point(u32);

View file

@ -312,6 +312,8 @@ set(AK_SOURCES
../AK/Time.cpp
../AK/Format.cpp
../AK/UUID.cpp
../AK/Utf8View.cpp
../AK/Utf16View.cpp
)
set(ELF_SOURCES