openMSX
Main Page
Namespaces
Classes
Files
File List
File Members
utils
string_ref.hh
Go to the documentation of this file.
1
#ifndef STRING_REF_HH
2
#define STRING_REF_HH
3
4
#include <string>
5
#include <iterator>
6
#include <iosfwd>
7
#include <cassert>
8
#include <cstring>
9
18
class
string_ref
19
{
20
public
:
21
typedef
size_t
size_type
;
22
typedef
std::ptrdiff_t
difference_type
;
23
typedef
const
char
*
const_iterator
;
24
typedef
std::reverse_iterator<const_iterator>
const_reverse_iterator
;
25
26
static
const
size_type
npos
=
size_type
(-1);
27
28
// construct/copy/assign
29
string_ref
()
30
: dat(nullptr), siz(0) {}
31
string_ref
(
const
string_ref
&
str
)
32
: dat(str.dat), siz(str.siz) {}
33
string_ref
(
const
char
*
str
)
34
: dat(str), siz(str ?
size_type
(strlen(str)) : 0) {}
35
string_ref
(
const
char
*
str
,
size_type
len)
36
: dat(str), siz(len) {
if
(!dat) assert(siz == 0); }
37
string_ref
(
const
char
*
begin
,
const
char
*
end
)
38
: dat(begin), siz(end - begin) {
if
(!dat) assert(siz == 0); }
39
string_ref
(
const
std::string&
str
)
40
: dat(str.
data
()), siz(str.
size
()) {}
41
42
string_ref
&
operator=
(
const
string_ref
& rhs) {
43
dat = rhs.
data
();
44
siz = rhs.
size
();
45
return
*
this
;
46
}
47
48
// iterators
49
const_iterator
begin
()
const
{
return
dat; }
50
const_iterator
end
()
const
{
return
dat + siz; }
51
const_reverse_iterator
rbegin
()
const
{
return
const_reverse_iterator
(
end
()); }
52
const_reverse_iterator
rend
()
const
{
return
const_reverse_iterator
(
begin
()); }
53
54
// capacity
55
size_type
size
()
const
{
return
siz; }
56
bool
empty
()
const
{
return
siz == 0; }
57
//size_type max_size() const;
58
//size_type length() const;
59
60
// element access
61
char
operator[]
(
size_type
i)
const
{
62
assert(i < siz);
63
return
dat[i];
64
}
65
//const char& at(size_type i) const;
66
char
front
()
const
{
return
*dat; }
67
char
back
()
const
{
return
*(dat + siz - 1); }
68
const
char
*
data
()
const
{
return
dat; }
69
70
// Outgoing conversion operators
71
//explicit operator std::string() const; // c++11
72
std::string
str
()
const
;
73
74
// mutators
75
void
clear
() { siz = 0; }
// no need to change 'dat'
76
void
remove_prefix
(
size_type
n);
77
void
remove_suffix
(
size_type
n);
78
void
pop_back
() {
remove_suffix
(1); }
79
void
pop_front
() {
remove_prefix
(1); }
80
81
// string operations with the same semantics as std::string
82
int
compare
(
string_ref
x)
const
;
83
string_ref
substr
(
size_type
pos,
size_type
n =
npos
)
const
;
84
//size_type copy(char* buf) const;
85
size_type
find
(
string_ref
s)
const
;
86
size_type
find
(
char
c)
const
;
87
size_type
rfind
(
string_ref
s)
const
;
88
size_type
rfind
(
char
c)
const
;
89
size_type
find_first_of
(
string_ref
s)
const
;
90
size_type
find_first_of
(
char
c)
const
;
91
//size_type find_first_not_of(string_ref s) const;
92
//size_type find_first_not_of(char c) const;
93
size_type
find_last_of
(
string_ref
s)
const
;
94
size_type
find_last_of
(
char
c)
const
;
95
//size_type find_last_not_of(string_ref s) const;
96
//size_type find_last_not_of(char c) const;
97
98
// new string operations (not part of std::string)
99
bool
starts_with
(
string_ref
x)
const
;
100
bool
ends_with
(
string_ref
x)
const
;
101
102
private
:
103
const
char
* dat;
104
size_type
siz;
105
};
106
107
108
// Comparison operators
109
bool
operator==
(
string_ref
x,
string_ref
y);
110
bool
operator<
(
string_ref
x,
string_ref
y);
111
inline
bool
operator!=
(
string_ref
x,
string_ref
y) {
return
!(x == y); }
112
inline
bool
operator>
(
string_ref
x,
string_ref
y) {
return
(y < x); }
113
inline
bool
operator<=
(
string_ref
x,
string_ref
y) {
return
!(y < x); }
114
inline
bool
operator>=
(
string_ref
x,
string_ref
y) {
return
!(x < y); }
115
116
// numeric conversions
117
int
stoi
(
string_ref
str,
string_ref::size_type
* idx =
nullptr
,
int
base = 0);
118
//long stol (string_ref str, string_ref::size_type* idx = nullptr, int base = 0);
119
unsigned
long
stoul
(
string_ref
str,
string_ref::size_type
* idx =
nullptr
,
int
base = 0);
120
long
long
stoll
(
string_ref
str,
string_ref::size_type
* idx =
nullptr
,
int
base = 0);
121
//unsigned long long stoull(string_ref str, string_ref::size_type* idx = nullptr, int base = 0);
122
//float stof (string_ref str, string_ref::size_type* idx = nullptr);
123
//double stod (string_ref str, string_ref::size_type* idx = nullptr);
124
//long double stold (string_ref str, string_ref::size_type* idx = nullptr);
125
126
// concatenation (this is not part of the std::string_ref proposal)
127
std::string
operator+
(
string_ref
x,
string_ref
y);
128
std::string
operator+
(
char
x,
string_ref
y);
129
std::string
operator+
(
string_ref
x,
char
y);
130
131
std::ostream&
operator<<
(std::ostream& os,
string_ref
str);
132
133
#endif
Generated on Mon May 20 2013 12:17:00 for openMSX by
1.8.1.2