-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsub_string.cpp
More file actions
87 lines (70 loc) · 1.32 KB
/
sub_string.cpp
File metadata and controls
87 lines (70 loc) · 1.32 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "sub_string.h"
#include <cassert>
sub_string::sub_string()
: begin_(nullptr)
, end_(nullptr)
{}
sub_string::sub_string(char const* begin, char const* end)
: begin_(begin)
, end_(end)
{}
sub_string::sub_string(std::string const& str)
: sub_string(str.data(), str.data() + str.size())
{}
void sub_string::advance(size_t i)
{
assert(i <= size());
begin_ += i;
}
char const& sub_string::operator[](size_t i) const
{
assert(i < size());
return begin_[i];
}
void sub_string::begin(char const* arg)
{
begin_ = arg;
}
void sub_string::end(char const* arg)
{
end_ = arg;
}
char const* sub_string::begin() const
{
return begin_;
}
char const* sub_string::end() const
{
return end_;
}
bool sub_string::empty() const
{
return begin_ == end_;
}
size_t sub_string::size() const
{
return end_ - begin_;
}
const char *sub_string::data() const
{
return begin_;
}
bool sub_string::has_prefix(sub_string other) const
{
if (size() < other.size())
return false;
return std::equal(other.begin_, other.end_, begin_);
}
bool sub_string::try_drop_prefix(sub_string other)
{
if (has_prefix(other))
{
advance(other.size());
return true;
}
return false;
}
std::string sub_string::as_string() const
{
return std::string{begin_, end_};
}