diff --git a/include/mtl/string.hpp b/include/mtl/string.hpp index 47c5af6..b4ba97e 100644 --- a/include/mtl/string.hpp +++ b/include/mtl/string.hpp @@ -151,7 +151,14 @@ public: template istring& replace(const_iterator first, const_iterator last, T first2, T last2); - size_t copy(char* dest, size_t count, size_t pos = 0) const; + /** + * \brief Copy the string to a buffer + * + * Copies the string to the buffer pointed to by dest. Will always + * copy the string from front to back, never in reverse. The strings + * must not overlap. + */ + size_t copy(char* dest, size_t count = npos, size_t pos = 0) const; void resize(size_t count); void resize(size_t count, char ch); diff --git a/include/mtl/string_view.hpp b/include/mtl/string_view.hpp index ed85290..60c23a0 100644 --- a/include/mtl/string_view.hpp +++ b/include/mtl/string_view.hpp @@ -56,7 +56,14 @@ public: constexpr size_t size() const { return m_size; } constexpr size_t length() const { return m_size; } - size_t copy(char* dest, size_t count, size_t pos = 0) const; + /** + * \brief Copy the string to a buffer + * + * Copies the string to the buffer pointed to by dest. Will always + * copy the string from front to back, never in reverse. The strings + * must not overlap. + */ + size_t copy(char* dest, size_t count = npos, size_t pos = 0) const; // Search diff --git a/src/string.cpp b/src/string.cpp index 6da8ee1..b633142 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -213,6 +213,11 @@ istring& istring::insert(size_t index, const istring& str) { return *this; } +size_t istring::copy(char* dest, size_t count, size_t pos) const { + mtl_hybridcpy(dest, m_str + pos, count); + return count; +} + string_ext::string_ext(char* buf, size_t buf_size) : istring(buf, buf_size - 1, 0) {} diff --git a/src/string_view.cpp b/src/string_view.cpp index add162c..0ddb293 100644 --- a/src/string_view.cpp +++ b/src/string_view.cpp @@ -9,4 +9,13 @@ string_view::string_view(const istring& str) string_view::string_view(const istring& str, size_t size) : m_str(str.c_str()), m_size(size) {} +size_t string_view::copy(char* dest, size_t count, size_t pos) const { + if (count == npos) { + count = m_size - pos; + } + + mtl_hybridcpy(dest, m_str + pos, count); + return count; +} + } // namespace mtl