diff --git a/AK/String.cpp b/AK/String.cpp index bdd2379b1ee..2c9c8ec1421 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -64,28 +64,6 @@ ErrorOr> StringData::create_uninitialized(size_t byte_ return new_string_data; } -static ErrorOr 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> 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> 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. @@ -135,15 +113,14 @@ ErrorOr String::from_utf8(StringView view) ErrorOr String::from_stream(Stream& stream, size_t byte_count) { - if (byte_count <= MAX_SHORT_STRING_BYTE_COUNT) { - ShortString short_string; - if (byte_count > 0) - TRY(Detail::read_stream_into_buffer(stream, { short_string.storage, byte_count })); - short_string.byte_count_and_short_string_flag = (byte_count << 1) | SHORT_STRING_FLAG; - return String { short_string }; - } - auto data = TRY(Detail::StringData::from_stream(stream, byte_count)); - return String { move(data) }; + String result; + TRY(result.replace_with_new_string(byte_count, [&](Bytes buffer) -> ErrorOr { + 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 {}; + })); + return result; } ErrorOr String::repeated(u32 code_point, size_t count) diff --git a/AK/StringInternals.h b/AK/StringInternals.h index 0e6f411d94f..39d0217f86f 100644 --- a/AK/StringInternals.h +++ b/AK/StringInternals.h @@ -17,7 +17,6 @@ class StringData final : public RefCounted { public: static ErrorOr> create_uninitialized(size_t, u8*& buffer); static ErrorOr> create_substring(StringData const& superstring, size_t start, size_t byte_count); - static ErrorOr> from_stream(Stream&, size_t byte_count); struct SubstringData { StringData const* superstring { nullptr };