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 <stdio.h>
10 #include <stdlib.h>
11 #include <algorithm>
12 #include <cinttypes>
13 #include <cmath>
14 #include <sstream>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 #include "port/port.h"
19 #include "port/sys_time.h"
20 #include "rocksdb/slice.h"
21 
22 #ifndef __has_cpp_attribute
23 #define ROCKSDB_HAS_CPP_ATTRIBUTE(x) 0
24 #else
25 #define ROCKSDB_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
26 #endif
27 
28 #if ROCKSDB_HAS_CPP_ATTRIBUTE(maybe_unused) && __cplusplus >= 201703L
29 #define ROCKSDB_MAYBE_UNUSED [[maybe_unused]]
30 #elif ROCKSDB_HAS_CPP_ATTRIBUTE(gnu::unused) || __GNUC__
31 #define ROCKSDB_MAYBE_UNUSED [[gnu::unused]]
32 #else
33 #define ROCKSDB_MAYBE_UNUSED
34 #endif
35 
36 namespace ROCKSDB_NAMESPACE {
37 
38 const std::string kNullptrString = "nullptr";
39 
StringSplit(const std::string & arg,char delim)40 std::vector<std::string> StringSplit(const std::string& arg, char delim) {
41   std::vector<std::string> splits;
42   std::stringstream ss(arg);
43   std::string item;
44   while (std::getline(ss, item, delim)) {
45     splits.push_back(item);
46   }
47   return splits;
48 }
49 
50 // for micros < 10ms, print "XX us".
51 // for micros < 10sec, print "XX ms".
52 // for micros >= 10 sec, print "XX sec".
53 // for micros <= 1 hour, print Y:X M:S".
54 // for micros > 1 hour, print Z:Y:X H:M:S".
AppendHumanMicros(uint64_t micros,char * output,int len,bool fixed_format)55 int AppendHumanMicros(uint64_t micros, char* output, int len,
56                       bool fixed_format) {
57   if (micros < 10000 && !fixed_format) {
58     return snprintf(output, len, "%" PRIu64 " us", micros);
59   } else if (micros < 10000000 && !fixed_format) {
60     return snprintf(output, len, "%.3lf ms",
61                     static_cast<double>(micros) / 1000);
62   } else if (micros < 1000000l * 60 && !fixed_format) {
63     return snprintf(output, len, "%.3lf sec",
64                     static_cast<double>(micros) / 1000000);
65   } else if (micros < 1000000ll * 60 * 60 && !fixed_format) {
66     return snprintf(output, len, "%02" PRIu64 ":%05.3f M:S",
67                     micros / 1000000 / 60,
68                     static_cast<double>(micros % 60000000) / 1000000);
69   } else {
70     return snprintf(output, len, "%02" PRIu64 ":%02" PRIu64 ":%05.3f H:M:S",
71                     micros / 1000000 / 3600, (micros / 1000000 / 60) % 60,
72                     static_cast<double>(micros % 60000000) / 1000000);
73   }
74 }
75 
76 // for sizes >=10TB, print "XXTB"
77 // for sizes >=10GB, print "XXGB"
78 // etc.
79 // append file size summary to output and return the len
AppendHumanBytes(uint64_t bytes,char * output,int len)80 int AppendHumanBytes(uint64_t bytes, char* output, int len) {
81   const uint64_t ull10 = 10;
82   if (bytes >= ull10 << 40) {
83     return snprintf(output, len, "%" PRIu64 "TB", bytes >> 40);
84   } else if (bytes >= ull10 << 30) {
85     return snprintf(output, len, "%" PRIu64 "GB", bytes >> 30);
86   } else if (bytes >= ull10 << 20) {
87     return snprintf(output, len, "%" PRIu64 "MB", bytes >> 20);
88   } else if (bytes >= ull10 << 10) {
89     return snprintf(output, len, "%" PRIu64 "KB", bytes >> 10);
90   } else {
91     return snprintf(output, len, "%" PRIu64 "B", bytes);
92   }
93 }
94 
AppendNumberTo(std::string * str,uint64_t num)95 void AppendNumberTo(std::string* str, uint64_t num) {
96   char buf[30];
97   snprintf(buf, sizeof(buf), "%" PRIu64, num);
98   str->append(buf);
99 }
100 
AppendEscapedStringTo(std::string * str,const Slice & value)101 void AppendEscapedStringTo(std::string* str, const Slice& value) {
102   for (size_t i = 0; i < value.size(); i++) {
103     char c = value[i];
104     if (c >= ' ' && c <= '~') {
105       str->push_back(c);
106     } else {
107       char buf[10];
108       snprintf(buf, sizeof(buf), "\\x%02x",
109                static_cast<unsigned int>(c) & 0xff);
110       str->append(buf);
111     }
112   }
113 }
114 
NumberToString(uint64_t num)115 std::string NumberToString(uint64_t num) {
116   std::string r;
117   AppendNumberTo(&r, num);
118   return r;
119 }
120 
NumberToHumanString(int64_t num)121 std::string NumberToHumanString(int64_t num) {
122   char buf[19];
123   int64_t absnum = num < 0 ? -num : num;
124   if (absnum < 10000) {
125     snprintf(buf, sizeof(buf), "%" PRIi64, num);
126   } else if (absnum < 10000000) {
127     snprintf(buf, sizeof(buf), "%" PRIi64 "K", num / 1000);
128   } else if (absnum < 10000000000LL) {
129     snprintf(buf, sizeof(buf), "%" PRIi64 "M", num / 1000000);
130   } else {
131     snprintf(buf, sizeof(buf), "%" PRIi64 "G", num / 1000000000);
132   }
133   return std::string(buf);
134 }
135 
BytesToHumanString(uint64_t bytes)136 std::string BytesToHumanString(uint64_t bytes) {
137   const char* size_name[] = {"KB", "MB", "GB", "TB"};
138   double final_size = static_cast<double>(bytes);
139   size_t size_idx;
140 
141   // always start with KB
142   final_size /= 1024;
143   size_idx = 0;
144 
145   while (size_idx < 3 && final_size >= 1024) {
146     final_size /= 1024;
147     size_idx++;
148   }
149 
150   char buf[20];
151   snprintf(buf, sizeof(buf), "%.2f %s", final_size, size_name[size_idx]);
152   return std::string(buf);
153 }
154 
TimeToHumanString(int unixtime)155 std::string TimeToHumanString(int unixtime) {
156   char time_buffer[80];
157   time_t rawtime = unixtime;
158   struct tm tInfo;
159   struct tm* timeinfo = localtime_r(&rawtime, &tInfo);
160   assert(timeinfo == &tInfo);
161   strftime(time_buffer, 80, "%c", timeinfo);
162   return std::string(time_buffer);
163 }
164 
EscapeString(const Slice & value)165 std::string EscapeString(const Slice& value) {
166   std::string r;
167   AppendEscapedStringTo(&r, value);
168   return r;
169 }
170 
ConsumeDecimalNumber(Slice * in,uint64_t * val)171 bool ConsumeDecimalNumber(Slice* in, uint64_t* val) {
172   uint64_t v = 0;
173   int digits = 0;
174   while (!in->empty()) {
175     char c = (*in)[0];
176     if (c >= '0' && c <= '9') {
177       ++digits;
178       const unsigned int delta = (c - '0');
179       static const uint64_t kMaxUint64 = ~static_cast<uint64_t>(0);
180       if (v > kMaxUint64 / 10 ||
181           (v == kMaxUint64 / 10 && delta > kMaxUint64 % 10)) {
182         // Overflow
183         return false;
184       }
185       v = (v * 10) + delta;
186       in->remove_prefix(1);
187     } else {
188       break;
189     }
190   }
191   *val = v;
192   return (digits > 0);
193 }
194 
isSpecialChar(const char c)195 bool isSpecialChar(const char c) {
196   if (c == '\\' || c == '#' || c == ':' || c == '\r' || c == '\n') {
197     return true;
198   }
199   return false;
200 }
201 
202 namespace {
203 using CharMap = std::pair<char, char>;
204 }
205 
UnescapeChar(const char c)206 char UnescapeChar(const char c) {
207   static const CharMap convert_map[] = {{'r', '\r'}, {'n', '\n'}};
208 
209   auto iter = std::find_if(std::begin(convert_map), std::end(convert_map),
210                            [c](const CharMap& p) { return p.first == c; });
211 
212   if (iter == std::end(convert_map)) {
213     return c;
214   }
215   return iter->second;
216 }
217 
EscapeChar(const char c)218 char EscapeChar(const char c) {
219   static const CharMap convert_map[] = {{'\n', 'n'}, {'\r', 'r'}};
220 
221   auto iter = std::find_if(std::begin(convert_map), std::end(convert_map),
222                            [c](const CharMap& p) { return p.first == c; });
223 
224   if (iter == std::end(convert_map)) {
225     return c;
226   }
227   return iter->second;
228 }
229 
EscapeOptionString(const std::string & raw_string)230 std::string EscapeOptionString(const std::string& raw_string) {
231   std::string output;
232   for (auto c : raw_string) {
233     if (isSpecialChar(c)) {
234       output += '\\';
235       output += EscapeChar(c);
236     } else {
237       output += c;
238     }
239   }
240 
241   return output;
242 }
243 
UnescapeOptionString(const std::string & escaped_string)244 std::string UnescapeOptionString(const std::string& escaped_string) {
245   bool escaped = false;
246   std::string output;
247 
248   for (auto c : escaped_string) {
249     if (escaped) {
250       output += UnescapeChar(c);
251       escaped = false;
252     } else {
253       if (c == '\\') {
254         escaped = true;
255         continue;
256       }
257       output += c;
258     }
259   }
260   return output;
261 }
262 
trim(const std::string & str)263 std::string trim(const std::string& str) {
264   if (str.empty()) return std::string();
265   size_t start = 0;
266   size_t end = str.size() - 1;
267   while (isspace(str[start]) != 0 && start < end) {
268     ++start;
269   }
270   while (isspace(str[end]) != 0 && start < end) {
271     --end;
272   }
273   if (start <= end) {
274     return str.substr(start, end - start + 1);
275   }
276   return std::string();
277 }
278 
EndsWith(const std::string & string,const std::string & pattern)279 bool EndsWith(const std::string& string, const std::string& pattern) {
280   size_t plen = pattern.size();
281   size_t slen = string.size();
282   if (plen <= slen) {
283     return string.compare(slen - plen, plen, pattern) == 0;
284   } else {
285     return false;
286   }
287 }
288 
StartsWith(const std::string & string,const std::string & pattern)289 bool StartsWith(const std::string& string, const std::string& pattern) {
290   return string.compare(0, pattern.size(), pattern) == 0;
291 }
292 
293 #ifndef ROCKSDB_LITE
294 
ParseBoolean(const std::string & type,const std::string & value)295 bool ParseBoolean(const std::string& type, const std::string& value) {
296   if (value == "true" || value == "1") {
297     return true;
298   } else if (value == "false" || value == "0") {
299     return false;
300   }
301   throw std::invalid_argument(type);
302 }
303 
ParseUint8(const std::string & value)304 uint8_t ParseUint8(const std::string& value) {
305   uint64_t num = ParseUint64(value);
306   if ((num >> 8LL) == 0) {
307     return static_cast<uint8_t>(num);
308   } else {
309     throw std::out_of_range(value);
310   }
311 }
312 
ParseUint32(const std::string & value)313 uint32_t ParseUint32(const std::string& value) {
314   uint64_t num = ParseUint64(value);
315   if ((num >> 32LL) == 0) {
316     return static_cast<uint32_t>(num);
317   } else {
318     throw std::out_of_range(value);
319   }
320 }
321 
ParseInt32(const std::string & value)322 int32_t ParseInt32(const std::string& value) {
323   int64_t num = ParseInt64(value);
324   if (num <= port::kMaxInt32 && num >= port::kMinInt32) {
325     return static_cast<int32_t>(num);
326   } else {
327     throw std::out_of_range(value);
328   }
329 }
330 
331 #endif
332 
ParseUint64(const std::string & value)333 uint64_t ParseUint64(const std::string& value) {
334   size_t endchar;
335 #ifndef CYGWIN
336   uint64_t num = std::stoull(value.c_str(), &endchar);
337 #else
338   char* endptr;
339   uint64_t num = std::strtoul(value.c_str(), &endptr, 0);
340   endchar = endptr - value.c_str();
341 #endif
342 
343   if (endchar < value.length()) {
344     char c = value[endchar];
345     if (c == 'k' || c == 'K')
346       num <<= 10LL;
347     else if (c == 'm' || c == 'M')
348       num <<= 20LL;
349     else if (c == 'g' || c == 'G')
350       num <<= 30LL;
351     else if (c == 't' || c == 'T')
352       num <<= 40LL;
353   }
354 
355   return num;
356 }
357 
ParseInt64(const std::string & value)358 int64_t ParseInt64(const std::string& value) {
359   size_t endchar;
360 #ifndef CYGWIN
361   int64_t num = std::stoll(value.c_str(), &endchar);
362 #else
363   char* endptr;
364   int64_t num = std::strtoll(value.c_str(), &endptr, 0);
365   endchar = endptr - value.c_str();
366 #endif
367 
368   if (endchar < value.length()) {
369     char c = value[endchar];
370     if (c == 'k' || c == 'K')
371       num <<= 10LL;
372     else if (c == 'm' || c == 'M')
373       num <<= 20LL;
374     else if (c == 'g' || c == 'G')
375       num <<= 30LL;
376     else if (c == 't' || c == 'T')
377       num <<= 40LL;
378   }
379 
380   return num;
381 }
382 
ParseInt(const std::string & value)383 int ParseInt(const std::string& value) {
384   size_t endchar;
385 #ifndef CYGWIN
386   int num = std::stoi(value.c_str(), &endchar);
387 #else
388   char* endptr;
389   int num = std::strtoul(value.c_str(), &endptr, 0);
390   endchar = endptr - value.c_str();
391 #endif
392 
393   if (endchar < value.length()) {
394     char c = value[endchar];
395     if (c == 'k' || c == 'K')
396       num <<= 10;
397     else if (c == 'm' || c == 'M')
398       num <<= 20;
399     else if (c == 'g' || c == 'G')
400       num <<= 30;
401   }
402 
403   return num;
404 }
405 
ParseDouble(const std::string & value)406 double ParseDouble(const std::string& value) {
407 #ifndef CYGWIN
408   return std::stod(value);
409 #else
410   return std::strtod(value.c_str(), 0);
411 #endif
412 }
413 
ParseSizeT(const std::string & value)414 size_t ParseSizeT(const std::string& value) {
415   return static_cast<size_t>(ParseUint64(value));
416 }
417 
ParseVectorInt(const std::string & value)418 std::vector<int> ParseVectorInt(const std::string& value) {
419   std::vector<int> result;
420   size_t start = 0;
421   while (start < value.size()) {
422     size_t end = value.find(':', start);
423     if (end == std::string::npos) {
424       result.push_back(ParseInt(value.substr(start)));
425       break;
426     } else {
427       result.push_back(ParseInt(value.substr(start, end - start)));
428       start = end + 1;
429     }
430   }
431   return result;
432 }
433 
SerializeIntVector(const std::vector<int> & vec,std::string * value)434 bool SerializeIntVector(const std::vector<int>& vec, std::string* value) {
435   *value = "";
436   for (size_t i = 0; i < vec.size(); ++i) {
437     if (i > 0) {
438       *value += ":";
439     }
440     *value += ToString(vec[i]);
441   }
442   return true;
443 }
444 
445 // Copied from folly/string.cpp:
446 // https://github.com/facebook/folly/blob/0deef031cb8aab76dc7e736f8b7c22d701d5f36b/folly/String.cpp#L457
447 // There are two variants of `strerror_r` function, one returns
448 // `int`, and another returns `char*`. Selecting proper version using
449 // preprocessor macros portably is extremely hard.
450 //
451 // For example, on Android function signature depends on `__USE_GNU` and
452 // `__ANDROID_API__` macros (https://git.io/fjBBE).
453 //
454 // So we are using C++ overloading trick: we pass a pointer of
455 // `strerror_r` to `invoke_strerror_r` function, and C++ compiler
456 // selects proper function.
457 
458 #if !(defined(_WIN32) && (defined(__MINGW32__) || defined(_MSC_VER)))
459 ROCKSDB_MAYBE_UNUSED
invoke_strerror_r(int (* strerror_r)(int,char *,size_t),int err,char * buf,size_t buflen)460 static std::string invoke_strerror_r(int (*strerror_r)(int, char*, size_t),
461                                      int err, char* buf, size_t buflen) {
462   // Using XSI-compatible strerror_r
463   int r = strerror_r(err, buf, buflen);
464 
465   // OSX/FreeBSD use EINVAL and Linux uses -1 so just check for non-zero
466   if (r != 0) {
467     snprintf(buf, buflen, "Unknown error %d (strerror_r failed with error %d)",
468              err, errno);
469   }
470   return buf;
471 }
472 
473 ROCKSDB_MAYBE_UNUSED
invoke_strerror_r(char * (* strerror_r)(int,char *,size_t),int err,char * buf,size_t buflen)474 static std::string invoke_strerror_r(char* (*strerror_r)(int, char*, size_t),
475                                      int err, char* buf, size_t buflen) {
476   // Using GNU strerror_r
477   return strerror_r(err, buf, buflen);
478 }
479 #endif  // !(defined(_WIN32) && (defined(__MINGW32__) || defined(_MSC_VER)))
480 
errnoStr(int err)481 std::string errnoStr(int err) {
482   char buf[1024];
483   buf[0] = '\0';
484 
485   std::string result;
486 
487   // https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/strerror_r.3.html
488   // http://www.kernel.org/doc/man-pages/online/pages/man3/strerror.3.html
489 #if defined(_WIN32) && (defined(__MINGW32__) || defined(_MSC_VER))
490   // mingw64 has no strerror_r, but Windows has strerror_s, which C11 added
491   // as well. So maybe we should use this across all platforms (together
492   // with strerrorlen_s). Note strerror_r and _s have swapped args.
493   int r = strerror_s(buf, sizeof(buf), err);
494   if (r != 0) {
495     snprintf(buf, sizeof(buf),
496              "Unknown error %d (strerror_r failed with error %d)", err, errno);
497   }
498   result.assign(buf);
499 #else
500   // Using any strerror_r
501   result.assign(invoke_strerror_r(strerror_r, err, buf, sizeof(buf)));
502 #endif
503 
504   return result;
505 }
506 
507 }  // namespace ROCKSDB_NAMESPACE
508