AK: Add String::repeated(StringView, size_t count)

This commit is contained in:
Matthew Olsson 2021-05-23 16:14:48 -07:00 committed by Ali Mohammad Pur
parent be6e4b6f3c
commit 777c232e16
Notes: sideshowbarker 2024-07-18 17:26:17 +09:00
2 changed files with 13 additions and 0 deletions

View file

@ -234,6 +234,7 @@ bool String::ends_with(char ch) const
return false; return false;
return characters()[length() - 1] == ch; return characters()[length() - 1] == ch;
} }
String String::repeated(char ch, size_t count) String String::repeated(char ch, size_t count)
{ {
if (!count) if (!count)
@ -244,6 +245,17 @@ String String::repeated(char ch, size_t count)
return *impl; return *impl;
} }
String String::repeated(const StringView& string, size_t count)
{
if (!count || string.is_empty())
return empty();
char* buffer;
auto impl = StringImpl::create_uninitialized(count * string.length(), buffer);
for (size_t i = 0; i < count; i++)
__builtin_memcpy(buffer + i * string.length(), string.characters_without_null_termination(), string.length());
return *impl;
}
String String::bijective_base_from(size_t value, unsigned base, StringView map) String String::bijective_base_from(size_t value, unsigned base, StringView map)
{ {
if (map.is_null()) if (map.is_null())

View file

@ -92,6 +92,7 @@ public:
String(const FlyString&); String(const FlyString&);
[[nodiscard]] static String repeated(char, size_t count); [[nodiscard]] static String repeated(char, size_t count);
[[nodiscard]] static String repeated(const StringView&, size_t count);
[[nodiscard]] static String bijective_base_from(size_t value, unsigned base = 26, StringView map = {}); [[nodiscard]] static String bijective_base_from(size_t value, unsigned base = 26, StringView map = {});