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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
// Copyright 2016 Google 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.!
#ifndef UTIL_H_
#define UTIL_H_
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include "common.h"
#include "sentencepiece_processor.h"
#include "third_party/absl/strings/string_view.h"
namespace sentencepiece {
template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
for (const auto n : v) {
out << " " << n;
}
return out;
}
// String utilities
namespace string_util {
inline absl::string_view ToSV(util::min_string_view data) {
return absl::string_view(data.data(), data.size());
}
struct string_view_hash {
// DJB hash function.
inline size_t operator()(const absl::string_view &sp) const {
size_t hash = 5381;
for (size_t i = 0; i < sp.size(); ++i) {
hash = ((hash << 5) + hash) + sp[i];
}
return hash;
}
};
inline std::string ToLower(absl::string_view arg) {
std::string lower_value = std::string(arg);
std::transform(lower_value.begin(), lower_value.end(), lower_value.begin(),
::tolower);
return lower_value;
}
inline std::string ToUpper(absl::string_view arg) {
std::string upper_value = std::string(arg);
std::transform(upper_value.begin(), upper_value.end(), upper_value.begin(),
::toupper);
return upper_value;
}
template <typename Target>
inline bool lexical_cast(absl::string_view arg, Target *result) {
std::stringstream ss;
return (ss << arg.data() && ss >> *result);
}
template <>
inline bool lexical_cast(absl::string_view arg, bool *result) {
const char *kTrue[] = {"1", "t", "true", "y", "yes"};
const char *kFalse[] = {"0", "f", "false", "n", "no"};
std::string lower_value = std::string(arg);
std::transform(lower_value.begin(), lower_value.end(), lower_value.begin(),
::tolower);
for (size_t i = 0; i < 5; ++i) {
if (lower_value == kTrue[i]) {
*result = true;
return true;
} else if (lower_value == kFalse[i]) {
*result = false;
return true;
}
}
return false;
}
template <>
inline bool lexical_cast(absl::string_view arg, std::string *result) {
*result = std::string(arg);
return true;
}
std::vector<std::string> Split(const std::string &str, const std::string &delim,
bool allow_empty = false);
std::vector<absl::string_view> SplitPiece(absl::string_view str,
absl::string_view delim,
bool allow_empty = false);
std::string Join(const std::vector<std::string> &tokens,
absl::string_view delim);
std::string Join(const std::vector<int> &tokens, absl::string_view delim);
std::string StringReplace(absl::string_view s, absl::string_view oldsub,
absl::string_view newsub, bool replace_all);
void StringReplace(absl::string_view s, absl::string_view oldsub,
absl::string_view newsub, bool replace_all,
std::string *res);
template <typename T>
inline bool DecodePOD(absl::string_view str, T *result) {
CHECK_NOTNULL(result);
if (sizeof(*result) != str.size()) {
return false;
}
memcpy(result, str.data(), sizeof(T));
return true;
}
template <typename T>
inline std::string EncodePOD(const T &value) {
std::string s;
s.resize(sizeof(T));
memcpy(const_cast<char *>(s.data()), &value, sizeof(T));
return s;
}
inline bool StartsWith(absl::string_view text, absl::string_view prefix) {
return prefix.empty() ||
(text.size() >= prefix.size() &&
memcmp(text.data(), prefix.data(), prefix.size()) == 0);
}
inline bool EndsWith(absl::string_view text, absl::string_view suffix) {
return suffix.empty() || (text.size() >= suffix.size() &&
memcmp(text.data() + (text.size() - suffix.size()),
suffix.data(), suffix.size()) == 0);
}
inline bool ConsumePrefix(absl::string_view *str, absl::string_view expected) {
if (!StartsWith(*str, expected)) return false;
str->remove_prefix(expected.size());
return true;
}
template <typename T>
inline std::string IntToHex(T value) {
std::ostringstream os;
os << std::hex << std::uppercase << value;
return os.str();
}
template <typename T>
inline T HexToInt(absl::string_view value) {
T n;
std::istringstream is(value.data());
is >> std::hex >> n;
return n;
}
template <typename T>
inline size_t Itoa(T val, char *s) {
char *org = s;
if (val < 0) {
*s++ = '-';
val = -val;
}
char *t = s;
T mod = 0;
while (val) {
mod = val % 10;
*t++ = static_cast<char>(mod) + '0';
val /= 10;
}
if (s == t) {
*t++ = '0';
}
*t = '\0';
std::reverse(s, t);
return static_cast<size_t>(t - org);
}
template <typename T>
std::string SimpleItoa(T val) {
char buf[32];
Itoa<T>(val, buf);
return std::string(buf);
}
// Return length of a single UTF-8 source character
inline size_t OneCharLen(const char *src) {
return "\1\1\1\1\1\1\1\1\1\1\1\1\2\2\3\4"[(*src & 0xFF) >> 4];
}
// Return (x & 0xC0) == 0x80;
// Since trail bytes are always in [0x80, 0xBF], we can optimize:
inline bool IsTrailByte(char x) { return static_cast<signed char>(x) < -0x40; }
inline bool IsValidCodepoint(char32 c) {
return (static_cast<uint32>(c) < 0xD800) || (c >= 0xE000 && c <= 0x10FFFF);
}
bool IsStructurallyValid(absl::string_view str);
using UnicodeText = std::vector<char32>;
char32 DecodeUTF8(const char *begin, const char *end, size_t *mblen);
inline char32 DecodeUTF8(absl::string_view input, size_t *mblen) {
return DecodeUTF8(input.data(), input.data() + input.size(), mblen);
}
inline bool IsValidDecodeUTF8(absl::string_view input, size_t *mblen) {
const char32 c = DecodeUTF8(input, mblen);
return c != kUnicodeError || *mblen == 3;
}
size_t EncodeUTF8(char32 c, char *output);
std::string UnicodeCharToUTF8(const char32 c);
UnicodeText UTF8ToUnicodeText(absl::string_view utf8);
std::string UnicodeTextToUTF8(const UnicodeText &utext);
} // namespace string_util
// IO related utilities.
namespace io {
class InputBuffer {
public:
explicit InputBuffer(absl::string_view filename);
util::Status status() const;
~InputBuffer();
bool ReadLine(std::string *line);
private:
util::Status status_;
std::istream *is_;
};
class OutputBuffer {
public:
explicit OutputBuffer(absl::string_view filename);
util::Status status() const;
~OutputBuffer();
bool Write(absl::string_view text);
bool WriteLine(absl::string_view text);
private:
util::Status status_;
std::ostream *os_;
};
} // namespace io
// other map/ptr utilties
namespace port {
template <class Collection, class Key>
bool ContainsKey(const Collection &collection, const Key &key) {
return collection.find(key) != collection.end();
}
template <class Collection>
const typename Collection::value_type::second_type &FindOrDie(
const Collection &collection,
const typename Collection::value_type::first_type &key) {
typename Collection::const_iterator it = collection.find(key);
CHECK(it != collection.end()) << "Map key not found: " << key;
return it->second;
}
template <class Collection>
const typename Collection::value_type::second_type &FindWithDefault(
const Collection &collection,
const typename Collection::value_type::first_type &key,
const typename Collection::value_type::second_type &value) {
typename Collection::const_iterator it = collection.find(key);
if (it == collection.end()) {
return value;
}
return it->second;
}
template <class Collection>
bool InsertIfNotPresent(Collection *const collection,
const typename Collection::value_type &vt) {
return collection->insert(vt).second;
}
template <class Collection>
bool InsertIfNotPresent(
Collection *const collection,
const typename Collection::value_type::first_type &key,
const typename Collection::value_type::second_type &value) {
return InsertIfNotPresent(collection,
typename Collection::value_type(key, value));
}
template <class Collection>
void InsertOrDie(Collection *const collection,
const typename Collection::value_type::first_type &key,
const typename Collection::value_type::second_type &data) {
CHECK(InsertIfNotPresent(collection, key, data)) << "duplicate key";
}
// hash
inline void mix(uint64 &a, uint64 &b, uint64 &c) { // 64bit version
a -= b;
a -= c;
a ^= (c >> 43);
b -= c;
b -= a;
b ^= (a << 9);
c -= a;
c -= b;
c ^= (b >> 8);
a -= b;
a -= c;
a ^= (c >> 38);
b -= c;
b -= a;
b ^= (a << 23);
c -= a;
c -= b;
c ^= (b >> 5);
a -= b;
a -= c;
a ^= (c >> 35);
b -= c;
b -= a;
b ^= (a << 49);
c -= a;
c -= b;
c ^= (b >> 11);
a -= b;
a -= c;
a ^= (c >> 12);
b -= c;
b -= a;
b ^= (a << 18);
c -= a;
c -= b;
c ^= (b >> 22);
}
inline uint64 FingerprintCat(uint64 x, uint64 y) {
uint64 b = 0xe08c1d668b756f82; // more of the golden ratio
mix(x, b, y);
return y;
}
// Trait to select overloads and return types for MakeUnique.
template <typename T>
struct MakeUniqueResult {
using scalar = std::unique_ptr<T>;
};
template <typename T>
struct MakeUniqueResult<T[]> {
using array = std::unique_ptr<T[]>;
};
template <typename T, size_t N>
struct MakeUniqueResult<T[N]> {
using invalid = void;
};
// MakeUnique<T>(...) is an early implementation of C++14 std::make_unique.
// It is designed to be 100% compatible with std::make_unique so that the
// eventual switchover will be a simple renaming operation.
template <typename T, typename... Args>
typename MakeUniqueResult<T>::scalar MakeUnique(Args &&... args) { // NOLINT
return std::unique_ptr<T>(
new T(std::forward<Args>(args)...)); // NOLINT(build/c++11)
}
// Overload for array of unknown bound.
// The allocation of arrays needs to use the array form of new,
// and cannot take element constructor arguments.
template <typename T>
typename MakeUniqueResult<T>::array MakeUnique(size_t n) {
return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]());
}
// Reject arrays of known bound.
template <typename T, typename... Args>
typename MakeUniqueResult<T>::invalid MakeUnique(Args &&... /* args */) =
delete; // NOLINT
template <typename T>
void STLDeleteElements(std::vector<T *> *vec) {
for (auto item : *vec) {
delete item;
}
vec->clear();
}
} // namespace port
namespace util {
std::string StrError(int errnum);
inline Status OkStatus() { return Status(); }
#define DECLARE_ERROR(FUNC, CODE) \
inline util::Status FUNC##Error(absl::string_view str) { \
return util::Status(error::CODE, str.data()); \
} \
inline bool Is##FUNC(const util::Status &status) { \
return status.code() == error::CODE; \
}
DECLARE_ERROR(Cancelled, CANCELLED)
DECLARE_ERROR(InvalidArgument, INVALID_ARGUMENT)
DECLARE_ERROR(NotFound, NOT_FOUND)
DECLARE_ERROR(AlreadyExists, ALREADY_EXISTS)
DECLARE_ERROR(ResourceExhausted, RESOURCE_EXHAUSTED)
DECLARE_ERROR(Unavailable, UNAVAILABLE)
DECLARE_ERROR(FailedPrecondition, FAILED_PRECONDITION)
DECLARE_ERROR(OutOfRange, OUT_OF_RANGE)
DECLARE_ERROR(Unimplemented, UNIMPLEMENTED)
DECLARE_ERROR(Internal, INTERNAL)
DECLARE_ERROR(Aborted, ABORTED)
DECLARE_ERROR(DeadlineExceeded, DEADLINE_EXCEEDED)
DECLARE_ERROR(DataLoss, DATA_LOSS)
DECLARE_ERROR(Unknown, UNKNOWN)
DECLARE_ERROR(PermissionDenied, PERMISSION_DENIED)
DECLARE_ERROR(Unauthenticated, UNAUTHENTICATED)
class StatusBuilder {
public:
explicit StatusBuilder(error::Code code) : code_(code) {}
template <typename T>
StatusBuilder &operator<<(const T &value) {
os_ << value;
return *this;
}
operator Status() const { return Status(code_, os_.str()); }
private:
error::Code code_;
std::ostringstream os_;
};
#define CHECK_OR_RETURN(condition) \
if (condition) { \
} else /* NOLINT */ \
return ::sentencepiece::util::StatusBuilder(util::error::INTERNAL) \
<< __FILE__ << "(" << __LINE__ << ") [" << #condition << "] "
#define CHECK_EQ_OR_RETURN(a, b) CHECK_OR_RETURN((a) == (b))
#define CHECK_NE_OR_RETURN(a, b) CHECK_OR_RETURN((a) != (b))
#define CHECK_GE_OR_RETURN(a, b) CHECK_OR_RETURN((a) >= (b))
#define CHECK_LE_OR_RETURN(a, b) CHECK_OR_RETURN((a) <= (b))
#define CHECK_GT_OR_RETURN(a, b) CHECK_OR_RETURN((a) > (b))
#define CHECK_LT_OR_RETURN(a, b) CHECK_OR_RETURN((a) < (b))
} // namespace util
} // namespace sentencepiece
#endif // UTIL_H_
|