diff --git a/AK/String.cpp b/AK/String.cpp index ab7f3dbcc0..4cb7f874ea 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -106,6 +106,18 @@ String String::empty() return StringImpl::the_empty_stringimpl(); } +bool String::copy_characters_to_buffer(char* buffer, size_t buffer_size) const +{ + // We must fit at least the NUL-terminator. + ASSERT(buffer_size > 0); + + size_t characters_to_copy = min(length(), buffer_size - 1); + __builtin_memcpy(buffer, characters(), characters_to_copy); + buffer[characters_to_copy] = 0; + + return characters_to_copy == length(); +} + String String::isolated_copy() const { if (!m_impl) diff --git a/AK/String.h b/AK/String.h index 1fcd6de7f1..1ddbd9b40e 100644 --- a/AK/String.h +++ b/AK/String.h @@ -146,6 +146,8 @@ public: // Includes NUL-terminator, if non-nullptr. ALWAYS_INLINE const char* characters() const { return m_impl ? m_impl->characters() : nullptr; } + [[nodiscard]] bool copy_characters_to_buffer(char* buffer, size_t buffer_size) const; + ALWAYS_INLINE ReadonlyBytes bytes() const { return m_impl ? m_impl->bytes() : nullptr; } ALWAYS_INLINE const char& operator[](size_t i) const