forked from jeremy-rifkin/cpptrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_view.cpp
More file actions
63 lines (52 loc) · 1.62 KB
/
string_view.cpp
File metadata and controls
63 lines (52 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "utils/string_view.hpp"
#include "utils/error.hpp"
#include "utils/microfmt.hpp"
#include <algorithm>
#include <cstring>
CPPTRACE_BEGIN_NAMESPACE
namespace detail {
constexpr std::size_t string_view::npos;
char string_view::operator[](size_t i) const {
ASSERT(i < size());
return ptr[i];
}
char string_view::at(size_t i) const {
if(i >= size()) {
throw std::runtime_error(microfmt::format("Out of bounds access {} >= {}", i, size()));
}
return ptr[i];
}
bool string_view::starts_with(string_view str) const {
return substr(0, str.size()) == str;
}
bool string_view::ends_with(string_view str) const {
if(size() < str.size()) {
return false;
}
return substr(size() - str.size(), str.size()) == str;
}
std::size_t string_view::find_last_of(string_view chars) const {
if(empty() || chars.empty()) {
return npos;
}
std::size_t pos = size();
while(pos-- > 0) {
if(std::find(chars.begin(), chars.end(), ptr[pos]) != chars.end()) {
return pos;
}
}
return npos;
}
bool operator==(string_view a, string_view b) {
return a.size() == b.size() && std::memcmp(a.data(), b.data(), a.size()) == 0;
}
constexpr std::size_t cstring_view::npos;
cstring_view cstring_view::substr(std::size_t pos) const {
ASSERT(pos <= count);
return {ptr + pos, count - pos};
}
void cstring_view::check_null() const {
ASSERT(ptr[count] == 0);
}
}
CPPTRACE_END_NAMESPACE