Skip to content

Commit

Permalink
add erase and erase_index for string
Browse files Browse the repository at this point in the history
  • Loading branch information
trcrsired committed Sep 30, 2024
1 parent 45ccea2 commit 26e2855
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions include/fast_io_dsal/impl/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,20 +833,19 @@ class
this->imp.curr_ptr = newcurrptr;
return retptr;
}
constexpr size_type insert_index_impl(size_type idx, char_type const *otherptr, size_type othern) noexcept
constexpr void insert_index_impl(size_type idx, char_type const *otherptr, size_type othern) noexcept
{
auto beginptr{this->imp.begin_ptr};
size_type sz{static_cast<size_type>(this->imp.curr_ptr - beginptr)};
if (sz < idx)
{
::fast_io::fast_terminate();
}
auto itr{this->insert_impl(beginptr + idx, otherptr, othern)};
return static_cast<size_type>(itr - this->imp.begin_ptr);
this->insert_impl(beginptr + idx, otherptr, othern);
}

public:
constexpr size_type insert_index(size_type idx, string_view_type vw) noexcept
constexpr void insert_index(size_type idx, string_view_type vw) noexcept
{
return this->insert_index_impl(idx, vw.data(), vw.size());
}
Expand All @@ -865,10 +864,45 @@ class
return this->insert_impl(const_cast<pointer>(ptr), vw.data(), vw.size());
}
}
constexpr size_type insert_index(size_type idx, basic_string const &other) noexcept
constexpr void insert_index(size_type idx, basic_string const &other) noexcept
{
return this->insert_index_impl(idx, other.data(), other.size());
}

private:
constexpr pointer erase_impl(pointer first, pointer last) noexcept
{
*(this->imp.curr_ptr = ::fast_io::freestanding::my_copy(last, this->imp.curr_ptr, first)) = 0;
}

public:
constexpr iterator erase(const_iterator first, const_iterator last) noexcept
{
#ifdef __cpp_if_consteval
if consteval
#else
if (__builtin_is_constant_evaluated())
#endif
{
auto beginptr{this->imp.begin_ptr};
return this->erase_impl(beginptr + (first - beginptr), beginptr + (last - beginptr));
}
else
{
return this->erase_impl(const_cast<pointer>(first), const_cast<pointer>(last));
}
}
constexpr void erase_index(size_type firstidx, size_type lastidx) noexcept
{
auto beginptr{this->imp.begin_ptr};
auto currptr{this->imp.curr_ptr};
size_type const sz{static_cast<size_type>(currptr - beginptr)};
if (lastidx < firstidx || sz <= lastidx) [[unlikely]]
{
::fast_io::fast_terminate();
}
this->erase_impl(beginptr + firstidx, beginptr + lastidx);
}
constexpr void swap(basic_string &other) noexcept
{
::std::swap(other.imp, this->imp);
Expand Down

0 comments on commit 26e2855

Please sign in to comment.