1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5 //
6 #include "util/string_util.h"
7
8 #include <errno.h>
9 #include <stdarg.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <algorithm>
13 #include <cinttypes>
14 #include <cmath>
15 #include <sstream>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 #include "port/port.h"
20 #include "port/sys_time.h"
21 #include "rocksdb/slice.h"
22
23 namespace ROCKSDB_NAMESPACE {
24
25 const std::string kNullptrString = "nullptr";
26
StringSplit(const std::string & arg,char delim)27 std::vector<std::string> StringSplit(const std::string& arg, char delim) {
28 std::vector<std::string> splits;
29 std::stringstream ss(arg);
30 std::string item;
31 while (std::getline(ss, item, delim)) {
32 splits.push_back(item);
33 }
34 return splits;
35 }
36
37 // for micros < 10ms, print "XX us".
38 // for micros < 10sec, print "XX ms".
39 // for micros >= 10 sec, print "XX sec".
40 // for micros <= 1 hour, print Y:X M:S".
41 // for micros > 1 hour, print Z:Y:X H:M:S".
AppendHumanMicros(uint64_t micros,char * output,int len,bool fixed_format)42 int AppendHumanMicros(uint64_t micros, char* output, int len,
43 bool fixed_format) {
44 if (micros < 10000 && !fixed_format) {
45 return snprintf(output, len, "%" PRIu64 " us", micros);
46 } else if (micros < 10000000 && !fixed_format) {
47 return snprintf(output, len, "%.3lf ms",
48 static_cast<double>(micros) / 1000);
49 } else if (micros < 1000000l * 60 && !fixed_format) {
50 return snprintf(output, len, "%.3lf sec",
51 static_cast<double>(micros) / 1000000);
52 } else if (micros < 1000000ll * 60 * 60 && !fixed_format) {
53 return snprintf(output, len, "%02" PRIu64 ":%05.3f M:S",
54 micros / 1000000 / 60,
55 static_cast<double>(micros % 60000000) / 1000000);
56 } else {
57 return snprintf(output, len, "%02" PRIu64 ":%02" PRIu64 ":%05.3f H:M:S",
58 micros / 1000000 / 3600, (micros / 1000000 / 60) % 60,
59 static_cast<double>(micros % 60000000) / 1000000);
60 }
61 }
62
63 // for sizes >=10TB, print "XXTB"
64 // for sizes >=10GB, print "XXGB"
65 // etc.
66 // append file size summary to output and return the len
AppendHumanBytes(uint64_t bytes,char * output,int len)67 int AppendHumanBytes(uint64_t bytes, char* output, int len) {
68 const uint64_t ull10 = 10;
69 if (bytes >= ull10 << 40) {
70 return snprintf(output, len, "%" PRIu64 "TB", bytes >> 40);
71 } else if (bytes >= ull10 << 30) {
72 return snprintf(output, len, "%" PRIu64 "GB", bytes >> 30);
73 } else if (bytes >= ull10 << 20) {
74 return snprintf(output, len, "%" PRIu64 "MB", bytes >> 20);
75 } else if (bytes >= ull10 << 10) {
76 return snprintf(output, len, "%" PRIu64 "KB", bytes >> 10);
77 } else {
78 return snprintf(output, len, "%" PRIu64 "B", bytes);
79 }
80 }
81
AppendNumberTo(std::string * str,uint64_t num)82 void AppendNumberTo(std::string* str, uint64_t num) {
83 char buf[30];
84 snprintf(buf, sizeof(buf), "%" PRIu64, num);
85 str->append(buf);
86 }
87
AppendEscapedStringTo(std::string * str,const Slice & value)88 void AppendEscapedStringTo(std::string* str, const Slice& value) {
89 for (size_t i = 0; i < value.size(); i++) {
90 char c = value[i];
91 if (c >= ' ' && c <= '~') {
92 str->push_back(c);
93 } else {
94 char buf[10];
95 snprintf(buf, sizeof(buf), "\\x%02x",
96 static_cast<unsigned int>(c) & 0xff);
97 str->append(buf);
98 }
99 }
100 }
101
NumberToString(uint64_t num)102 std::string NumberToString(uint64_t num) {
103 std::string r;
104 AppendNumberTo(&r, num);
105 return r;
106 }
107
NumberToHumanString(int64_t num)108 std::string NumberToHumanString(int64_t num) {
109 char buf[19];
110 int64_t absnum = num < 0 ? -num : num;
111 if (absnum < 10000) {
112 snprintf(buf, sizeof(buf), "%" PRIi64, num);
113 } else if (absnum < 10000000) {
114 snprintf(buf, sizeof(buf), "%" PRIi64 "K", num / 1000);
115 } else if (absnum < 10000000000LL) {
116 snprintf(buf, sizeof(buf), "%" PRIi64 "M", num / 1000000);
117 } else {
118 snprintf(buf, sizeof(buf), "%" PRIi64 "G", num / 1000000000);
119 }
120 return std::string(buf);
121 }
122
BytesToHumanString(uint64_t bytes)123 std::string BytesToHumanString(uint64_t bytes) {
124 const char* size_name[] = {"KB", "MB", "GB", "TB"};
125 double final_size = static_cast<double>(bytes);
126 size_t size_idx;
127
128 // always start with KB
129 final_size /= 1024;
130 size_idx = 0;
131
132 while (size_idx < 3 && final_size >= 1024) {
133 final_size /= 1024;
134 size_idx++;
135 }
136
137 char buf[20];
138 snprintf(buf, sizeof(buf), "%.2f %s", final_size, size_name[size_idx]);
139 return std::string(buf);
140 }
141
TimeToHumanString(int unixtime)142 std::string TimeToHumanString(int unixtime) {
143 char time_buffer[80];
144 time_t rawtime = unixtime;
145 struct tm tInfo;
146 struct tm* timeinfo = localtime_r(&rawtime, &tInfo);
147 assert(timeinfo == &tInfo);
148 strftime(time_buffer, 80, "%c", timeinfo);
149 return std::string(time_buffer);
150 }
151
EscapeString(const Slice & value)152 std::string EscapeString(const Slice& value) {
153 std::string r;
154 AppendEscapedStringTo(&r, value);
155 return r;
156 }
157
ConsumeDecimalNumber(Slice * in,uint64_t * val)158 bool ConsumeDecimalNumber(Slice* in, uint64_t* val) {
159 uint64_t v = 0;
160 int digits = 0;
161 while (!in->empty()) {
162 char c = (*in)[0];
163 if (c >= '0' && c <= '9') {
164 ++digits;
165 const unsigned int delta = (c - '0');
166 static const uint64_t kMaxUint64 = ~static_cast<uint64_t>(0);
167 if (v > kMaxUint64 / 10 ||
168 (v == kMaxUint64 / 10 && delta > kMaxUint64 % 10)) {
169 // Overflow
170 return false;
171 }
172 v = (v * 10) + delta;
173 in->remove_prefix(1);
174 } else {
175 break;
176 }
177 }
178 *val = v;
179 return (digits > 0);
180 }
181
isSpecialChar(const char c)182 bool isSpecialChar(const char c) {
183 if (c == '\\' || c == '#' || c == ':' || c == '\r' || c == '\n') {
184 return true;
185 }
186 return false;
187 }
188
189 namespace {
190 using CharMap = std::pair<char, char>;
191 }
192
UnescapeChar(const char c)193 char UnescapeChar(const char c) {
194 static const CharMap convert_map[] = {{'r', '\r'}, {'n', '\n'}};
195
196 auto iter = std::find_if(std::begin(convert_map), std::end(convert_map),
197 [c](const CharMap& p) { return p.first == c; });
198
199 if (iter == std::end(convert_map)) {
200 return c;
201 }
202 return iter->second;
203 }
204
EscapeChar(const char c)205 char EscapeChar(const char c) {
206 static const CharMap convert_map[] = {{'\n', 'n'}, {'\r', 'r'}};
207
208 auto iter = std::find_if(std::begin(convert_map), std::end(convert_map),
209 [c](const CharMap& p) { return p.first == c; });
210
211 if (iter == std::end(convert_map)) {
212 return c;
213 }
214 return iter->second;
215 }
216
EscapeOptionString(const std::string & raw_string)217 std::string EscapeOptionString(const std::string& raw_string) {
218 std::string output;
219 for (auto c : raw_string) {
220 if (isSpecialChar(c)) {
221 output += '\\';
222 output += EscapeChar(c);
223 } else {
224 output += c;
225 }
226 }
227
228 return output;
229 }
230
UnescapeOptionString(const std::string & escaped_string)231 std::string UnescapeOptionString(const std::string& escaped_string) {
232 bool escaped = false;
233 std::string output;
234
235 for (auto c : escaped_string) {
236 if (escaped) {
237 output += UnescapeChar(c);
238 escaped = false;
239 } else {
240 if (c == '\\') {
241 escaped = true;
242 continue;
243 }
244 output += c;
245 }
246 }
247 return output;
248 }
249
trim(const std::string & str)250 std::string trim(const std::string& str) {
251 if (str.empty()) return std::string();
252 size_t start = 0;
253 size_t end = str.size() - 1;
254 while (isspace(str[start]) != 0 && start < end) {
255 ++start;
256 }
257 while (isspace(str[end]) != 0 && start < end) {
258 --end;
259 }
260 if (start <= end) {
261 return str.substr(start, end - start + 1);
262 }
263 return std::string();
264 }
265
266 #ifndef ROCKSDB_LITE
267
ParseBoolean(const std::string & type,const std::string & value)268 bool ParseBoolean(const std::string& type, const std::string& value) {
269 if (value == "true" || value == "1") {
270 return true;
271 } else if (value == "false" || value == "0") {
272 return false;
273 }
274 throw std::invalid_argument(type);
275 }
276
ParseUint32(const std::string & value)277 uint32_t ParseUint32(const std::string& value) {
278 uint64_t num = ParseUint64(value);
279 if ((num >> 32LL) == 0) {
280 return static_cast<uint32_t>(num);
281 } else {
282 throw std::out_of_range(value);
283 }
284 }
285
ParseInt32(const std::string & value)286 int32_t ParseInt32(const std::string& value) {
287 int64_t num = ParseInt64(value);
288 if (num <= port::kMaxInt32 && num >= port::kMinInt32) {
289 return static_cast<int32_t>(num);
290 } else {
291 throw std::out_of_range(value);
292 }
293 }
294
295 #endif
296
ParseUint64(const std::string & value)297 uint64_t ParseUint64(const std::string& value) {
298 size_t endchar;
299 #ifndef CYGWIN
300 uint64_t num = std::stoull(value.c_str(), &endchar);
301 #else
302 char* endptr;
303 uint64_t num = std::strtoul(value.c_str(), &endptr, 0);
304 endchar = endptr - value.c_str();
305 #endif
306
307 if (endchar < value.length()) {
308 char c = value[endchar];
309 if (c == 'k' || c == 'K')
310 num <<= 10LL;
311 else if (c == 'm' || c == 'M')
312 num <<= 20LL;
313 else if (c == 'g' || c == 'G')
314 num <<= 30LL;
315 else if (c == 't' || c == 'T')
316 num <<= 40LL;
317 }
318
319 return num;
320 }
321
ParseInt64(const std::string & value)322 int64_t ParseInt64(const std::string& value) {
323 size_t endchar;
324 #ifndef CYGWIN
325 int64_t num = std::stoll(value.c_str(), &endchar);
326 #else
327 char* endptr;
328 int64_t num = std::strtoll(value.c_str(), &endptr, 0);
329 endchar = endptr - value.c_str();
330 #endif
331
332 if (endchar < value.length()) {
333 char c = value[endchar];
334 if (c == 'k' || c == 'K')
335 num <<= 10LL;
336 else if (c == 'm' || c == 'M')
337 num <<= 20LL;
338 else if (c == 'g' || c == 'G')
339 num <<= 30LL;
340 else if (c == 't' || c == 'T')
341 num <<= 40LL;
342 }
343
344 return num;
345 }
346
ParseInt(const std::string & value)347 int ParseInt(const std::string& value) {
348 size_t endchar;
349 #ifndef CYGWIN
350 int num = std::stoi(value.c_str(), &endchar);
351 #else
352 char* endptr;
353 int num = std::strtoul(value.c_str(), &endptr, 0);
354 endchar = endptr - value.c_str();
355 #endif
356
357 if (endchar < value.length()) {
358 char c = value[endchar];
359 if (c == 'k' || c == 'K')
360 num <<= 10;
361 else if (c == 'm' || c == 'M')
362 num <<= 20;
363 else if (c == 'g' || c == 'G')
364 num <<= 30;
365 }
366
367 return num;
368 }
369
ParseDouble(const std::string & value)370 double ParseDouble(const std::string& value) {
371 #ifndef CYGWIN
372 return std::stod(value);
373 #else
374 return std::strtod(value.c_str(), 0);
375 #endif
376 }
377
ParseSizeT(const std::string & value)378 size_t ParseSizeT(const std::string& value) {
379 return static_cast<size_t>(ParseUint64(value));
380 }
381
ParseVectorInt(const std::string & value)382 std::vector<int> ParseVectorInt(const std::string& value) {
383 std::vector<int> result;
384 size_t start = 0;
385 while (start < value.size()) {
386 size_t end = value.find(':', start);
387 if (end == std::string::npos) {
388 result.push_back(ParseInt(value.substr(start)));
389 break;
390 } else {
391 result.push_back(ParseInt(value.substr(start, end - start)));
392 start = end + 1;
393 }
394 }
395 return result;
396 }
397
SerializeIntVector(const std::vector<int> & vec,std::string * value)398 bool SerializeIntVector(const std::vector<int>& vec, std::string* value) {
399 *value = "";
400 for (size_t i = 0; i < vec.size(); ++i) {
401 if (i > 0) {
402 *value += ":";
403 }
404 *value += ToString(vec[i]);
405 }
406 return true;
407 }
408
409 } // namespace ROCKSDB_NAMESPACE
410