mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-25 17:25:08 +02:00
AK+LibIPC: Implement an encoder/decoder for UTF-16 strings
This commit is contained in:
Notes:
github-actions[bot]
2025-08-02 17:11:30 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/13ed6aba712 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5633 Reviewed-by: https://github.com/gmta
@@ -4,6 +4,7 @@
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Stream.h>
|
||||
#include <AK/TypedTransfer.h>
|
||||
#include <AK/Utf16StringData.h>
|
||||
#include <AK/Utf32View.h>
|
||||
@@ -158,6 +159,31 @@ NonnullRefPtr<Utf16StringData> Utf16StringData::from_string_builder(StringBuilde
|
||||
return adopt_ref(*new (buffer->buffer.data()) Utf16StringData { storage_type, code_unit_length });
|
||||
}
|
||||
|
||||
ErrorOr<NonnullRefPtr<Utf16StringData>> Utf16StringData::from_ipc_stream(Stream& stream, size_t length_in_code_units, bool is_ascii)
|
||||
{
|
||||
RefPtr<Utf16StringData> string;
|
||||
|
||||
if (is_ascii) {
|
||||
string = create_uninitialized(StorageType::ASCII, length_in_code_units);
|
||||
|
||||
Bytes bytes { string->m_ascii_data, length_in_code_units };
|
||||
TRY(stream.read_until_filled(bytes));
|
||||
|
||||
if (!string->ascii_view().is_ascii())
|
||||
return Error::from_string_literal("Stream contains invalid ASCII data");
|
||||
} else {
|
||||
string = create_uninitialized(StorageType::UTF16, length_in_code_units);
|
||||
|
||||
Bytes bytes { reinterpret_cast<u8*>(string->m_utf16_data), length_in_code_units * sizeof(char16_t) };
|
||||
TRY(stream.read_until_filled(bytes));
|
||||
|
||||
if (!string->utf16_view().validate())
|
||||
return Error::from_string_literal("Stream contains invalid UTF-16 data");
|
||||
}
|
||||
|
||||
return string.release_nonnull();
|
||||
}
|
||||
|
||||
NonnullRefPtr<Utf16StringData> Utf16StringData::to_well_formed(Utf16View const& utf16_string)
|
||||
{
|
||||
VERIFY(!utf16_string.has_ascii_storage());
|
||||
|
||||
Reference in New Issue
Block a user