string_util.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <sstream>
  8. #include <string>
  9. #include <unordered_map>
  10. #include <vector>
  11. #include "rocksdb/rocksdb_namespace.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. class Slice;
  14. extern std::vector<std::string> StringSplit(const std::string& arg, char delim);
  15. template <typename T>
  16. inline std::string ToString(T value) {
  17. #if !(defined OS_ANDROID) && !(defined CYGWIN) && !(defined OS_FREEBSD)
  18. return std::to_string(value);
  19. #else
  20. // Andorid or cygwin doesn't support all of C++11, std::to_string() being
  21. // one of the not supported features.
  22. std::ostringstream os;
  23. os << value;
  24. return os.str();
  25. #endif
  26. }
  27. // Append a human-readable printout of "num" to *str
  28. extern void AppendNumberTo(std::string* str, uint64_t num);
  29. // Append a human-readable printout of "value" to *str.
  30. // Escapes any non-printable characters found in "value".
  31. extern void AppendEscapedStringTo(std::string* str, const Slice& value);
  32. // Return a string printout of "num"
  33. extern std::string NumberToString(uint64_t num);
  34. // Return a human-readable version of num.
  35. // for num >= 10.000, prints "xxK"
  36. // for num >= 10.000.000, prints "xxM"
  37. // for num >= 10.000.000.000, prints "xxG"
  38. extern std::string NumberToHumanString(int64_t num);
  39. // Return a human-readable version of bytes
  40. // ex: 1048576 -> 1.00 GB
  41. extern std::string BytesToHumanString(uint64_t bytes);
  42. // Return a human-readable version of unix time
  43. // ex: 1562116015 -> "Tue Jul 2 18:06:55 2019"
  44. extern std::string TimeToHumanString(int unixtime);
  45. // Append a human-readable time in micros.
  46. int AppendHumanMicros(uint64_t micros, char* output, int len,
  47. bool fixed_format);
  48. // Append a human-readable size in bytes
  49. int AppendHumanBytes(uint64_t bytes, char* output, int len);
  50. // Return a human-readable version of "value".
  51. // Escapes any non-printable characters found in "value".
  52. extern std::string EscapeString(const Slice& value);
  53. // Parse a human-readable number from "*in" into *value. On success,
  54. // advances "*in" past the consumed number and sets "*val" to the
  55. // numeric value. Otherwise, returns false and leaves *in in an
  56. // unspecified state.
  57. extern bool ConsumeDecimalNumber(Slice* in, uint64_t* val);
  58. // Returns true if the input char "c" is considered as a special character
  59. // that will be escaped when EscapeOptionString() is called.
  60. //
  61. // @param c the input char
  62. // @return true if the input char "c" is considered as a special character.
  63. // @see EscapeOptionString
  64. bool isSpecialChar(const char c);
  65. // If the input char is an escaped char, it will return the its
  66. // associated raw-char. Otherwise, the function will simply return
  67. // the original input char.
  68. char UnescapeChar(const char c);
  69. // If the input char is a control char, it will return the its
  70. // associated escaped char. Otherwise, the function will simply return
  71. // the original input char.
  72. char EscapeChar(const char c);
  73. // Converts a raw string to an escaped string. Escaped-characters are
  74. // defined via the isSpecialChar() function. When a char in the input
  75. // string "raw_string" is classified as a special characters, then it
  76. // will be prefixed by '\' in the output.
  77. //
  78. // It's inverse function is UnescapeOptionString().
  79. // @param raw_string the input string
  80. // @return the '\' escaped string of the input "raw_string"
  81. // @see isSpecialChar, UnescapeOptionString
  82. std::string EscapeOptionString(const std::string& raw_string);
  83. // The inverse function of EscapeOptionString. It converts
  84. // an '\' escaped string back to a raw string.
  85. //
  86. // @param escaped_string the input '\' escaped string
  87. // @return the raw string of the input "escaped_string"
  88. std::string UnescapeOptionString(const std::string& escaped_string);
  89. std::string trim(const std::string& str);
  90. #ifndef ROCKSDB_LITE
  91. bool ParseBoolean(const std::string& type, const std::string& value);
  92. uint32_t ParseUint32(const std::string& value);
  93. int32_t ParseInt32(const std::string& value);
  94. #endif
  95. uint64_t ParseUint64(const std::string& value);
  96. int ParseInt(const std::string& value);
  97. int64_t ParseInt64(const std::string& value);
  98. double ParseDouble(const std::string& value);
  99. size_t ParseSizeT(const std::string& value);
  100. std::vector<int> ParseVectorInt(const std::string& value);
  101. bool SerializeIntVector(const std::vector<int>& vec, std::string* value);
  102. extern const std::string kNullptrString;
  103. } // namespace ROCKSDB_NAMESPACE