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