]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/string_util.h
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).
11 #include <unordered_map>
18 extern std::vector
<std::string
> StringSplit(const std::string
& arg
, char delim
);
21 inline std::string
ToString(T value
) {
22 #if !(defined OS_ANDROID) && !(defined CYGWIN) && !(defined OS_FREEBSD)
23 return std::to_string(value
);
25 // Andorid or cygwin doesn't support all of C++11, std::to_string() being
26 // one of the not supported features.
27 std::ostringstream os
;
33 // Append a human-readable printout of "num" to *str
34 extern void AppendNumberTo(std::string
* str
, uint64_t num
);
36 // Append a human-readable printout of "value" to *str.
37 // Escapes any non-printable characters found in "value".
38 extern void AppendEscapedStringTo(std::string
* str
, const Slice
& value
);
40 // Return a string printout of "num"
41 extern std::string
NumberToString(uint64_t num
);
43 // Return a human-readable version of num.
44 // for num >= 10.000, prints "xxK"
45 // for num >= 10.000.000, prints "xxM"
46 // for num >= 10.000.000.000, prints "xxG"
47 extern std::string
NumberToHumanString(int64_t num
);
49 // Return a human-readable version of bytes
50 // ex: 1048576 -> 1.00 GB
51 extern std::string
BytesToHumanString(uint64_t bytes
);
53 // Append a human-readable time in micros.
54 int AppendHumanMicros(uint64_t micros
, char* output
, int len
,
57 // Append a human-readable size in bytes
58 int AppendHumanBytes(uint64_t bytes
, char* output
, int len
);
60 // Return a human-readable version of "value".
61 // Escapes any non-printable characters found in "value".
62 extern std::string
EscapeString(const Slice
& value
);
64 // Parse a human-readable number from "*in" into *value. On success,
65 // advances "*in" past the consumed number and sets "*val" to the
66 // numeric value. Otherwise, returns false and leaves *in in an
68 extern bool ConsumeDecimalNumber(Slice
* in
, uint64_t* val
);
70 // Returns true if the input char "c" is considered as a special character
71 // that will be escaped when EscapeOptionString() is called.
73 // @param c the input char
74 // @return true if the input char "c" is considered as a special character.
75 // @see EscapeOptionString
76 bool isSpecialChar(const char c
);
78 // If the input char is an escaped char, it will return the its
79 // associated raw-char. Otherwise, the function will simply return
80 // the original input char.
81 char UnescapeChar(const char c
);
83 // If the input char is a control char, it will return the its
84 // associated escaped char. Otherwise, the function will simply return
85 // the original input char.
86 char EscapeChar(const char c
);
88 // Converts a raw string to an escaped string. Escaped-characters are
89 // defined via the isSpecialChar() function. When a char in the input
90 // string "raw_string" is classified as a special characters, then it
91 // will be prefixed by '\' in the output.
93 // It's inverse function is UnescapeOptionString().
94 // @param raw_string the input string
95 // @return the '\' escaped string of the input "raw_string"
96 // @see isSpecialChar, UnescapeOptionString
97 std::string
EscapeOptionString(const std::string
& raw_string
);
99 // The inverse function of EscapeOptionString. It converts
100 // an '\' escaped string back to a raw string.
102 // @param escaped_string the input '\' escaped string
103 // @return the raw string of the input "escaped_string"
104 std::string
UnescapeOptionString(const std::string
& escaped_string
);
106 std::string
trim(const std::string
& str
);
109 bool ParseBoolean(const std::string
& type
, const std::string
& value
);
111 uint32_t ParseUint32(const std::string
& value
);
113 int32_t ParseInt32(const std::string
& value
);
116 uint64_t ParseUint64(const std::string
& value
);
118 int ParseInt(const std::string
& value
);
121 int64_t ParseInt64(const std::string
& value
);
123 double ParseDouble(const std::string
& value
);
125 size_t ParseSizeT(const std::string
& value
);
127 std::vector
<int> ParseVectorInt(const std::string
& value
);
129 bool SerializeIntVector(const std::vector
<int>& vec
, std::string
* value
);
131 extern const std::string kNullptrString
;
133 } // namespace rocksdb