-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconstexpr_string_view.hpp
91 lines (72 loc) · 1.92 KB
/
constexpr_string_view.hpp
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
88
89
90
91
#ifndef META_CRUSH_SAGA_CONSTEXPR_STRING_VIEW_HPP
#define META_CRUSH_SAGA_CONSTEXPR_STRING_VIEW_HPP
#include <algorithm>
#include <cstddef>
#include <stdexcept>
#include "constexpr_string.hpp"
class constexpr_string_view
{
public:
using const_iterator = const char*;
public:
template <std::size_t N>
constexpr constexpr_string_view(const char (&a)[N])
: ptr_(a), size_(N - 1)
{
}
template <std::size_t N>
constexpr constexpr_string_view(const constexpr_string<N>& s)
: ptr_(s.data()), size_(s.size())
{
}
constexpr constexpr_string_view(const char* a, int size)
: ptr_(a), size_(size)
{
}
constexpr char operator[](std::size_t n) const
{
return n < size_ ? ptr_[n] : throw std::out_of_range("");
}
constexpr std::size_t size() const
{
return size_;
}
constexpr bool startswith(const constexpr_string_view& other)
{
std::size_t i = 0;
for (; i < other.size() && i < size(); ++i) {
if (ptr_[i] != other[i]) {
return false;
}
}
return i == other.size();
}
constexpr constexpr_string_view substr(std::size_t pos, std::size_t len = -1) const
{
if (pos > size_) {
throw std::runtime_error("out of range");
}
const std::size_t new_size = (len == static_cast<std::size_t>(-1)) ? (size_ - pos) : std::min(len, (size_ - pos));
return constexpr_string_view(ptr_ + pos, new_size);
}
constexpr const_iterator begin() const
{
return ptr_;
}
constexpr const_iterator end() const
{
return ptr_ + size_;
}
constexpr const_iterator cbegin() const
{
return ptr_;
}
constexpr const_iterator cend() const
{
return ptr_ + size_;
}
private:
const char* ptr_;
std::size_t size_;
};
#endif // META_CRUSH_SAGA_CONSTEXPR_STRING_VIEW_HPP