/Users/deen/code/yugabyte-db/src/yb/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 the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | |
20 | | #ifndef YB_UTIL_STRING_UTIL_H |
21 | | #define YB_UTIL_STRING_UTIL_H |
22 | | |
23 | | #pragma once |
24 | | |
25 | | #include <sstream> |
26 | | #include <string> |
27 | | #include <vector> |
28 | | |
29 | | #include <boost/range/iterator_range.hpp> |
30 | | |
31 | | #include "yb/util/slice.h" |
32 | | #include "yb/util/tostring.h" |
33 | | |
34 | | namespace yb { |
35 | | namespace details { |
36 | | |
37 | | template<class Container> |
38 | | struct Unpacker { |
39 | | using container_type = Container; |
40 | | Container container; |
41 | | }; |
42 | | |
43 | | template<class T, class... Args> |
44 | | size_t ItemCount(const T&, const Args&...); |
45 | | |
46 | | template<class T, class... Args> |
47 | | void AppendItem(vector<string>* dest, const T& t, const Args&... args); |
48 | | |
49 | | inline size_t ItemCount() { return 0; } |
50 | | inline void AppendItem(vector<string>* dest) {} |
51 | | |
52 | | template<class T> |
53 | | struct ToStringVectorHelper { |
54 | | template<class... Args> |
55 | | static size_t Count(const T& t, const Args&... args) { |
56 | | return 1 + ItemCount(args...); |
57 | | } |
58 | | |
59 | | template<class... Args> |
60 | | static void Append(vector<string>* dest, const T& t, const Args&... args) { |
61 | | dest->push_back(ToString(t)); |
62 | | AppendItem(dest, args...); |
63 | | } |
64 | | }; |
65 | | |
66 | | template<class T> |
67 | | struct ToStringVectorHelper<Unpacker<T> > { |
68 | | template<class... Args> |
69 | | static size_t Count(const Unpacker<T>& unpacker, const Args&... args) { |
70 | | return std::distance(std::begin(unpacker.container), std::end(unpacker.container)) + |
71 | | ItemCount(args...); |
72 | | } |
73 | | |
74 | | template<class... Args> |
75 | | static void Append(vector<string>* dest, const Unpacker<T>& unpacker, const Args&... args) { |
76 | | for(auto&& i : unpacker.container) { |
77 | | dest->push_back(ToString(i)); |
78 | | } |
79 | | AppendItem(dest, args...); |
80 | | } |
81 | | }; |
82 | | |
83 | | template<class T, class... Args> |
84 | | size_t ItemCount(const T& t, const Args&...args) { |
85 | | return ToStringVectorHelper<T>::Count(t, args...); |
86 | | } |
87 | | |
88 | | template<class T, class... Args> |
89 | | void AppendItem(vector<string>* dest, const T& t, const Args&... args) { |
90 | | return ToStringVectorHelper<T>::Append(dest, t, args...); |
91 | | } |
92 | | |
93 | | } // namespace details |
94 | | |
95 | | // Whether the string contains (arbitrary long) integer value |
96 | | bool IsBigInteger(const Slice& s); |
97 | | |
98 | | // Whether the string contains (arbitrary long) decimal or integer value |
99 | | bool IsDecimal(const Slice& s); |
100 | | |
101 | | // Whether the string is "true"/"false" (case-insensitive) |
102 | | bool IsBoolean(const Slice& s); |
103 | | |
104 | | using StringVector = std::vector<std::string>; |
105 | | StringVector StringSplit(const std::string& arg, char delim); |
106 | | |
107 | | template<typename Iterator> |
108 | 40.2k | inline std::string RangeToString(Iterator begin, Iterator end) { |
109 | 40.2k | return ToString(boost::make_iterator_range(begin, end)); |
110 | 40.2k | } |
111 | | |
112 | | template <typename T> |
113 | 48 | inline std::string VectorToString(const std::vector<T>& vec) { |
114 | 48 | return ToString(vec); |
115 | 48 | } Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::VectorToString<yb::Status>(std::__1::vector<yb::Status, std::__1::allocator<yb::Status> > const&) std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::VectorToString<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) Line | Count | Source | 113 | 48 | inline std::string VectorToString(const std::vector<T>& vec) { | 114 | 48 | return ToString(vec); | 115 | 48 | } |
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::VectorToString<yb::docdb::PrimitiveValue>(std::__1::vector<yb::docdb::PrimitiveValue, std::__1::allocator<yb::docdb::PrimitiveValue> > const&) |
116 | | |
117 | | // Whether or not content of two strings is equal ignoring case |
118 | | // Examples: |
119 | | // - abcd == ABCD |
120 | | // - AbCd == aBCD |
121 | | bool EqualsIgnoreCase(const std::string &string1, |
122 | | const std::string &string2); |
123 | | |
124 | | template <class T> |
125 | 114 | std::string RightPadToWidth(const T& val, int width) { |
126 | 114 | std::stringstream ss; |
127 | 114 | ss << val; |
128 | 114 | string ss_str = ss.str(); |
129 | 114 | int64_t padding = width - ss_str.size(); |
130 | 114 | if (padding <= 0) { |
131 | 4 | return ss_str; |
132 | 4 | } |
133 | 110 | return ss_str + string(padding, ' '); |
134 | 114 | } Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::RightPadToWidth<char [16]>(char const (&) [16], int) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::RightPadToWidth<char [7]>(char const (&) [7], int) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::RightPadToWidth<char [8]>(char const (&) [8], int) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::RightPadToWidth<char [9]>(char const (&) [9], int) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::RightPadToWidth<char [15]>(char const (&) [15], int) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::RightPadToWidth<char [11]>(char const (&) [11], int) std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::RightPadToWidth<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int) Line | Count | Source | 125 | 86 | std::string RightPadToWidth(const T& val, int width) { | 126 | 86 | std::stringstream ss; | 127 | 86 | ss << val; | 128 | 86 | string ss_str = ss.str(); | 129 | 86 | int64_t padding = width - ss_str.size(); | 130 | 86 | if (padding <= 0) { | 131 | 4 | return ss_str; | 132 | 4 | } | 133 | 82 | return ss_str + string(padding, ' '); | 134 | 86 | } |
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::RightPadToWidth<char const*>(char const* const&, int) Line | Count | Source | 125 | 13 | std::string RightPadToWidth(const T& val, int width) { | 126 | 13 | std::stringstream ss; | 127 | 13 | ss << val; | 128 | 13 | string ss_str = ss.str(); | 129 | 13 | int64_t padding = width - ss_str.size(); | 130 | 13 | if (padding <= 0) { | 131 | 0 | return ss_str; | 132 | 0 | } | 133 | 13 | return ss_str + string(padding, ' '); | 134 | 13 | } |
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::RightPadToWidth<unsigned long long>(unsigned long long const&, int) std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::RightPadToWidth<char [6]>(char const (&) [6], int) Line | Count | Source | 125 | 13 | std::string RightPadToWidth(const T& val, int width) { | 126 | 13 | std::stringstream ss; | 127 | 13 | ss << val; | 128 | 13 | string ss_str = ss.str(); | 129 | 13 | int64_t padding = width - ss_str.size(); | 130 | 13 | if (padding <= 0) { | 131 | 0 | return ss_str; | 132 | 0 | } | 133 | 13 | return ss_str + string(padding, ' '); | 134 | 13 | } |
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::RightPadToWidth<char [10]>(char const (&) [10], int) Line | Count | Source | 125 | 2 | std::string RightPadToWidth(const T& val, int width) { | 126 | 2 | std::stringstream ss; | 127 | 2 | ss << val; | 128 | 2 | string ss_str = ss.str(); | 129 | 2 | int64_t padding = width - ss_str.size(); | 130 | 2 | if (padding <= 0) { | 131 | 0 | return ss_str; | 132 | 0 | } | 133 | 2 | return ss_str + string(padding, ' '); | 134 | 2 | } |
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::RightPadToWidth<char [5]>(char const (&) [5], int) |
135 | | |
136 | | // Returns true if s starts with substring start. |
137 | | bool StringStartsWithOrEquals(const std::string& s, const char* start, size_t start_len); |
138 | | |
139 | | inline bool StringStartsWithOrEquals(const std::string& s, const std::string start) { |
140 | | return StringStartsWithOrEquals(s, start.c_str(), start.length()); |
141 | | } |
142 | | |
143 | | // Returns true if s ends with substring end, and s has at least one more character before |
144 | | // end. If left is a valid string pointer, it will contain s minus the end substring. |
145 | | // Example 1: s = "15ms", end = "ms", then this function will return true and set left to "15". |
146 | | // Example 2: s = "ms", end = "ms", this function will return false. |
147 | | bool StringEndsWith( |
148 | | const std::string& s, const char* end, size_t end_len, std::string* left = nullptr); |
149 | | |
150 | | inline bool StringEndsWith( |
151 | 0 | const std::string& s, const std::string end, std::string* left = nullptr) { |
152 | 0 | return StringEndsWith(s, end.c_str(), end.length(), left); |
153 | 0 | } |
154 | | |
155 | | static constexpr const char* kDefaultSeparatorStr = ", "; |
156 | | |
157 | | // Append then given string to the given destination string. If the destination string is already |
158 | | // non-empty, append a separator first. |
159 | | void AppendWithSeparator(const std::string& to_append, |
160 | | std::string* dest, |
161 | | const char* separator = kDefaultSeparatorStr); |
162 | | |
163 | | void AppendWithSeparator(const char* to_append, |
164 | | std::string* dest, |
165 | | const char* separator = kDefaultSeparatorStr); |
166 | | |
167 | | template<class Container> |
168 | | auto unpack(Container&& container) { |
169 | | return details::Unpacker<Container>{std::forward<Container>(container)}; |
170 | | } |
171 | | |
172 | | template<class... Args> |
173 | | vector<string> ToStringVector(Args&&... args) { |
174 | | vector<string> result; |
175 | | result.reserve(details::ItemCount(args...)); |
176 | | details::AppendItem(&result, args...); |
177 | | return result; |
178 | | } |
179 | | |
180 | 480k | inline void EnlargeBufferIfNeeded(std::string* buffer, const size_t new_capacity) { |
181 | 480k | if (new_capacity <= buffer->capacity()) { |
182 | 479k | return; |
183 | 479k | } |
184 | 652 | buffer->reserve(new_capacity); |
185 | 652 | } |
186 | | |
187 | | // Takes a vector of strings and treats each element as a list of items separated by the given set |
188 | | // of separator characters (only comma by default). Splits each string using these separators and |
189 | | // returns the combined list of all items. |
190 | | std::vector<std::string> SplitAndFlatten( |
191 | | const std::vector<std::string>& input, |
192 | | const char* separators = ","); |
193 | | |
194 | | } // namespace yb |
195 | | |
196 | | namespace rocksdb { |
197 | | using yb::ToString; |
198 | | using yb::StringSplit; |
199 | | using yb::VectorToString; |
200 | | } |
201 | | |
202 | | #endif // YB_UTIL_STRING_UTIL_H |