string_util.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #pragma once
  7. #include <cstdint>
  8. #include <sstream>
  9. #include <string>
  10. #include <unordered_map>
  11. #include <vector>
  12. #include "rocksdb/rocksdb_namespace.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. class Slice;
  15. std::vector<std::string> StringSplit(const std::string& arg, char delim);
  16. // Append a human-readable printout of "num" to *str
  17. void AppendNumberTo(std::string* str, uint64_t num);
  18. // Append a human-readable printout of "value" to *str.
  19. // Escapes any non-printable characters found in "value".
  20. void AppendEscapedStringTo(std::string* str, const Slice& value);
  21. // Put n digits from v in base kBase to (*buf)[0] to (*buf)[n-1] and
  22. // advance *buf to the position after what was written.
  23. template <size_t kBase>
  24. inline void PutBaseChars(char** buf, size_t n, uint64_t v, bool uppercase) {
  25. const char* digitChars = uppercase ? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  26. : "0123456789abcdefghijklmnopqrstuvwxyz";
  27. for (size_t i = n; i > 0; --i) {
  28. (*buf)[i - 1] = digitChars[static_cast<size_t>(v % kBase)];
  29. v /= kBase;
  30. }
  31. *buf += n;
  32. }
  33. // Construct a string of n digits from v in base kBase
  34. template <size_t kBase>
  35. inline std::string ToBaseCharsString(size_t n, uint64_t v, bool uppercase) {
  36. std::string result;
  37. result.resize(n);
  38. char* buf = &result[0];
  39. PutBaseChars<kBase>(&buf, n, v, uppercase);
  40. return result;
  41. }
  42. // Parse n digits from *buf in base kBase to *v and advance *buf to the
  43. // position after what was read. On success, true is returned. On failure,
  44. // false is returned, *buf is placed at the first bad character, and *v
  45. // contains the partial parsed data. Overflow is not checked but the
  46. // result is accurate mod 2^64. Requires the starting value of *v to be
  47. // zero or previously accumulated parsed digits, i.e.
  48. // ParseBaseChars(&b, n, &v);
  49. // is equivalent to n calls to
  50. // ParseBaseChars(&b, 1, &v);
  51. template <int kBase>
  52. inline bool ParseBaseChars(const char** buf, size_t n, uint64_t* v) {
  53. while (n) {
  54. char c = **buf;
  55. *v *= static_cast<uint64_t>(kBase);
  56. if (c >= '0' && (kBase >= 10 ? c <= '9' : c < '0' + kBase)) {
  57. *v += static_cast<uint64_t>(c - '0');
  58. } else if (kBase > 10 && c >= 'A' && c < 'A' + kBase - 10) {
  59. *v += static_cast<uint64_t>(c - 'A' + 10);
  60. } else if (kBase > 10 && c >= 'a' && c < 'a' + kBase - 10) {
  61. *v += static_cast<uint64_t>(c - 'a' + 10);
  62. } else {
  63. return false;
  64. }
  65. --n;
  66. ++*buf;
  67. }
  68. return true;
  69. }
  70. // Return a human-readable version of num.
  71. // for num >= 10.000, prints "xxK"
  72. // for num >= 10.000.000, prints "xxM"
  73. // for num >= 10.000.000.000, prints "xxG"
  74. std::string NumberToHumanString(int64_t num);
  75. // Return a human-readable version of bytes
  76. // ex: 1048576 -> 1.00 GB
  77. std::string BytesToHumanString(uint64_t bytes);
  78. // Return a human-readable version of unix time
  79. // ex: 1562116015 -> "Tue Jul 2 18:06:55 2019"
  80. std::string TimeToHumanString(int unixtime);
  81. // Append a human-readable time in micros.
  82. int AppendHumanMicros(uint64_t micros, char* output, int len,
  83. bool fixed_format);
  84. // Append a human-readable size in bytes
  85. int AppendHumanBytes(uint64_t bytes, char* output, int len);
  86. // Return a human-readable version of "value".
  87. // Escapes any non-printable characters found in "value".
  88. std::string EscapeString(const Slice& value);
  89. // Parse a human-readable number from "*in" into *value. On success,
  90. // advances "*in" past the consumed number and sets "*val" to the
  91. // numeric value. Otherwise, returns false and leaves *in in an
  92. // unspecified state.
  93. bool ConsumeDecimalNumber(Slice* in, uint64_t* val);
  94. // Returns true if the input char "c" is considered as a special character
  95. // that will be escaped when EscapeOptionString() is called.
  96. //
  97. // @param c the input char
  98. // @return true if the input char "c" is considered as a special character.
  99. // @see EscapeOptionString
  100. bool isSpecialChar(const char c);
  101. // If the input char is an escaped char, it will return the its
  102. // associated raw-char. Otherwise, the function will simply return
  103. // the original input char.
  104. char UnescapeChar(const char c);
  105. // If the input char is a control char, it will return the its
  106. // associated escaped char. Otherwise, the function will simply return
  107. // the original input char.
  108. char EscapeChar(const char c);
  109. // Converts a raw string to an escaped string. Escaped-characters are
  110. // defined via the isSpecialChar() function. When a char in the input
  111. // string "raw_string" is classified as a special characters, then it
  112. // will be prefixed by '\' in the output.
  113. //
  114. // It's inverse function is UnescapeOptionString().
  115. // @param raw_string the input string
  116. // @return the '\' escaped string of the input "raw_string"
  117. // @see isSpecialChar, UnescapeOptionString
  118. std::string EscapeOptionString(const std::string& raw_string);
  119. // The inverse function of EscapeOptionString. It converts
  120. // an '\' escaped string back to a raw string.
  121. //
  122. // @param escaped_string the input '\' escaped string
  123. // @return the raw string of the input "escaped_string"
  124. std::string UnescapeOptionString(const std::string& escaped_string);
  125. std::string trim(const std::string& str);
  126. // Returns true if "string" ends with "pattern"
  127. bool EndsWith(const std::string& string, const std::string& pattern);
  128. // Returns true if "string" starts with "pattern"
  129. bool StartsWith(const std::string& string, const std::string& pattern);
  130. bool ParseBoolean(const std::string& type, const std::string& value);
  131. uint8_t ParseUint8(const std::string& value);
  132. uint32_t ParseUint32(const std::string& value);
  133. int32_t ParseInt32(const std::string& value);
  134. uint64_t ParseUint64(const std::string& value);
  135. int ParseInt(const std::string& value);
  136. int64_t ParseInt64(const std::string& value);
  137. double ParseDouble(const std::string& value);
  138. size_t ParseSizeT(const std::string& value);
  139. std::vector<int> ParseVectorInt(const std::string& value);
  140. bool SerializeIntVector(const std::vector<int>& vec, std::string* value);
  141. // Expects HH:mm format for the input value
  142. // Returns -1 if invalid input. Otherwise returns seconds since midnight
  143. int ParseTimeStringToSeconds(const std::string& value);
  144. // Expects HH:mm-HH:mm format for the input value
  145. // Returns false, if invalid format.
  146. // Otherwise, returns true and start_time and end_time are set
  147. bool TryParseTimeRangeString(const std::string& value, int& start_time,
  148. int& end_time);
  149. extern const std::string kNullptrString;
  150. // errnoStr() function returns a string that describes the error code passed in
  151. // the argument err
  152. std::string errnoStr(int err);
  153. } // namespace ROCKSDB_NAMESPACE