-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringView.cpp
More file actions
31 lines (23 loc) · 833 Bytes
/
StringView.cpp
File metadata and controls
31 lines (23 loc) · 833 Bytes
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
#include "StringView.h"
StringView::StringView(const char* begin, const char* end) : begin(begin), end(end) {}
StringView::StringView(const char* string) : StringView(string, string + strlen(string)) {}
StringView::StringView(const MyString& string) : StringView(string.c_str()) {}
size_t StringView::length() const {
return end - begin;
}
char StringView::operator[](size_t ind) const {
return begin[ind];
}
StringView StringView::substr(size_t from, size_t length) const {
if (begin + from + length > end)
throw std::length_error("Substring out of range!");
return StringView(begin + from, begin + from + length);
}
std::ostream& operator<<(std::ostream& os, const StringView& strView) {
const char* it = strView.begin;
while (it != strView.end) {
os << *it;
it++;
}
return os;
}