mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
AK: Simplify String::repeated by leveraging StringBase helpers
This commit is contained in:
parent
7dbe357e9f
commit
5d6cd65e29
Notes:
sideshowbarker
2024-07-16 19:42:24 +09:00
Author: https://github.com/DanShaders Commit: https://github.com/SerenityOS/serenity/commit/5d6cd65e29 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
1 changed files with 23 additions and 34 deletions
|
@ -134,31 +134,19 @@ ErrorOr<String> String::repeated(u32 code_point, size_t count)
|
||||||
code_point_as_utf8[i++] = static_cast<u8>(byte);
|
code_point_as_utf8[i++] = static_cast<u8>(byte);
|
||||||
});
|
});
|
||||||
|
|
||||||
auto copy_to_buffer = [&](u8* buffer) {
|
|
||||||
if (code_point_byte_length == 1) {
|
|
||||||
memset(buffer, code_point_as_utf8[0], count);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < count; ++i)
|
|
||||||
memcpy(buffer + (i * code_point_byte_length), code_point_as_utf8.data(), code_point_byte_length);
|
|
||||||
};
|
|
||||||
|
|
||||||
auto total_byte_count = code_point_byte_length * count;
|
auto total_byte_count = code_point_byte_length * count;
|
||||||
|
|
||||||
if (total_byte_count <= MAX_SHORT_STRING_BYTE_COUNT) {
|
String result;
|
||||||
ShortString short_string;
|
TRY(result.replace_with_new_string(total_byte_count, [&](Bytes buffer) {
|
||||||
copy_to_buffer(short_string.storage);
|
if (code_point_byte_length == 1) {
|
||||||
short_string.byte_count_and_short_string_flag = (total_byte_count << 1) | SHORT_STRING_FLAG;
|
buffer.fill(code_point_as_utf8[0]);
|
||||||
|
} else {
|
||||||
return String { short_string };
|
for (i = 0; i < count; ++i)
|
||||||
}
|
memcpy(buffer.data() + (i * code_point_byte_length), code_point_as_utf8.data(), code_point_byte_length);
|
||||||
|
}
|
||||||
u8* buffer = nullptr;
|
return ErrorOr<void> {};
|
||||||
auto new_string_data = TRY(Detail::StringData::create_uninitialized(total_byte_count, buffer));
|
}));
|
||||||
copy_to_buffer(buffer);
|
return result;
|
||||||
|
|
||||||
return String { move(new_string_data) };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StringView String::bytes_as_string_view() const
|
StringView String::bytes_as_string_view() const
|
||||||
|
@ -462,18 +450,19 @@ bool String::equals_ignoring_ascii_case(StringView other) const
|
||||||
String String::repeated(String const& input, size_t count)
|
String String::repeated(String const& input, size_t count)
|
||||||
{
|
{
|
||||||
VERIFY(!Checked<size_t>::multiplication_would_overflow(count, input.bytes().size()));
|
VERIFY(!Checked<size_t>::multiplication_would_overflow(count, input.bytes().size()));
|
||||||
u8* buffer = nullptr;
|
|
||||||
auto data = MUST(Detail::StringData::create_uninitialized(count * input.bytes().size(), buffer));
|
|
||||||
|
|
||||||
if (input.bytes().size() == 1) {
|
String result;
|
||||||
memset(buffer, input.bytes().first(), count);
|
size_t input_size = input.bytes().size();
|
||||||
return String { move(data) };
|
MUST(result.replace_with_new_string(count * input_size, [&](Bytes buffer) {
|
||||||
}
|
if (input_size == 1) {
|
||||||
|
buffer.fill(input.bytes().first());
|
||||||
for (size_t i = 0; i < count; ++i) {
|
} else {
|
||||||
memcpy(buffer + (i * input.bytes().size()), input.bytes().data(), input.bytes().size());
|
for (size_t i = 0; i < count; ++i)
|
||||||
}
|
input.bytes().copy_to(buffer.slice(i * input_size, input_size));
|
||||||
return String { data };
|
}
|
||||||
|
return ErrorOr<void> {};
|
||||||
|
}));
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue