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
|
// 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.!
#include "util.h"
#include <iostream>
namespace sentencepiece {
namespace string_util {
template <typename T>
std::vector<T> SplitInternal(const T &str, const T &delim, bool allow_empty) {
std::vector<T> result;
size_t current_pos = 0;
size_t found_pos = 0;
while ((found_pos = str.find_first_of(delim, current_pos)) != T::npos) {
if ((allow_empty && found_pos >= current_pos) ||
(!allow_empty && found_pos > current_pos)) {
result.push_back(str.substr(current_pos, found_pos - current_pos));
}
current_pos = found_pos + 1;
}
if (str.size() > current_pos) {
result.push_back(str.substr(current_pos, str.size() - current_pos));
}
return result;
}
std::vector<std::string> Split(const std::string &str, const std::string &delim,
bool allow_empty) {
return SplitInternal<std::string>(str, delim, allow_empty);
}
std::vector<absl::string_view> SplitPiece(absl::string_view str,
absl::string_view delim,
bool allow_empty) {
return SplitInternal<absl::string_view>(str, delim, allow_empty);
}
std::string Join(const std::vector<std::string> &tokens,
absl::string_view delim) {
std::string result;
if (!tokens.empty()) {
result.append(tokens[0]);
}
for (size_t i = 1; i < tokens.size(); ++i) {
result.append(delim.data(), delim.size());
result.append(tokens[i]);
}
return result;
}
std::string Join(const std::vector<int> &tokens, absl::string_view delim) {
std::string result;
char buf[32];
if (!tokens.empty()) {
const size_t len = Itoa(tokens[0], buf);
result.append(buf, len);
}
for (size_t i = 1; i < tokens.size(); ++i) {
result.append(delim.data(), delim.size());
const size_t len = Itoa(tokens[i], buf);
result.append(buf, len);
}
return result;
}
std::string StringReplace(absl::string_view s, absl::string_view oldsub,
absl::string_view newsub, bool replace_all) {
std::string ret;
StringReplace(s, oldsub, newsub, replace_all, &ret);
return ret;
}
void StringReplace(absl::string_view s, absl::string_view oldsub,
absl::string_view newsub, bool replace_all,
std::string *res) {
if (oldsub.empty()) {
res->append(s.data(), s.size());
return;
}
absl::string_view::size_type start_pos = 0;
do {
const absl::string_view::size_type pos = s.find(oldsub, start_pos);
if (pos == absl::string_view::npos) {
break;
}
res->append(s.data() + start_pos, pos - start_pos);
res->append(newsub.data(), newsub.size());
start_pos = pos + oldsub.size();
} while (replace_all);
res->append(s.data() + start_pos, s.size() - start_pos);
}
// mblen sotres the number of bytes consumed after decoding.
char32 DecodeUTF8(const char *begin, const char *end, size_t *mblen) {
const size_t len = end - begin;
if (static_cast<unsigned char>(begin[0]) < 0x80) {
*mblen = 1;
return static_cast<unsigned char>(begin[0]);
} else if (len >= 2 && (begin[0] & 0xE0) == 0xC0) {
const char32 cp = (((begin[0] & 0x1F) << 6) | ((begin[1] & 0x3F)));
if (IsTrailByte(begin[1]) && cp >= 0x0080 && IsValidCodepoint(cp)) {
*mblen = 2;
return cp;
}
} else if (len >= 3 && (begin[0] & 0xF0) == 0xE0) {
const char32 cp = (((begin[0] & 0x0F) << 12) | ((begin[1] & 0x3F) << 6) |
((begin[2] & 0x3F)));
if (IsTrailByte(begin[1]) && IsTrailByte(begin[2]) && cp >= 0x0800 &&
IsValidCodepoint(cp)) {
*mblen = 3;
return cp;
}
} else if (len >= 4 && (begin[0] & 0xf8) == 0xF0) {
const char32 cp = (((begin[0] & 0x07) << 18) | ((begin[1] & 0x3F) << 12) |
((begin[2] & 0x3F) << 6) | ((begin[3] & 0x3F)));
if (IsTrailByte(begin[1]) && IsTrailByte(begin[2]) &&
IsTrailByte(begin[3]) && cp >= 0x10000 && IsValidCodepoint(cp)) {
*mblen = 4;
return cp;
}
}
// Invalid UTF-8.
*mblen = 1;
return kUnicodeError;
}
bool IsStructurallyValid(absl::string_view str) {
const char *begin = str.data();
const char *end = str.data() + str.size();
size_t mblen = 0;
while (begin < end) {
const char32 c = DecodeUTF8(begin, end, &mblen);
if (c == kUnicodeError && mblen != 3) return false;
if (!IsValidCodepoint(c)) return false;
begin += mblen;
}
return true;
}
size_t EncodeUTF8(char32 c, char *output) {
if (c <= 0x7F) {
*output = static_cast<char>(c);
return 1;
}
if (c <= 0x7FF) {
output[1] = 0x80 | (c & 0x3F);
c >>= 6;
output[0] = 0xC0 | c;
return 2;
}
// if `c` is out-of-range, convert it to REPLACEMENT CHARACTER (U+FFFD).
// This treatment is the same as the original runetochar.
if (c > 0x10FFFF) c = kUnicodeError;
if (c <= 0xFFFF) {
output[2] = 0x80 | (c & 0x3F);
c >>= 6;
output[1] = 0x80 | (c & 0x3F);
c >>= 6;
output[0] = 0xE0 | c;
return 3;
}
output[3] = 0x80 | (c & 0x3F);
c >>= 6;
output[2] = 0x80 | (c & 0x3F);
c >>= 6;
output[1] = 0x80 | (c & 0x3F);
c >>= 6;
output[0] = 0xF0 | c;
return 4;
}
std::string UnicodeCharToUTF8(const char32 c) { return UnicodeTextToUTF8({c}); }
UnicodeText UTF8ToUnicodeText(absl::string_view utf8) {
UnicodeText uc;
const char *begin = utf8.data();
const char *end = utf8.data() + utf8.size();
while (begin < end) {
size_t mblen;
const char32 c = DecodeUTF8(begin, end, &mblen);
uc.push_back(c);
begin += mblen;
}
return uc;
}
std::string UnicodeTextToUTF8(const UnicodeText &utext) {
char buf[8];
std::string result;
for (const char32 c : utext) {
const size_t mblen = EncodeUTF8(c, buf);
result.append(buf, mblen);
}
return result;
}
} // namespace string_util
namespace io {
InputBuffer::InputBuffer(absl::string_view filename)
: is_(filename.empty() ? &std::cin
: new std::ifstream(WPATH(filename.data()))) {
if (!*is_)
status_ = util::StatusBuilder(util::error::NOT_FOUND)
<< "\"" << filename.data() << "\": " << util::StrError(errno);
}
InputBuffer::~InputBuffer() {
if (is_ != &std::cin) {
delete is_;
}
}
util::Status InputBuffer::status() const { return status_; }
bool InputBuffer::ReadLine(std::string *line) {
return static_cast<bool>(std::getline(*is_, *line));
}
OutputBuffer::OutputBuffer(absl::string_view filename)
: os_(filename.empty()
? &std::cout
: new std::ofstream(WPATH(filename.data()), OUTPUT_MODE)) {
if (!*os_)
status_ = util::StatusBuilder(util::error::PERMISSION_DENIED)
<< "\"" << filename.data() << "\": " << util::StrError(errno);
}
OutputBuffer::~OutputBuffer() {
if (os_ != &std::cout) {
delete os_;
}
}
util::Status OutputBuffer::status() const { return status_; }
bool OutputBuffer::Write(absl::string_view text) {
os_->write(text.data(), text.size());
return os_->good();
}
bool OutputBuffer::WriteLine(absl::string_view text) {
return Write(text) && Write("\n");
}
} // namespace io
namespace util {
std::string StrError(int errnum) {
constexpr int kStrErrorSize = 1024;
char buffer[kStrErrorSize];
char *str = nullptr;
#if defined(_GLIBC__) && defined(_GNU_SOURCE)
str = strerror_r(errnum, buffer, kStrErrorSize - 1);
#elif defined(_WIN32)
strerror_s(buffer, kStrErrorSize - 1, errnum);
str = buffer;
#else
strerror_r(errnum, buffer, kStrErrorSize - 1);
str = buffer;
#endif
std::ostringstream os;
os << str << " Error #" << errnum;
return os.str();
}
} // namespace util
} // namespace sentencepiece
|