]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/util/string_util.h
import 14.2.4 nautilus point release
[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
9#include <sstream>
10#include <string>
11#include <unordered_map>
12#include <vector>
13
14namespace rocksdb {
15
16class Slice;
17
18extern std::vector<std::string> StringSplit(const std::string& arg, char delim);
19
20template <typename T>
21inline std::string ToString(T value) {
22#if !(defined OS_ANDROID) && !(defined CYGWIN) && !(defined OS_FREEBSD)
23 return std::to_string(value);
24#else
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;
28 os << value;
29 return os.str();
30#endif
31}
32
33// Append a human-readable printout of "num" to *str
34extern void AppendNumberTo(std::string* str, uint64_t num);
35
36// Append a human-readable printout of "value" to *str.
37// Escapes any non-printable characters found in "value".
38extern void AppendEscapedStringTo(std::string* str, const Slice& value);
39
40// Return a string printout of "num"
41extern std::string NumberToString(uint64_t num);
42
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"
47extern std::string NumberToHumanString(int64_t num);
48
49// Return a human-readable version of bytes
50// ex: 1048576 -> 1.00 GB
51extern std::string BytesToHumanString(uint64_t bytes);
52
53// Append a human-readable time in micros.
54int AppendHumanMicros(uint64_t micros, char* output, int len,
55 bool fixed_format);
56
57// Append a human-readable size in bytes
58int AppendHumanBytes(uint64_t bytes, char* output, int len);
59
60// Return a human-readable version of "value".
61// Escapes any non-printable characters found in "value".
62extern std::string EscapeString(const Slice& value);
63
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
67// unspecified state.
68extern bool ConsumeDecimalNumber(Slice* in, uint64_t* val);
69
70// Returns true if the input char "c" is considered as a special character
71// that will be escaped when EscapeOptionString() is called.
72//
73// @param c the input char
74// @return true if the input char "c" is considered as a special character.
75// @see EscapeOptionString
76bool isSpecialChar(const char c);
77
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.
81char UnescapeChar(const char c);
82
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.
86char EscapeChar(const char c);
87
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.
92//
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
97std::string EscapeOptionString(const std::string& raw_string);
98
99// The inverse function of EscapeOptionString. It converts
100// an '\' escaped string back to a raw string.
101//
102// @param escaped_string the input '\' escaped string
103// @return the raw string of the input "escaped_string"
104std::string UnescapeOptionString(const std::string& escaped_string);
105
106std::string trim(const std::string& str);
107
108#ifndef ROCKSDB_LITE
109bool ParseBoolean(const std::string& type, const std::string& value);
110
111uint32_t ParseUint32(const std::string& value);
494da23a
TL
112
113int32_t ParseInt32(const std::string& value);
7c673cae
FG
114#endif
115
116uint64_t ParseUint64(const std::string& value);
117
118int ParseInt(const std::string& value);
119
494da23a
TL
120
121int64_t ParseInt64(const std::string& value);
122
7c673cae
FG
123double ParseDouble(const std::string& value);
124
125size_t ParseSizeT(const std::string& value);
126
127std::vector<int> ParseVectorInt(const std::string& value);
128
129bool SerializeIntVector(const std::vector<int>& vec, std::string* value);
130
131extern const std::string kNullptrString;
132
133} // namespace rocksdb