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