text
stringlengths
0
2.2M
string_view::const_reference string_view::at(size_type pos) const {
if (pos < size_)
return data_[pos];
CAF_RAISE_ERROR(std::out_of_range, "string_view::at out of range");
}
// -- modifiers ----------------------------------------------------------------
void string_view::remove_prefix(size_type n) {
if (n < size()) {
data_ += n;
size_ -= n;
} else {
size_ = 0;
}
}
void string_view::remove_suffix(size_type n) {
if (n < size())
size_ -= n;
else
size_ = 0;
}
void string_view::assign(const_pointer data, size_type len) {
data_ = data;
size_ = len;
}
// -- algorithms ---------------------------------------------------------------
string_view::size_type string_view::copy(pointer dest, size_type n,
size_type pos) const {
if (pos > size_)
CAF_RAISE_ERROR("string_view::copy out of range");
auto first = begin() + pos;
auto end = first + std::min(n, size() - pos);
auto cpy_end = std::copy(first, end, dest);
return static_cast<size_type>(std::distance(dest, cpy_end));
}
string_view string_view::substr(size_type pos, size_type n) const noexcept {
if (pos >= size())
return {};
return {data_ + pos, std::min(size_ - pos, n)};
}
int string_view::compare(string_view str) const noexcept {
// TODO: use lexicographical_compare_three_way when switching to C++20
auto i0 = begin();
auto e0 = end();
auto i1 = str.begin();
auto e1 = str.end();
while (i0 != e0 && i1 != e1)
if (auto diff = *i0++ - *i1++; diff != 0)
return diff;
if (i0 == e0)
return i1 != e1 ? -1 : 0;
else
return i1 == e1 ? 1 : 0;
}
int string_view::compare(size_type pos1, size_type n1,
string_view str) const noexcept {
return substr(pos1, n1).compare(str);
}
int string_view::compare(size_type pos1, size_type n1, string_view str,
size_type pos2, size_type n2) const noexcept {
return substr(pos1, n1).compare(str.substr(pos2, n2));
}
int string_view::compare(const_pointer str) const noexcept {
return strncmp(data(), str, size());
}
int string_view::compare(size_type pos, size_type n,
const_pointer str) const noexcept {
return substr(pos, n).compare(str);
}
int string_view::compare(size_type pos1, size_type n1,
const_pointer str, size_type n2) const noexcept {
return substr(pos1, n1).compare(string_view{str, n2});
}
size_type string_view::find(string_view str, size_type pos) const noexcept {
string_view tmp;
if (pos < size_)
tmp.assign(data_ + pos, size_ - pos);
auto first = tmp.begin();
auto last = tmp.end();
auto i = std::search(first, last, str.begin(), str.end());
if (i != last)
return static_cast<size_type>(std::distance(first, i)) + pos;
return npos;
}
size_type string_view::find(value_type ch, size_type pos) const noexcept {
return find(string_view{&ch, 1}, pos);