forked from p4lang/p4c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcstring.cpp
298 lines (247 loc) · 8.98 KB
/
cstring.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "cstring.h"
#include <algorithm>
#include <cctype>
#include <iomanip>
#include <ios>
#include <string>
#include <unordered_set>
#include "hash.h"
namespace {
enum class table_entry_flags {
none,
no_need_copy = 1 << 0,
require_destruction = 1 << 1,
inplace = 1 << 2,
};
inline table_entry_flags operator&(table_entry_flags l, table_entry_flags r) {
return static_cast<table_entry_flags>(static_cast<int>(l) & static_cast<int>(r));
}
inline table_entry_flags operator|(table_entry_flags l, table_entry_flags r) {
return static_cast<table_entry_flags>(static_cast<int>(l) | static_cast<int>(r));
}
// cache entry, ordered by string length
class table_entry {
std::size_t m_length = 0;
table_entry_flags m_flags = table_entry_flags::none;
union {
const char *m_string;
char m_inplace_string[sizeof(const char *)];
};
public:
// entry ctor, makes copy of passed string
table_entry(const char *string, std::size_t length, table_entry_flags flags)
: m_length(length) {
if ((flags & table_entry_flags::no_need_copy) == table_entry_flags::no_need_copy) {
// No need to copy object, it's view of string, string literal or string allocated
// on heap and wrapped with cstring.
// Inherit require_destruction flag here, because string can be allready
// allocated on heap and wrapped with cstring object, so cstring owns the string now
m_flags = flags & table_entry_flags::require_destruction;
m_string = string;
return;
}
if (length < sizeof(const char *)) {
// String with length less than size of pointer store directly
// in pointer, that hint allows reduce stack fragmentation.
// We can make such optimization because std::unordered_set never
// moves objects in memory on new element insert
std::memcpy(m_inplace_string, string, length);
m_inplace_string[length] = '\0';
m_flags = table_entry_flags::inplace;
} else {
// Make copy of string elseware
auto copy = new char[length + 1];
std::memcpy(copy, string, length);
copy[length] = '\0';
m_string = copy;
// destruction required, because we own copy of string
m_flags = table_entry_flags::require_destruction;
}
}
// table_entry moveable only
table_entry(const table_entry &) = delete;
table_entry(table_entry &&other) : m_length(other.m_length), m_flags(other.m_flags) {
// this object for internal usage only, length will never be accessed
// if object was moved, so do not zero other.m_length here
if (is_inplace()) {
std::memcpy(m_inplace_string, other.m_inplace_string, sizeof(m_inplace_string));
} else {
m_string = other.string();
other.m_string = nullptr;
}
}
~table_entry() {
if ((m_flags & table_entry_flags::require_destruction) ==
table_entry_flags::require_destruction) {
// null pointer checked in operator delete [], so we don't need
// to check it explicitly
delete[] m_string;
}
}
std::size_t length() const { return m_length; }
const char *string() const {
if (is_inplace()) {
return m_inplace_string;
}
return m_string;
}
bool operator==(const table_entry &other) const {
return length() == other.length() && std::memcmp(string(), other.string(), length()) == 0;
}
private:
bool is_inplace() const {
return (m_flags & table_entry_flags::inplace) == table_entry_flags::inplace;
}
};
} // namespace
namespace std {
template <>
struct hash<table_entry> {
std::size_t operator()(const table_entry &entry) const {
return Util::hash(entry.string(), entry.length());
}
};
} // namespace std
namespace {
std::unordered_set<table_entry> &cache() {
static std::unordered_set<table_entry> g_cache;
return g_cache;
}
const char *save_to_cache(const char *string, std::size_t length, table_entry_flags flags) {
if ((flags & table_entry_flags::no_need_copy) == table_entry_flags::no_need_copy) {
return cache().emplace(string, length, flags).first->string();
}
// temporary table_entry, used for searching only. no need to copy string
auto found = cache().find(table_entry(string, length, table_entry_flags::no_need_copy));
if (found == cache().end()) {
return cache().emplace(string, length, flags).first->string();
}
return found->string();
}
} // namespace
void cstring::construct_from_shared(const char *string, std::size_t length) {
str = save_to_cache(string, length, table_entry_flags::none);
}
void cstring::construct_from_unique(const char *string, std::size_t length) {
str = save_to_cache(string, length,
table_entry_flags::no_need_copy | table_entry_flags::require_destruction);
}
void cstring::construct_from_literal(const char *string, std::size_t length) {
str = save_to_cache(string, length, table_entry_flags::no_need_copy);
}
size_t cstring::cache_size(size_t &count) {
size_t rv = 0;
count = cache().size();
for (auto &s : cache()) rv += sizeof(s) + s.length();
return rv;
}
cstring cstring::newline = cstring("\n");
cstring cstring::empty = cstring("");
bool cstring::startsWith(const cstring &prefix) const {
if (prefix.isNullOrEmpty()) return true;
return size() >= prefix.size() && memcmp(str, prefix.str, prefix.size()) == 0;
}
bool cstring::endsWith(const cstring &suffix) const {
if (suffix.isNullOrEmpty()) return true;
return size() >= suffix.size() &&
memcmp(str + size() - suffix.size(), suffix.str, suffix.size()) == 0;
}
cstring cstring::before(const char *at) const { return substr(0, at - str); }
cstring cstring::substr(size_t start, size_t length) const {
if (size() <= start) return cstring::empty;
std::string s = str;
return s.substr(start, length);
}
cstring cstring::replace(char c, char with) const {
char *dup = strdup(c_str());
for (char *p = dup; *p; ++p)
if (*p == c) *p = with;
return cstring(dup);
}
cstring cstring::replace(cstring search, cstring replace) const {
if (search.isNullOrEmpty() || isNullOrEmpty()) return *this;
std::string s_str = str;
std::string s_search = search.str;
std::string s_replace = replace.str;
size_t pos = 0;
while ((pos = s_str.find(s_search, pos)) != std::string::npos) {
s_str.replace(pos, s_search.length(), s_replace);
pos += s_replace.length();
}
return cstring(s_str);
}
cstring cstring::indent(size_t amount) const {
std::string spaces = "";
for (size_t i = 0; i < amount; i++) spaces += " ";
cstring spc = cstring("\n") + spaces;
return cstring(spaces) + replace("\n", spc);
}
// See https://stackoverflow.com/a/33799784/4538702
cstring cstring::escapeJson() const {
std::ostringstream o;
for (size_t i = 0; i < size(); i++) {
char c = get(i);
switch (c) {
case '"':
o << "\\\"";
break;
case '\\':
o << "\\\\";
break;
case '\b':
o << "\\b";
break;
case '\f':
o << "\\f";
break;
case '\n':
o << "\\n";
break;
case '\r':
o << "\\r";
break;
case '\t':
o << "\\t";
break;
default: {
if ('\x00' <= c && c <= '\x1f') {
o << "\\u" << std::hex << std::setw(4) << std::setfill('0')
<< static_cast<int>(c);
} else {
o << c;
}
}
}
}
return cstring(o.str());
}
cstring cstring::toUpper() const {
std::string st = str;
std::transform(st.begin(), st.end(), st.begin(), ::toupper);
cstring ret = cstring::to_cstring(st);
return ret;
}
cstring cstring::toLower() const {
std::string st = str;
std::transform(st.begin(), st.end(), st.begin(), ::tolower);
cstring ret = cstring::to_cstring(st);
return ret;
}
cstring cstring::capitalize() const {
std::string st = str;
st[0] = ::toupper(st[0]);
cstring ret = cstring::to_cstring(st);
return ret;
}