forked from bloomberg/blazingmq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmqu_stringutil.cpp
More file actions
276 lines (234 loc) · 8.13 KB
/
Copy pathbmqu_stringutil.cpp
File metadata and controls
276 lines (234 loc) · 8.13 KB
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
// Copyright 2017-2023 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// 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 <bmqu_stringutil.h>
#include <bmqscm_version.h>
// BDE
#include <bdlb_chartype.h>
#include <bsl_algorithm.h>
#include <bsl_bitset.h>
#include <bsl_cctype.h>
#include <bsl_climits.h>
#include <bsl_functional.h>
#include <bsls_assert.h>
#include <bsls_performancehint.h>
namespace BloombergLP {
namespace bmqu {
namespace {
/// Rearrange characters in place within the contiguous sequence indicated
/// by the specified `[begin, end)` such that all repeated runs of any
/// characters belonging to the set indicated by the specified
/// `[allowlistBegin, allowlistEnd)` are reduced to one appearance of the
/// character. Return a pointer to the new end of the sequence. Note that
/// this function is similar to `bsl::remove_if` with a special predicate.
char* removeIfPrecededBySame(char* begin,
char* end,
const char* allowlistBegin,
const char* allowlistEnd)
{
bsl::bitset<UCHAR_MAX + 1> allowlist;
for (const char* iter = allowlistBegin; iter != allowlistEnd; ++iter) {
allowlist[static_cast<unsigned char>(*iter)] = true;
}
char* result = begin;
for (; begin != end; ++begin) {
// If this is *not* a repeated item from the allowlist, then copy it
// into its final place.
if (!allowlist[static_cast<unsigned char>(*begin)] ||
*begin != *(begin - 1)) {
*result++ = *begin;
}
}
return result;
}
/// Return false if the specified char `c` is a space character, and true
/// otherwise.
bool isNotSpace(char c)
{
return !bsl::isspace(c);
}
} // close unnamed namespace
// -----------------
// struct StringUtil
// -----------------
bool StringUtil::contains(const bsl::string& str,
const bslstl::StringRef& substr)
{
return str.find(substr, 0) != bsl::string::npos;
}
bool StringUtil::startsWith(const bslstl::StringRef& str,
const bslstl::StringRef& prefix,
size_t offset)
{
if (offset > str.length() || ((str.length() - offset) < prefix.length())) {
// There is not enough characters in 'str' after 'offset' to contain
// the full 'prefix' string. Note that the first check is covered by
// the second, but needed due to unsigned operation.
return false; // RETURN
}
size_t idx = 0;
while (idx < prefix.length()) {
if (str[offset++] != prefix[idx++]) {
return false; // RETURN
}
}
return true;
}
bool StringUtil::endsWith(const bslstl::StringRef& str,
const bslstl::StringRef& suffix)
{
if (str.length() < suffix.length()) {
// There is not enough characters in 'str' to contain the full 'suffix'
// string.
return false; // RETURN
}
int i = static_cast<int>(str.length() - 1);
int j = static_cast<int>(suffix.length() - 1);
while ((i >= 0) && (j >= 0)) {
if (str[i--] != suffix[j--]) {
return false; // RETURN
}
}
return true;
}
bool StringUtil::isPrintable(const bslstl::StringRef& str)
{
for (bslstl::StringRef::const_iterator it = str.begin(); it != str.end();
++it) {
if (BSLS_PERFORMANCEHINT_PREDICT_UNLIKELY(
!bdlb::CharType::isPrint(*it))) {
BSLS_PERFORMANCEHINT_UNLIKELY_HINT;
return false; // RETURN
}
}
return true;
}
bsl::string& StringUtil::trim(bsl::string* str)
{
return ltrim(&rtrim(str));
}
bsl::string& StringUtil::ltrim(bsl::string* str)
{
str->erase(str->begin(),
bsl::find_if(str->begin(), str->end(), &isNotSpace));
return *str;
}
bsl::string& StringUtil::rtrim(bsl::string* str)
{
str->erase(bsl::find_if(str->rbegin(), str->rend(), &isNotSpace).base(),
str->end());
return *str;
}
bsl::vector<bslstl::StringRef>
StringUtil::strTokenizeRef(const bsl::string& str,
const bslstl::StringRef& delims)
{
bsl::vector<bslstl::StringRef> res;
if (str.empty()) {
return res; // RETURN
}
if (delims.length() == 0) {
res.push_back(bslstl::StringRef(str.c_str(), str.length()));
return res; // RETURN
}
bsl::string::size_type idx = 0, delimIdx = 0, len = 0;
while ((delimIdx = str.find_first_of(delims, idx)) != bsl::string::npos) {
if ((len = delimIdx - idx) != 0) {
res.push_back(bslstl::StringRef(str.c_str() + idx, len));
}
else {
// Put an empty ""
res.push_back(bslstl::StringRef(str.c_str() + idx, 0));
}
idx = delimIdx + 1;
}
if (idx != str.length()) {
res.push_back(
bslstl::StringRef(str.c_str() + idx, str.length() - idx));
}
else {
// Put an empty ""
res.push_back(bslstl::StringRef(str.c_str() + idx, 0));
}
return res;
}
bool StringUtil::match(const bslstl::StringRef& str,
const bslstl::StringRef& pattern)
{
// This implementation is taken almost exactly from the blog post:
// https://research.swtch.com/glob
const int strLength = static_cast<int>(str.size());
const int patternLength = static_cast<int>(pattern.size());
int i = 0; // index into 'str'
int j = 0; // index into 'pattern'
// When a '*' is encountered, we have a decision about whether it consumes
// any of the 'str'. We proceed by assuming it doesn't, but save the
// position we'd try if it did consume at least a character. The reason
// that this algorithm is efficient is that (remarkably) we only ever need
// to keep track of the most recently encountered '*'.
int iSaved = -1, jSaved = -1; // '-1' means "unset"
while (i < strLength || j < patternLength) {
if (j < patternLength) {
switch (pattern[j]) {
case '?':
if (i < strLength) {
++i;
++j;
continue;
}
break;
case '*':
iSaved = i + 1;
jSaved = j;
++j;
continue;
default:
if (i < strLength && str[i] == pattern[j]) {
++i;
++j;
continue;
}
}
}
// If we previously encountered a '*' and there still is hope of
// matching by having it consume more of 'str', then try that.
if (iSaved > 0 && iSaved <= strLength) {
i = iSaved;
j = jSaved;
continue;
}
// We would have hit a 'continue' already if there were a chance that
// we have a match.
return false; // RETURN
}
// Got to the end of both 'str' and 'pattern' without encountering the
// 'false' case, above, so we have a match.
return true;
}
bsl::string& StringUtil::squeeze(bsl::string* str,
const bslstl::StringRef& characters)
{
BSLS_ASSERT_SAFE(str);
bsl::string& strRef = *str;
if (strRef.size() > 1) {
strRef.erase(removeIfPrecededBySame(strRef.begin() + 1,
strRef.end(),
characters.begin(),
characters.end()),
strRef.end());
}
return strRef;
}
} // close package namespace
} // close enterprise namespace