-
Notifications
You must be signed in to change notification settings - Fork 117
/
str.hh
165 lines (151 loc) · 4.5 KB
/
str.hh
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2013 President and Fellows of Harvard College
* Copyright (c) 2012-2013 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#ifndef STR_HH
#define STR_HH
#include "string_base.hh"
#include <stdarg.h>
#include <stdio.h>
namespace lcdf {
struct Str : public String_base<Str> {
typedef Str substring_type;
typedef Str argument_type;
const char *s;
int len;
Str()
: s(0), len(0) {
}
template <typename T>
Str(const String_base<T>& x)
: s(x.data()), len(x.length()) {
}
Str(const char* s_)
: s(s_), len(strlen(s_)) {
}
Str(const char* s_, int len_)
: s(s_), len(len_) {
}
Str(const unsigned char* s_, int len_)
: s(reinterpret_cast<const char*>(s_)), len(len_) {
}
Str(const char *first, const char *last)
: s(first), len(last - first) {
precondition(first <= last);
}
Str(const unsigned char *first, const unsigned char *last)
: s(reinterpret_cast<const char*>(first)), len(last - first) {
precondition(first <= last);
}
Str(const std::string& str)
: s(str.data()), len(str.length()) {
}
Str(const uninitialized_type &unused) {
(void) unused;
}
static const Str maxkey;
void assign() {
s = 0;
len = 0;
}
template <typename T>
void assign(const String_base<T> &x) {
s = x.data();
len = x.length();
}
void assign(const char *s_) {
s = s_;
len = strlen(s_);
}
void assign(const char *s_, int len_) {
s = s_;
len = len_;
}
const char *data() const {
return s;
}
int length() const {
return len;
}
char* mutable_data() {
return const_cast<char*>(s);
}
Str prefix(int lenx) const {
return Str(s, lenx < len ? lenx : len);
}
Str substring(const char *first, const char *last) const {
if (first <= last && first >= s && last <= s + len)
return Str(first, last);
else
return Str();
}
Str substring(const unsigned char *first, const unsigned char *last) const {
const unsigned char *u = reinterpret_cast<const unsigned char*>(s);
if (first <= last && first >= u && last <= u + len)
return Str(first, last);
else
return Str();
}
Str fast_substring(const char *first, const char *last) const {
assert(begin() <= first && first <= last && last <= end());
return Str(first, last);
}
Str fast_substring(const unsigned char *first, const unsigned char *last) const {
assert(ubegin() <= first && first <= last && last <= uend());
return Str(first, last);
}
Str ltrim() const {
return String_generic::ltrim(*this);
}
Str rtrim() const {
return String_generic::rtrim(*this);
}
Str trim() const {
return String_generic::trim(*this);
}
long to_i() const { // XXX does not handle negative
long x = 0;
int p;
for (p = 0; p < len && s[p] >= '0' && s[p] <= '9'; ++p)
x = (x * 10) + s[p] - '0';
return p == len && p != 0 ? x : -1;
}
static Str snprintf(char *buf, size_t size, const char *fmt, ...) {
va_list val;
va_start(val, fmt);
int n = vsnprintf(buf, size, fmt, val);
va_end(val);
return Str(buf, n);
}
};
struct inline_string : public String_base<inline_string> {
int len;
char s[0];
const char *data() const {
return s;
}
int length() const {
return len;
}
size_t size() const {
return sizeof(inline_string) + len;
}
static size_t size(int len) {
return sizeof(inline_string) + len;
}
};
} // namespace lcdf
LCDF_MAKE_STRING_HASH(lcdf::Str)
LCDF_MAKE_STRING_HASH(lcdf::inline_string)
#endif