mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-24 09:52:31 +00:00
AK: Support UTF-16 string formatting
The underlying storage used during string formatting is StringBuilder. To support UTF-16 strings, this patch allows callers to specify a mode during StringBuilder construction. The default mode is UTF-8, for which StringBuilder remains unchanged. In UTF-16 mode, we treat the StringBuilder's internal ByteBuffer as a series of u16 code units. Appending a single character will append 2 bytes for that character (cast to a char16_t). Appending a StringView will transcode the string to UTF-16. Utf16String also gains the same memory optimization that we added for String, where we hand-off the underlying buffer to Utf16String to avoid having to re-allocate. In the future, we may want to further optimize for ASCII strings. For example, we could defer committing to the u16-esque storage until we see a non-ASCII code point.
This commit is contained in:
parent
fe676585f5
commit
2803d66d87
Notes:
github-actions[bot]
2025-07-18 16:47:24 +00:00
Author: https://github.com/trflynn89
Commit: 2803d66d87
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5388
Reviewed-by: https://github.com/shannonbooth ✅
11 changed files with 362 additions and 55 deletions
|
@ -1280,7 +1280,7 @@ ThrowCompletionOr<String> get_substitution(VM& vm, Utf16View const& matched, Utf
|
|||
VERIFY(position <= string_length);
|
||||
|
||||
// 3. Let result be the empty String.
|
||||
Utf16Data result;
|
||||
StringBuilder result(StringBuilder::Mode::UTF16);
|
||||
|
||||
// 4. Let templateRemainder be replacementTemplate.
|
||||
auto replace_template_string = TRY(replacement_template.to_utf16_string(vm));
|
||||
|
@ -1451,7 +1451,7 @@ ThrowCompletionOr<String> get_substitution(VM& vm, Utf16View const& matched, Utf
|
|||
auto ref_length = ref.length_in_code_units();
|
||||
|
||||
// k. Set result to the string-concatenation of result and refReplacement.
|
||||
result.append(ref_replacement.utf16_span().data(), ref_replacement.length_in_code_units());
|
||||
result.append(ref_replacement);
|
||||
|
||||
// j. Set templateRemainder to the substring of templateRemainder from refLength.
|
||||
// NOTE: We do this step last because refReplacement may point to templateRemainder.
|
||||
|
@ -1459,7 +1459,7 @@ ThrowCompletionOr<String> get_substitution(VM& vm, Utf16View const& matched, Utf
|
|||
}
|
||||
|
||||
// 6. Return result.
|
||||
return MUST(Utf16View { result }.to_utf8());
|
||||
return MUST(result.utf16_string_view().to_utf8());
|
||||
}
|
||||
|
||||
void DisposeCapability::visit_edges(GC::Cell::Visitor& visitor) const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue