mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
AK: Simplify String::from_stream by leveraging StringBase helpers
This commit is contained in:
parent
7506736869
commit
7dbe357e9f
Notes:
sideshowbarker
2024-07-16 23:08:48 +09:00
Author: https://github.com/DanShaders Commit: https://github.com/SerenityOS/serenity/commit/7dbe357e9f Pull-request: https://github.com/SerenityOS/serenity/pull/21661 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/kleinesfilmroellchen Reviewed-by: https://github.com/trflynn89
2 changed files with 8 additions and 32 deletions
|
@ -64,28 +64,6 @@ ErrorOr<NonnullRefPtr<StringData>> StringData::create_uninitialized(size_t byte_
|
||||||
return new_string_data;
|
return new_string_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ErrorOr<void> read_stream_into_buffer(Stream& stream, Bytes buffer)
|
|
||||||
{
|
|
||||||
TRY(stream.read_until_filled(buffer));
|
|
||||||
|
|
||||||
if (!Utf8View { StringView { buffer } }.validate())
|
|
||||||
return Error::from_string_literal("String::from_stream: Input was not valid UTF-8");
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<StringData>> StringData::from_stream(Stream& stream, size_t byte_count)
|
|
||||||
{
|
|
||||||
// Strings of MAX_SHORT_STRING_BYTE_COUNT bytes or less should be handled by the String short string optimization.
|
|
||||||
VERIFY(byte_count > String::MAX_SHORT_STRING_BYTE_COUNT);
|
|
||||||
|
|
||||||
u8* buffer = nullptr;
|
|
||||||
auto new_string_data = TRY(create_uninitialized(byte_count, buffer));
|
|
||||||
TRY(read_stream_into_buffer(stream, { buffer, byte_count }));
|
|
||||||
|
|
||||||
return new_string_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<StringData>> StringData::create_substring(StringData const& superstring, size_t start, size_t byte_count)
|
ErrorOr<NonnullRefPtr<StringData>> StringData::create_substring(StringData const& superstring, size_t start, size_t byte_count)
|
||||||
{
|
{
|
||||||
// Strings of MAX_SHORT_STRING_BYTE_COUNT bytes or less should be handled by the String short string optimization.
|
// Strings of MAX_SHORT_STRING_BYTE_COUNT bytes or less should be handled by the String short string optimization.
|
||||||
|
@ -135,15 +113,14 @@ ErrorOr<String> String::from_utf8(StringView view)
|
||||||
|
|
||||||
ErrorOr<String> String::from_stream(Stream& stream, size_t byte_count)
|
ErrorOr<String> String::from_stream(Stream& stream, size_t byte_count)
|
||||||
{
|
{
|
||||||
if (byte_count <= MAX_SHORT_STRING_BYTE_COUNT) {
|
String result;
|
||||||
ShortString short_string;
|
TRY(result.replace_with_new_string(byte_count, [&](Bytes buffer) -> ErrorOr<void> {
|
||||||
if (byte_count > 0)
|
TRY(stream.read_until_filled(buffer));
|
||||||
TRY(Detail::read_stream_into_buffer(stream, { short_string.storage, byte_count }));
|
if (!Utf8View { StringView { buffer } }.validate())
|
||||||
short_string.byte_count_and_short_string_flag = (byte_count << 1) | SHORT_STRING_FLAG;
|
return Error::from_string_literal("String::from_stream: Input was not valid UTF-8");
|
||||||
return String { short_string };
|
return {};
|
||||||
}
|
}));
|
||||||
auto data = TRY(Detail::StringData::from_stream(stream, byte_count));
|
return result;
|
||||||
return String { move(data) };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<String> String::repeated(u32 code_point, size_t count)
|
ErrorOr<String> String::repeated(u32 code_point, size_t count)
|
||||||
|
|
|
@ -17,7 +17,6 @@ class StringData final : public RefCounted<StringData> {
|
||||||
public:
|
public:
|
||||||
static ErrorOr<NonnullRefPtr<StringData>> create_uninitialized(size_t, u8*& buffer);
|
static ErrorOr<NonnullRefPtr<StringData>> create_uninitialized(size_t, u8*& buffer);
|
||||||
static ErrorOr<NonnullRefPtr<StringData>> create_substring(StringData const& superstring, size_t start, size_t byte_count);
|
static ErrorOr<NonnullRefPtr<StringData>> create_substring(StringData const& superstring, size_t start, size_t byte_count);
|
||||||
static ErrorOr<NonnullRefPtr<StringData>> from_stream(Stream&, size_t byte_count);
|
|
||||||
|
|
||||||
struct SubstringData {
|
struct SubstringData {
|
||||||
StringData const* superstring { nullptr };
|
StringData const* superstring { nullptr };
|
||||||
|
|
Loading…
Reference in a new issue