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