2 * Copyright 2012 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef FOLLY_BASE_STRING_H_
18 #define FOLLY_BASE_STRING_H_
22 #include <boost/type_traits.hpp>
25 # include <ext/hash_set>
26 # include <ext/hash_map>
29 #include "folly/Conv.h"
30 #include "folly/FBString.h"
31 #include "folly/FBVector.h"
32 #include "folly/Range.h"
33 #include "folly/ScopeGuard.h"
35 // Compatibility function, to make sure toStdString(s) can be called
36 // to convert a std::string or fbstring variable s into type std::string
37 // with very little overhead if s was already std::string
41 std::string toStdString(const folly::fbstring& s) {
42 return std::string(s.data(), s.size());
46 const std::string& toStdString(const std::string& s) {
50 // If called with a temporary, the compiler will select this overload instead
51 // of the above, so we don't return a (lvalue) reference to a temporary.
53 std::string&& toStdString(std::string&& s) {
58 * C-Escape a string, making it suitable for representation as a C string
59 * literal. Appends the result to the output string.
61 * Backslashes all occurrences of backslash and double-quote:
65 * Replaces all non-printable ASCII characters with backslash-octal
69 * Note that we use backslash-octal instead of backslash-hex because the octal
70 * representation is guaranteed to consume no more than 3 characters; "\3760"
71 * represents two characters, one with value 254, and one with value 48 ('0'),
72 * whereas "\xfe0" represents only one character (with value 4064, which leads
73 * to implementation-defined behavior).
75 template <class String>
76 void cEscape(StringPiece str, String& out);
79 * Similar to cEscape above, but returns the escaped string.
81 template <class String>
82 String cEscape(StringPiece str) {
89 * C-Unescape a string; the opposite of cEscape above. Appends the result
90 * to the output string.
92 * Recognizes the standard C escape sequences:
94 * \' \" \? \\ \a \b \f \n \r \t \v
98 * In strict mode (default), throws std::invalid_argument if it encounters
99 * an unrecognized escape sequence. In non-strict mode, it leaves
100 * the escape sequence unchanged.
102 template <class String>
103 void cUnescape(StringPiece str, String& out, bool strict = true);
106 * Similar to cUnescape above, but returns the escaped string.
108 template <class String>
109 String cUnescape(StringPiece str, bool strict = true) {
111 cUnescape(str, out, strict);
116 * stringPrintf is much like printf but deposits its result into a
117 * string. Two signatures are supported: the first simply returns the
118 * resulting string, and the second appends the produced characters to
119 * the specified string and returns a reference to it.
121 std::string stringPrintf(const char* format, ...)
122 __attribute__ ((format (printf, 1, 2)));
124 /** Similar to stringPrintf, with different signiture.
126 void stringPrintf(std::string* out, const char* fmt, ...)
127 __attribute__ ((format (printf, 2, 3)));
129 std::string& stringAppendf(std::string* output, const char* format, ...)
130 __attribute__ ((format (printf, 2, 3)));
133 * Backslashify a string, that is, replace non-printable characters
134 * with C-style (but NOT C compliant) "\xHH" encoding. If hex_style
135 * is false, then shorthand notations like "\0" will be used instead
136 * of "\x00" for the most common backslash cases.
138 * There are two forms, one returning the input string, and one
139 * creating output in the specified output string.
141 * This is mainly intended for printing to a terminal, so it is not
142 * particularly optimized.
144 * Do *not* use this in situations where you expect to be able to feed
145 * the string to a C or C++ compiler, as there are nuances with how C
146 * parses such strings that lead to failures. This is for display
147 * purposed only. If you want a string you can embed for use in C or
148 * C++, use cEscape instead. This function is for display purposes
151 template <class String1, class String2>
152 void backslashify(const String1& input, String2& output, bool hex_style=false);
154 template <class String>
155 String backslashify(const String& input, bool hex_style=false) {
157 backslashify(input, output, hex_style);
162 * Take a string and "humanify" it -- that is, make it look better.
163 * Since "better" is subjective, caveat emptor. The basic approach is
164 * to count the number of unprintable characters. If there are none,
165 * then the output is the input. If there are relatively few, or if
166 * there is a long "enough" prefix of printable characters, use
167 * backslashify. If it is mostly binary, then simply hex encode.
169 * This is an attempt to make a computer smart, and so likely is wrong
172 template <class String1, class String2>
173 void humanify(const String1& input, String2& output);
175 template <class String>
176 String humanify(const String& input) {
178 humanify(input, output);
183 * Same functionality as Python's binascii.hexlify. Returns true
184 * on successful conversion.
186 * If append_output is true, append data to the output rather than
189 template<class InputString, class OutputString>
190 bool hexlify(const InputString& input, OutputString& output,
194 * Same functionality as Python's binascii.unhexlify. Returns true
195 * on successful conversion.
197 template<class InputString, class OutputString>
198 bool unhexlify(const InputString& input, OutputString& output);
201 * A pretty-printer for numbers that appends suffixes of units of the
202 * given type. It prints 4 sig-figs of value with the most
205 * If `addSpace' is true, we put a space between the units suffix and
209 * PRETTY_TIME - s, ms, us, ns, etc.
210 * PRETTY_BYTES_METRIC - kB, MB, GB, etc (goes up by 10^3 = 1000 each time)
211 * PRETTY_BYTES - kB, MB, GB, etc (goes up by 2^10 = 1024 each time)
212 * PRETTY_BYTES_IEC - KiB, MiB, GiB, etc
213 * PRETTY_UNITS_METRIC - k, M, G, etc (goes up by 10^3 = 1000 each time)
214 * PRETTY_UNITS_BINARY - k, M, G, etc (goes up by 2^10 = 1024 each time)
215 * PRETTY_UNITS_BINARY_IEC - Ki, Mi, Gi, etc
217 * @author Mark Rabkin <mrabkin@fb.com>
224 PRETTY_BYTES = PRETTY_BYTES_BINARY,
225 PRETTY_BYTES_BINARY_IEC,
226 PRETTY_BYTES_IEC = PRETTY_BYTES_BINARY_IEC,
230 PRETTY_UNITS_BINARY_IEC,
235 std::string prettyPrint(double val, PrettyType, bool addSpace = true);
238 * Write a hex dump of size bytes starting at ptr to out.
240 * The hex dump is formatted as follows:
242 * for the string "abcdefghijklmnopqrstuvwxyz\x02"
243 00000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 |abcdefghijklmnop|
244 00000010 71 72 73 74 75 76 77 78 79 7a 02 |qrstuvwxyz. |
246 * that is, we write 16 bytes per line, both as hex bytes and as printable
247 * characters. Non-printable characters are replaced with '.'
248 * Lines are written to out one by one (one StringPiece at a time) without
251 template <class OutIt>
252 void hexDump(const void* ptr, size_t size, OutIt out);
255 * Return the hex dump of size bytes starting at ptr as a string.
257 std::string hexDump(const void* ptr, size_t size);
260 * Return a fbstring containing the description of the given errno value.
261 * Takes care not to overwrite the actual system errno, so calling
262 * errnoStr(errno) is valid.
264 fbstring errnoStr(int err);
267 * Return the demangled (prettyfied) version of a C++ type.
269 * This function tries to produce a human-readable type, but the type name will
270 * be returned unchanged in case of error or if demangling isn't supported on
273 * Use for debugging -- do not rely on demangle() returning anything useful.
275 * This function may allocate memory (and therefore throw).
277 fbstring demangle(const char* name);
278 inline fbstring demangle(const std::type_info& type) {
279 return demangle(type.name());
283 * Debug string for an exception: include type and what().
285 inline fbstring exceptionStr(const std::exception& e) {
286 return folly::to<fbstring>(demangle(typeid(e)), ": ", e.what());
289 inline fbstring exceptionStr(std::exception_ptr ep) {
291 std::rethrow_exception(ep);
292 } catch (const std::exception& e) {
293 return exceptionStr(e);
295 return "<unknown exception>";
300 * Split a string into a list of tokens by delimiter.
302 * The split interface here supports different output types, selected
303 * at compile time: StringPiece, fbstring, or std::string. If you are
304 * using a vector to hold the output, it detects the type based on
305 * what your vector contains. If the output vector is not empty, split
306 * will append to the end of the vector.
308 * You can also use splitTo() to write the output to an arbitrary
309 * OutputIterator (e.g. std::inserter() on a std::set<>), in which
310 * case you have to tell the function the type. (Rationale:
311 * OutputIterators don't have a value_type, so we can't detect the
312 * type in splitTo without being told.)
316 * std::vector<folly::StringPiece> v;
317 * folly::split(":", "asd:bsd", v);
319 * std::set<StringPiece> s;
320 * folly::splitTo<StringPiece>(":", "asd:bsd:asd:csd",
321 * std::inserter(s, s.begin()));
323 * Split also takes a flag (ignoreEmpty) that indicates whether adjacent
324 * delimiters should be treated as one single separator (ignoring empty tokens)
325 * or not (generating empty tokens).
328 template<class Delim, class String, class OutputType>
329 void split(const Delim& delimiter,
331 std::vector<OutputType>& out,
332 bool ignoreEmpty = false);
334 template<class Delim, class String, class OutputType>
335 void split(const Delim& delimiter,
337 folly::fbvector<OutputType>& out,
338 bool ignoreEmpty = false);
340 template<class OutputValueType, class Delim, class String,
341 class OutputIterator>
342 void splitTo(const Delim& delimiter,
345 bool ignoreEmpty = false);
348 * Join list of tokens.
350 * Stores a string representation of tokens in the same order with
351 * deliminer between each element.
354 template <class Delim, class Iterator, class String>
355 void join(const Delim& delimiter,
360 template <class Delim, class Container, class String>
361 void join(const Delim& delimiter,
362 const Container& container,
364 join(delimiter, container.begin(), container.end(), output);
367 template <class Delim, class Container>
368 std::string join(const Delim& delimiter,
369 const Container& container) {
371 join(delimiter, container.begin(), container.end(), output);
377 // Hash functions for string and fbstring usable with e.g. hash_map
379 namespace __gnu_cxx {
382 struct hash<folly::basic_fbstring<C> > : private hash<const C*> {
383 size_t operator()(const folly::basic_fbstring<C> & s) const {
384 return hash<const C*>::operator()(s.c_str());
389 struct hash<std::basic_string<C> > : private hash<const C*> {
390 size_t operator()(const std::basic_string<C> & s) const {
391 return hash<const C*>::operator()(s.c_str());
395 } // namespace __gnu_cxx
398 // Hook into boost's type traits
401 struct has_nothrow_constructor<folly::basic_fbstring<T> > : true_type {
402 enum { value = true };
406 #include "folly/String-inl.h"