Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit 479fbfa

Browse files
committed
string_view: implement modifiers API for string_view
- add remove_prefix, remove_suffix and swap for string_view
1 parent 1ec67b0 commit 479fbfa

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

include/libpmemobj++/string_view.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <limits>
1414
#include <stdexcept>
1515
#include <string>
16+
#include <utility>
1617

1718
#if __cpp_lib_string_view
1819
#include <string_view>
@@ -88,6 +89,10 @@ class basic_string_view {
8889
constexpr const_reference front() const noexcept;
8990
constexpr const_reference back() const noexcept;
9091

92+
void remove_prefix(size_type n);
93+
void remove_suffix(size_type n);
94+
void swap(basic_string_view &v) noexcept;
95+
9196
int compare(const basic_string_view &other) const noexcept;
9297

9398
private:
@@ -320,6 +325,47 @@ basic_string_view<CharT, Traits>::front() const noexcept
320325
return operator[](0);
321326
}
322327

328+
/**
329+
* Moves the start of the view forward by n characters.
330+
* The behavior is undefined if n > size().
331+
*
332+
* @param[in] n number of characters to remove from the start of the view
333+
*/
334+
template <typename CharT, typename Traits>
335+
void
336+
basic_string_view<CharT, Traits>::remove_prefix(size_type n)
337+
{
338+
data_ += n;
339+
size_ -= n;
340+
}
341+
342+
/**
343+
* Moves the end of the view back by n characters.
344+
* The behavior is undefined if n > size().
345+
*
346+
* @param[in] n number of characters to remove from the end of the view
347+
*/
348+
template <typename CharT, typename Traits>
349+
void
350+
basic_string_view<CharT, Traits>::remove_suffix(size_type n)
351+
{
352+
size_ -= n;
353+
}
354+
355+
/**
356+
* Exchanges the view with that of v.
357+
*
358+
* @param[in] v view to swap with
359+
*/
360+
template <typename CharT, typename Traits>
361+
void
362+
basic_string_view<CharT, Traits>::swap(
363+
basic_string_view<CharT, Traits> &v) noexcept
364+
{
365+
std::swap(data_, v.data_);
366+
std::swap(size_, v.size_);
367+
}
368+
323369
/**
324370
* Compares this string_view with other. Works in the same way as
325371
* std::basic_string::compare.

0 commit comments

Comments
 (0)