1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:14:58 +00:00

AK: Add StringView::copy_characters_to_buffer()

This commit is contained in:
Tim Schumacher 2022-03-29 02:52:20 +02:00 committed by Andreas Kling
parent 3237efc661
commit 8209c2b570
2 changed files with 14 additions and 0 deletions

View file

@ -182,6 +182,18 @@ StringView StringView::substring_view_starting_after_substring(StringView substr
return { remaining_characters, remaining_length };
}
bool StringView::copy_characters_to_buffer(char* buffer, size_t buffer_size) const
{
// We must fit at least the NUL-terminator.
VERIFY(buffer_size > 0);
size_t characters_to_copy = min(m_length, buffer_size - 1);
__builtin_memcpy(buffer, m_characters, characters_to_copy);
buffer[characters_to_copy] = 0;
return characters_to_copy == m_length;
}
template<typename T>
Optional<T> StringView::to_int() const
{