blob: a12508475a8b9289971f922b85bd0de34f14bee2 (
plain)
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
|
/*
* mrustc common tools
* - by John Hodge (Mutabah)
*
* tools/common/helpers.h
* - General helper classes
*/
// TODO: Replace this header with src/includ/string_view.hpp
#pragma once
#include <string>
#include <cstring>
#include <iostream>
namespace helpers {
class string_view
{
const char* m_start;
const size_t m_len;
public:
string_view(const char* s, size_t n):
m_start(s), m_len(n)
{
}
bool operator==(const ::std::string& s) const {
return *this == s.c_str();
}
bool operator==(const char* s) const {
if(::std::strncmp(m_start, s, m_len) != 0)
return false;
return s[m_len] == '\0';
}
char operator[](size_t n) const {
return m_start[n];
}
operator ::std::string() const {
return ::std::string { m_start, m_start + m_len };
}
friend ::std::string& operator+=(::std::string& x, const string_view& sv) {
x.append(sv.m_start, sv.m_start+sv.m_len);
return x;
}
friend ::std::ostream& operator<<(::std::ostream& os, const string_view& sv) {
os.write(sv.m_start, sv.m_len);
return os;
}
const char* begin() const {
return m_start;
}
const char* end() const {
return m_start+m_len;
}
};
} // namespace helpers
|