Coverage Report

Created: 2024-07-27 06:53

/src/rocksdb/util/string_util.h
Line
Count
Source (jump to first uncovered line)
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
27.9k
inline void PutBaseChars(char** buf, size_t n, uint64_t v, bool uppercase) {
34
27.9k
  const char* digitChars = uppercase ? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
35
27.9k
                                     : "0123456789abcdefghijklmnopqrstuvwxyz";
36
307k
  for (size_t i = n; i > 0; --i) {
37
279k
    (*buf)[i - 1] = digitChars[static_cast<size_t>(v % kBase)];
38
279k
    v /= kBase;
39
279k
  }
40
27.9k
  *buf += n;
41
27.9k
}
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
27.9k
inline void PutBaseChars(char** buf, size_t n, uint64_t v, bool uppercase) {
34
27.9k
  const char* digitChars = uppercase ? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
35
27.9k
                                     : "0123456789abcdefghijklmnopqrstuvwxyz";
36
307k
  for (size_t i = n; i > 0; --i) {
37
279k
    (*buf)[i - 1] = digitChars[static_cast<size_t>(v % kBase)];
38
279k
    v /= kBase;
39
279k
  }
40
27.9k
  *buf += n;
41
27.9k
}
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
129k
inline bool ParseBaseChars(const char** buf, size_t n, uint64_t* v) {
54
1.42M
  while (n) {
55
1.29M
    char c = **buf;
56
1.29M
    *v *= static_cast<uint64_t>(kBase);
57
1.29M
    if (c >= '0' && (kBase >= 10 ? c <= '9' : c < '0' + kBase)) {
58
240k
      *v += static_cast<uint64_t>(c - '0');
59
1.05M
    } else if (kBase > 10 && c >= 'A' && c < 'A' + kBase - 10) {
60
1.05M
      *v += static_cast<uint64_t>(c - 'A' + 10);
61
18.4E
    } else if (kBase > 10 && c >= 'a' && c < 'a' + kBase - 10) {
62
0
      *v += static_cast<uint64_t>(c - 'a' + 10);
63
18.4E
    } else {
64
18.4E
      return false;
65
18.4E
    }
66
1.29M
    --n;
67
1.29M
    ++*buf;
68
1.29M
  }
69
130k
  return true;
70
129k
}
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
std::string NumberToHumanString(int64_t num);
77
78
// Return a human-readable version of bytes
79
// ex: 1048576 -> 1.00 GB
80
std::string BytesToHumanString(uint64_t bytes);
81
82
// Return a human-readable version of unix time
83
// ex: 1562116015 -> "Tue Jul  2 18:06:55 2019"
84
std::string TimeToHumanString(int unixtime);
85
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
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
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
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
147
bool ParseBoolean(const std::string& type, const std::string& value);
148
149
uint8_t ParseUint8(const std::string& value);
150
151
uint32_t ParseUint32(const std::string& value);
152
153
int32_t ParseInt32(const std::string& value);
154
155
uint64_t ParseUint64(const std::string& value);
156
157
int ParseInt(const std::string& value);
158
159
int64_t ParseInt64(const std::string& value);
160
161
double ParseDouble(const std::string& value);
162
163
size_t ParseSizeT(const std::string& value);
164
165
std::vector<int> ParseVectorInt(const std::string& value);
166
167
bool SerializeIntVector(const std::vector<int>& vec, std::string* value);
168
169
// Expects HH:mm format for the input value
170
// Returns -1 if invalid input. Otherwise returns seconds since midnight
171
int ParseTimeStringToSeconds(const std::string& value);
172
173
// Expects HH:mm-HH:mm format for the input value
174
// Returns false, if invalid format.
175
// Otherwise, returns true and start_time and end_time are set
176
bool TryParseTimeRangeString(const std::string& value, int& start_time,
177
                             int& end_time);
178
179
extern const std::string kNullptrString;
180
181
// errnoStr() function returns a string that describes the error code passed in
182
// the argument err
183
std::string errnoStr(int err);
184
185
}  // namespace ROCKSDB_NAMESPACE