blob: 9a50019233100b8ba92e2fa95fac20dfddb7da0d (
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
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
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* include/string_view.hpp
* - Clone of the `string_view` class (introduced in C++17)
*/
#pragma once
#include <string>
#include <cstddef> // size_t
#include <iostream> // ostream
namespace stdx {
using namespace std;
class string_view
{
const char* m_start;
const char* m_end;
public:
string_view():
m_start(nullptr), m_end(nullptr)
{
}
string_view(const char* s, const char* e):
m_start(s), m_end(e)
{
if(!(s <= e))
throw ::std::invalid_argument("start must be before end for string_view");
}
string_view(const char* s):
m_start(s), m_end(s)
{
while(*m_end)
m_end ++;
}
string_view(const string& s):
m_start(s.data()), m_end(m_start + s.size())
{
}
size_t size() const {
return m_end - m_start;
}
bool operator==(const string_view& x) const { return cmp(x) == 0; }
bool operator!=(const string_view& x) const { return cmp(x) != 0; }
bool operator< (const string_view& x) const { return cmp(x) < 0; }
bool operator> (const string_view& x) const { return cmp(x) > 0; }
bool operator<=(const string_view& x) const { return cmp(x) <= 0; }
bool operator>=(const string_view& x) const { return cmp(x) >= 0; }
bool operator==(const char* x) const { return cmp(string_view(x)) == 0; }
bool operator!=(const char* x) const { return cmp(string_view(x)) != 0; }
bool operator< (const char* x) const { return cmp(string_view(x)) < 0; }
bool operator> (const char* x) const { return cmp(string_view(x)) > 0; }
bool operator<=(const char* x) const { return cmp(string_view(x)) <= 0; }
bool operator>=(const char* x) const { return cmp(string_view(x)) >= 0; }
bool operator==(const string& x) const { return cmp(string_view(x)) == 0; }
bool operator!=(const string& x) const { return cmp(string_view(x)) != 0; }
bool operator< (const string& x) const { return cmp(string_view(x)) < 0; }
bool operator> (const string& x) const { return cmp(string_view(x)) > 0; }
bool operator<=(const string& x) const { return cmp(string_view(x)) <= 0; }
bool operator>=(const string& x) const { return cmp(string_view(x)) >= 0; }
friend ::std::ostream& operator<<(::std::ostream& os, const string_view& x) {
for(const char* s = x.m_start; s != x.m_end; s++)
os << *s;
return os;
}
private:
int cmp(const string_view& x) const {
const char *a, *b;
for( a = m_start, b = x.m_start; a != m_end && b != x.m_end; a++, b++)
{
if( *a != *b ) {
return *a < *b ? -1 : 1;
}
}
if( a == m_end && b == x.m_end )
return 0;
if( a == m_end )
return -1;
else
return 1;
}
};
}
|