]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/util/string_util.h
bump version to 18.2.4-pve3
[ceph.git] / ceph / src / rocksdb / util / string_util.h
CommitLineData
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
17namespace ROCKSDB_NAMESPACE {
7c673cae
FG
18
19class Slice;
20
21extern std::vector<std::string> StringSplit(const std::string& arg, char delim);
22
7c673cae
FG
23// Append a human-readable printout of "num" to *str
24extern 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".
28extern 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.
32template <size_t kBase>
33inline 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);
52template <int kBase>
53inline 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"
76extern std::string NumberToHumanString(int64_t num);
77
78// Return a human-readable version of bytes
79// ex: 1048576 -> 1.00 GB
80extern 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"
84extern std::string TimeToHumanString(int unixtime);
85
7c673cae
FG
86// Append a human-readable time in micros.
87int AppendHumanMicros(uint64_t micros, char* output, int len,
88 bool fixed_format);
89
90// Append a human-readable size in bytes
91int 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".
95extern 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.
101extern 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
109bool 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.
114char 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.
119char 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
130std::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"
137std::string UnescapeOptionString(const std::string& escaped_string);
138
139std::string trim(const std::string& str);
140
20effc67
TL
141// Returns true if "string" ends with "pattern"
142bool EndsWith(const std::string& string, const std::string& pattern);
143
144// Returns true if "string" starts with "pattern"
145bool StartsWith(const std::string& string, const std::string& pattern);
146
7c673cae
FG
147#ifndef ROCKSDB_LITE
148bool ParseBoolean(const std::string& type, const std::string& value);
149
1e59de90
TL
150uint8_t ParseUint8(const std::string& value);
151
7c673cae 152uint32_t ParseUint32(const std::string& value);
494da23a
TL
153
154int32_t ParseInt32(const std::string& value);
7c673cae
FG
155#endif
156
157uint64_t ParseUint64(const std::string& value);
158
159int ParseInt(const std::string& value);
160
494da23a
TL
161int64_t ParseInt64(const std::string& value);
162
7c673cae
FG
163double ParseDouble(const std::string& value);
164
165size_t ParseSizeT(const std::string& value);
166
167std::vector<int> ParseVectorInt(const std::string& value);
168
169bool SerializeIntVector(const std::vector<int>& vec, std::string* value);
170
171extern const std::string kNullptrString;
172
1e59de90
TL
173// errnoStr() function returns a string that describes the error code passed in
174// the argument err
175extern std::string errnoStr(int err);
176
f67539c2 177} // namespace ROCKSDB_NAMESPACE