Implement string::copy and string_view::copy

This commit is contained in:
Myles Busig 2024-03-07 15:57:29 -07:00
parent 674371a66d
commit cb4f9d2861
4 changed files with 30 additions and 2 deletions

View File

@ -151,7 +151,14 @@ public:
template <typename T> template <typename T>
istring& replace(const_iterator first, const_iterator last, T first2, T last2); 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);
void resize(size_t count, char ch); void resize(size_t count, char ch);

View File

@ -56,7 +56,14 @@ public:
constexpr size_t size() const { return m_size; } constexpr size_t size() const { return m_size; }
constexpr size_t length() 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 // Search

View File

@ -213,6 +213,11 @@ istring& istring::insert(size_t index, const istring& str) {
return *this; 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) {} string_ext::string_ext(char* buf, size_t buf_size) : istring(buf, buf_size - 1, 0) {}

View File

@ -9,4 +9,13 @@ string_view::string_view(const istring& str)
string_view::string_view(const istring& str, size_t size) string_view::string_view(const istring& str, size_t size)
: m_str(str.c_str()), m_size(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 } // namespace mtl