| 6 |
| // You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 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. |
| 15 | // |
| 16 | // ----------------------------------------------------------------------------- |
| 17 | // File: str_cat.h |
| 18 | // ----------------------------------------------------------------------------- |
| 19 | // |
| 20 | // This package contains functions for efficiently concatenating and appending |
| 21 | // strings: `StrCat()` and `StrAppend()`. Most of the work within these routines |
| 22 | // is actually handled through use of a special AlphaNum type, which was |
| 23 | // designed to be used as a parameter type that efficiently manages conversion |
| 24 | // to strings and avoids copies in the above operations. |
| 25 | // |
| 26 | // Any routine accepting either a string or a number may accept `AlphaNum`. |
| 27 | // The basic idea is that by accepting a `const AlphaNum &` as an argument |
| 28 | // to your function, your callers will automagically convert bools, integers, |
| 29 | // and floating point values to strings for you. |
| 30 | // |
| 31 | // NOTE: Use of `AlphaNum` outside of the //absl/strings package is unsupported |
| 32 | // except for the specific case of function parameters of type `AlphaNum` or |
| 33 | // `const AlphaNum &`. In particular, instantiating `AlphaNum` directly as a |
| 34 | // stack variable is not supported. |
| 35 | // |
| 36 | // Conversion from 8-bit values is not accepted because, if it were, then an |
| 37 | // attempt to pass ':' instead of ":" might result in a 58 ending up in your |
| 38 | // result. |
| 39 | // |
| 40 | // Bools convert to "0" or "1". Pointers to types other than `char *` are not |
| 41 | // valid inputs. No output is generated for null `char *` pointers. |
| 42 | // |
| 43 | // Floating point numbers are formatted with six-digit precision, which is |
| 44 | // the default for "std::cout <<" or printf "%g" (the same as "%.6g"). |
| 45 | // |
| 46 | // You can convert to hexadecimal output rather than decimal output using the |
| 47 | // `Hex` type contained here. To do so, pass `Hex(my_int)` as a parameter to |
| 48 | // `StrCat()` or `StrAppend()`. You may specify a minimum hex field width using |
| 49 | // a `PadSpec` enum. |
| 50 | // |
| 51 | // User-defined types can be formatted with the `AbslStringify()` customization |
| 52 | // point. The API relies on detecting an overload in the user-defined type's |
| 53 | // namespace of a free (non-member) `AbslStringify()` function as a definition |
| 54 | // (typically declared as a friend and implemented in-line. |
| 55 | // with the following signature: |
| 56 | // |
| 57 | // class MyClass { ... }; |
| 58 | // |
| 59 | // template <typename Sink> |
| 60 | // void AbslStringify(Sink& sink, const MyClass& value); |
| 61 | // |
| 62 | // An `AbslStringify()` overload for a type should only be declared in the same |
| 63 | // file and namespace as said type. |
| 64 | // |
| 65 | // Note that `AbslStringify()` also supports use with `absl::StrFormat()` and |
| 66 | // `absl::Substitute()`. |
| 67 | // |
| 68 | // Example: |
| 69 | // |
| 70 | // struct Point { |
| 71 | // // To add formatting support to `Point`, we simply need to add a free |
| 72 | // // (non-member) function `AbslStringify()`. This method specifies how |
| 73 | // // Point should be printed when absl::StrCat() is called on it. You can add |
| 74 | // // such a free function using a friend declaration within the body of the |
| 75 | // // class. The sink parameter is a templated type to avoid requiring |
| 76 | // // dependencies. |
| 77 | // template <typename Sink> friend void AbslStringify(Sink& |
| 78 | // sink, const Point& p) { |
| 79 | // absl::Format(&sink, "(%v, %v)", p.x, p.y); |
| 80 | // } |
| 81 | // |
| 82 | // int x; |
| 83 | // int y; |
| 84 | // }; |
| 85 | // ----------------------------------------------------------------------------- |
| 86 |
| 87 | #ifndef ABSL_STRINGS_STR_CAT_H_ |
| 88 | #define ABSL_STRINGS_STR_CAT_H_ |
| 89 |
| 90 | #include <array> |
| 91 | #include <cstdint> |
| 92 | #include <string> |
| 93 | #include <type_traits> |
| 94 | #include <utility> |
| 95 | #include <vector> |
| 96 |
| 97 | #include "absl/base/port.h" |
| 98 | #include "absl/strings/internal/has_absl_stringify.h" |
| 99 | #include "absl/strings/internal/stringify_sink.h" |
| 100 | #include "absl/strings/numbers.h" |
| 101 | #include "absl/strings/string_view.h" |
| 102 |
| 103 | namespace absl { |
| 104 | ABSL_NAMESPACE_BEGIN |
| 105 |
| 106 | namespace strings_internal { |
| 107 | // AlphaNumBuffer allows a way to pass a string to StrCat without having to do |
| 108 | // memory allocation. It is simply a pair of a fixed-size character array, and |
| 109 | // a size. Please don't use outside of absl, yet. |
| 110 | template <size_t max_size> |
| 111 | struct AlphaNumBuffer { |
| 112 | std::array<char, max_size> data; |
| 113 | size_t size; |
| 114 | }; |
| 115 |
| 116 | } // namespace strings_internal |
| 117 |
| 118 | // Enum that specifies the number of significant digits to return in a `Hex` or |
| 119 | // `Dec` conversion and fill character to use. A `kZeroPad2` value, for example, |
| 120 | // would produce hexadecimal strings such as "0a","0f" and a 'kSpacePad5' value |
| 121 | // would produce hexadecimal strings such as " a"," f". |
| 122 | enum PadSpec : uint8_t { |
| 123 | kNoPad = 1, |
| 124 | kZeroPad2, |
| 125 | kZeroPad3, |
| 126 | kZeroPad4, |
| 127 | kZeroPad5, |
| 128 | kZeroPad6, |
| 129 | kZeroPad7, |
| 130 | kZeroPad8, |
| 131 | kZeroPad9, |
| 132 | kZeroPad10, |
| 133 | kZeroPad11, |
| 134 | kZeroPad12, |
| 135 | kZeroPad13, |
| 136 | kZeroPad14, |
| 137 | kZeroPad15, |
| 138 | kZeroPad16, |
| 139 | kZeroPad17, |
| 140 | kZeroPad18, |
| 141 | kZeroPad19, |
| 142 | kZeroPad20, |
| 143 |
| 144 | kSpacePad2 = kZeroPad2 + 64, |
| 145 | kSpacePad3, |
| 146 | kSpacePad4, |
| 147 | kSpacePad5, |
| 148 | kSpacePad6, |
| 149 | kSpacePad7, |
| 150 | kSpacePad8, |
| 151 | kSpacePad9, |
| 152 | kSpacePad10, |
| 153 | kSpacePad11, |
| 154 | kSpacePad12, |
| 155 | kSpacePad13, |
| 156 | kSpacePad14, |
| 157 | kSpacePad15, |
| 158 | kSpacePad16, |
| 159 | kSpacePad17, |
| 160 | kSpacePad18, |
| 161 | kSpacePad19, |
| 162 | kSpacePad20, |
| 163 | }; |
| 164 |
| 165 | // ----------------------------------------------------------------------------- |
| 166 | // Hex |
| 167 | // ----------------------------------------------------------------------------- |
| 168 | // |
| 169 | // `Hex` stores a set of hexadecimal string conversion parameters for use |
| 170 | // within `AlphaNum` string conversions. |
| 171 | struct Hex { |
| 172 | uint64_t value; |
| 173 | uint8_t width; |
| 174 | char fill; |
| 175 |
| 176 | template <typename Int> |
| 177 | explicit Hex( |
| 178 | Int v, PadSpec spec = absl::kNoPad, |
| 179 | typename std::enable_if<sizeof(Int) == 1 && |
| 180 | !std::is_pointer<Int>::value>::type* = nullptr) |
| 181 | : Hex(spec, static_cast<uint8_t>(v)) {} |
| 182 | template <typename Int> |
| 183 | explicit Hex( |
| 184 | Int v, PadSpec spec = absl::kNoPad, |
| 185 | typename std::enable_if<sizeof(Int) == 2 && |
| 186 | !std::is_pointer<Int>::value>::type* = nullptr) |
| 187 | : Hex(spec, static_cast<uint16_t>(v)) {} |
| 188 | template <typename Int> |
| 189 | explicit Hex( |
| 190 | Int v, PadSpec spec = absl::kNoPad, |
| 191 | typename std::enable_if<sizeof(Int) == 4 && |
| 192 | !std::is_pointer<Int>::value>::type* = nullptr) |
| 193 | : Hex(spec, static_cast<uint32_t>(v)) {} |
| 194 | template <typename Int> |
| 195 | explicit Hex( |
| 196 | Int v, PadSpec spec = absl::kNoPad, |
| 197 | typename std::enable_if<sizeof(Int) == 8 && |
| 198 | !std::is_pointer<Int>::value>::type* = nullptr) |
| 199 | : Hex(spec, static_cast<uint64_t>(v)) {} |
| 200 | template <typename Pointee> |
| 201 | explicit Hex(Pointee* v, PadSpec spec = absl::kNoPad) |
| 202 | : Hex(spec, reinterpret_cast<uintptr_t>(v)) {} |
| 203 |
| 204 | private: |
| 205 | Hex(PadSpec spec, uint64_t v) |
| 206 | : value(v), |
| 207 | width(spec == absl::kNoPad |
| 208 | ? 1 |
| 209 | : spec >= absl::kSpacePad2 ? spec - absl::kSpacePad2 + 2 |
| 210 | : spec - absl::kZeroPad2 + 2), |
| 211 | fill(spec >= absl::kSpacePad2 ? ' ' : '0') {} |
| 212 | }; |
| 213 |
| 214 | // ----------------------------------------------------------------------------- |
| 215 | // Dec |
| 216 | // ----------------------------------------------------------------------------- |
| 217 | // |
| 218 | // `Dec` stores a set of decimal string conversion parameters for use |
| 219 | // within `AlphaNum` string conversions. Dec is slower than the default |
| 220 | // integer conversion, so use it only if you need padding. |
| 221 | struct Dec { |
| 222 | uint64_t value; |
| 223 | uint8_t width; |
| 224 | char fill; |
| 225 | bool neg; |
| 226 |
| 227 | template <typename Int> |
| 228 | explicit Dec(Int v, PadSpec spec = absl::kNoPad, |
| 229 | typename std::enable_if<(sizeof(Int) <= 8)>::type* = nullptr) |
| 230 | : value(v >= 0 ? static_cast<uint64_t>(v) |
| 231 | : uint64_t{0} - static_cast<uint64_t>(v)), |
| 232 | width(spec == absl::kNoPad |
| 233 | ? 1 |
| 234 | : spec >= absl::kSpacePad2 ? spec - absl::kSpacePad2 + 2 |
| 235 | : spec - absl::kZeroPad2 + 2), |
| 236 | fill(spec >= absl::kSpacePad2 ? ' ' : '0'), |
| 237 | neg(v < 0) {} |
| 238 | }; |
| 239 |
| 240 | // ----------------------------------------------------------------------------- |
| 241 | // AlphaNum |
| 242 | // ----------------------------------------------------------------------------- |
| 243 | // |
| 244 | // The `AlphaNum` class acts as the main parameter type for `StrCat()` and |
| 245 | // `StrAppend()`, providing efficient conversion of numeric, boolean, and |
| 246 | // hexadecimal values (through the `Hex` type) into strings. |
| 247 |
| 248 | class AlphaNum { |
| 249 | public: |
| 250 | // No bool ctor -- bools convert to an integral type. |
| 251 | // A bool ctor would also convert incoming pointers (bletch). |
| 252 |
| 253 | AlphaNum(int x) // NOLINT(runtime/explicit) |
| 254 | : piece_(digits_, static_cast<size_t>( |
| 255 | numbers_internal::FastIntToBuffer(x, digits_) - |
| 256 | &digits_[0])) {} |
| 257 | AlphaNum(unsigned int x) // NOLINT(runtime/explicit) |
| 258 | : piece_(digits_, static_cast<size_t>( |
| 259 | numbers_internal::FastIntToBuffer(x, digits_) - |
| 260 | &digits_[0])) {} |
| 261 | AlphaNum(long x) // NOLINT(*) |
| 262 | : piece_(digits_, static_cast<size_t>( |
| 263 | numbers_internal::FastIntToBuffer(x, digits_) - |
| 264 | &digits_[0])) {} |
| 265 | AlphaNum(unsigned long x) // NOLINT(*) |
| 266 | : piece_(digits_, static_cast<size_t>( |
| 267 | numbers_internal::FastIntToBuffer(x, digits_) - |
| 268 | &digits_[0])) {} |
| 269 | AlphaNum(long long x) // NOLINT(*) |
| 270 | : piece_(digits_, static_cast<size_t>( |
| 271 | numbers_internal::FastIntToBuffer(x, digits_) - |
| 272 | &digits_[0])) {} |
| 273 | AlphaNum(unsigned long long x) // NOLINT(*) |
| 274 | : piece_(digits_, static_cast<size_t>( |
| 275 | numbers_internal::FastIntToBuffer(x, digits_) - |
| 276 | &digits_[0])) {} |
| 277 |
| 278 | AlphaNum(float f) // NOLINT(runtime/explicit) |
| 279 | : piece_(digits_, numbers_internal::SixDigitsToBuffer(f, digits_)) {} |
| 280 | AlphaNum(double f) // NOLINT(runtime/explicit) |
| 281 | : piece_(digits_, numbers_internal::SixDigitsToBuffer(f, digits_)) {} |
| 282 |
| 283 | AlphaNum(Hex hex); // NOLINT(runtime/explicit) |
| 284 | AlphaNum(Dec dec); // NOLINT(runtime/explicit) |
| 285 |
| 286 | template <size_t size> |
| 287 | AlphaNum( // NOLINT(runtime/explicit) |
| 288 | const strings_internal::AlphaNumBuffer<size>& buf) |
| 289 | : piece_(&buf.data[0], buf.size) {} |
| 290 |
| 291 | AlphaNum(const char* c_str) // NOLINT(runtime/explicit) |
| 292 | : piece_(NullSafeStringView(c_str)) {} // NOLINT(runtime/explicit) |
| 293 | AlphaNum(absl::string_view pc) : piece_(pc) {} // NOLINT(runtime/explicit) |
| 294 |
| 295 | template <typename T, typename = typename std::enable_if< |
| 296 | strings_internal::HasAbslStringify<T>::value>::type> |
| 297 | AlphaNum( // NOLINT(runtime/explicit) |
| 298 | const T& v, // NOLINT(runtime/explicit) |
| 299 | strings_internal::StringifySink&& sink = {}) // NOLINT(runtime/explicit) |
| 300 | : piece_(strings_internal::ExtractStringification(sink, v)) {} |
| 301 |
| 302 | template <typename Allocator> |
| 303 | AlphaNum( // NOLINT(runtime/explicit) |
| 304 | const std::basic_string<char, std::char_traits<char>, Allocator>& str) |
| 305 | : piece_(str) {} |
| 306 |
| 307 | // Use string literals ":" instead of character literals ':'. |
| 308 | AlphaNum(char c) = delete; // NOLINT(runtime/explicit) |
| 309 |
| 310 | AlphaNum(const AlphaNum&) = delete; |
| 311 | AlphaNum& operator=(const AlphaNum&) = delete; |
| 312 |
| 313 | absl::string_view::size_type size() const { return piece_.size(); } |
| 314 | const char* data() const { return piece_.data(); } |
| 315 | absl::string_view Piece() const { return piece_; } |
| 316 |
| 317 | // Normal enums are already handled by the integer formatters. |
| 318 | // This overload matches only scoped enums. |
| 319 | template <typename T, |
| 320 | typename = typename std::enable_if< |
| 321 | std::is_enum<T>{} && !std::is_convertible<T, int>{} && |
| 322 | !strings_internal::HasAbslStringify<T>::value>::type> |
| 323 | AlphaNum(T e) // NOLINT(runtime/explicit) |
| 324 | : AlphaNum(static_cast<typename std::underlying_type<T>::type>(e)) {} |
| 325 |
| 326 | // vector<bool>::reference and const_reference require special help to |
| 327 | // convert to `AlphaNum` because it requires two user defined conversions. |
| 328 | template < |
| 329 | typename T, |
| 330 | typename std::enable_if< |
| 331 | std::is_class<T>::value && |
| 332 | (std::is_same<T, std::vector<bool>::reference>::value || |
| 333 | std::is_same<T, std::vector<bool>::const_reference>::value)>::type* = |
| 334 | nullptr> |
| 335 | AlphaNum(T e) : AlphaNum(static_cast<bool>(e)) {} // NOLINT(runtime/explicit) |
| 336 |
| 337 | private: |
| 338 | absl::string_view piece_; |
| 339 | char digits_[numbers_internal::kFastToBufferSize]; |
| 340 | }; |
| 341 |
| 342 | // ----------------------------------------------------------------------------- |
| 343 | // StrCat() |
| 344 | // ----------------------------------------------------------------------------- |
| 345 | // |
| 346 | // Merges given strings or numbers, using no delimiter(s), returning the merged |
| 347 | // result as a string. |
| 348 | // |
| 349 | // `StrCat()` is designed to be the fastest possible way to construct a string |
| 350 | // out of a mix of raw C strings, string_views, strings, bool values, |
| 351 | // and numeric values. |
| 352 | // |
| 353 | // Don't use `StrCat()` for user-visible strings. The localization process |
| 354 | // works poorly on strings built up out of fragments. |
| 355 | // |
| 356 | // For clarity and performance, don't use `StrCat()` when appending to a |
| 357 | // string. Use `StrAppend()` instead. In particular, avoid using any of these |
| 358 | // (anti-)patterns: |
| 359 | // |
| 360 | // str.append(StrCat(...)) |
| 361 | // str += StrCat(...) |
| 362 | // str = StrCat(str, ...) |
| 363 | // |
| 364 | // The last case is the worst, with a potential to change a loop |
| 365 | // from a linear time operation with O(1) dynamic allocations into a |
| 366 | // quadratic time operation with O(n) dynamic allocations. |
| 367 | // |
| 368 | // See `StrAppend()` below for more information. |
| 369 |
| 370 | namespace strings_internal { |
| 371 |
| 372 | // Do not call directly - this is not part of the public API. |
| 373 | std::string CatPieces(std::initializer_list<absl::string_view> pieces); |
| 374 | void AppendPieces(std::string* dest, |
| 375 | std::initializer_list<absl::string_view> pieces); |
| 376 |
| 377 | } // namespace strings_internal |
| 378 |
| 379 | ABSL_MUST_USE_RESULT inline std::string StrCat() { return std::string(); } |
| 380 |
| 381 | ABSL_MUST_USE_RESULT inline std::string StrCat(const AlphaNum& a) { |
| 382 | return std::string(a.data(), a.size()); |
| 383 | } |
| 384 |
| 385 | ABSL_MUST_USE_RESULT std::string StrCat(const AlphaNum& a, const AlphaNum& b); |
| 386 | ABSL_MUST_USE_RESULT std::string StrCat(const AlphaNum& a, const AlphaNum& b, |
| 387 | const AlphaNum& c); |
| 388 | ABSL_MUST_USE_RESULT std::string StrCat(const AlphaNum& a, const AlphaNum& b, |
| 389 | const AlphaNum& c, const AlphaNum& d); |
| 390 |
| 391 | // Support 5 or more arguments |
| 392 | template <typename... AV> |
| 393 | ABSL_MUST_USE_RESULT inline std::string StrCat( |
| 394 | const AlphaNum& a, const AlphaNum& b, const AlphaNum& c, const AlphaNum& d, |
| 395 | const AlphaNum& e, const AV&... args) { |
| 396 | return strings_internal::CatPieces( |
| 397 | {a.Piece(), b.Piece(), c.Piece(), d.Piece(), e.Piece(), |
| 398 | static_cast<const AlphaNum&>(args).Piece()...}); |
| 399 | } |
| 400 |
| 401 | // ----------------------------------------------------------------------------- |
| 402 | // StrAppend() |
| 403 | // ----------------------------------------------------------------------------- |
| 404 | // |
| 405 | // Appends a string or set of strings to an existing string, in a similar |
| 406 | // fashion to `StrCat()`. |
| 407 | // |
| 408 | // WARNING: `StrAppend(&str, a, b, c, ...)` requires that none of the |
| 409 | // a, b, c, parameters be a reference into str. For speed, `StrAppend()` does |
| 410 | // not try to check each of its input arguments to be sure that they are not |
| 411 | // a subset of the string being appended to. That is, while this will work: |
| 412 | // |
| 413 | // std::string s = "foo"; |
| 414 | // s += s; |
| 415 | // |
| 416 | // This output is undefined: |
| 417 | // |
| 418 | // std::string s = "foo"; |
| 419 | // StrAppend(&s, s); |
| 420 | // |
| 421 | // This output is undefined as well, since `absl::string_view` does not own its |
| 422 | // data: |
| 423 | // |
| 424 | // std::string s = "foobar"; |
| 425 | // absl::string_view p = s; |
| 426 | // StrAppend(&s, p); |
| 427 |
| 428 | inline void StrAppend(std::string*) {} |
| 429 | void StrAppend(std::string* dest, const AlphaNum& a); |
| 430 | void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b); |
| 431 | void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b, |
| 432 | const AlphaNum& c); |
| 433 | void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b, |
| 434 | const AlphaNum& c, const AlphaNum& d); |
| 435 |
| 436 | // Support 5 or more arguments |
| 437 | template <typename... AV> |
| 438 | inline void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b, |
| 439 | const AlphaNum& c, const AlphaNum& d, const AlphaNum& e, |
| 440 | const AV&... args) { |
| 441 | strings_internal::AppendPieces( |
| 442 | dest, {a.Piece(), b.Piece(), c.Piece(), d.Piece(), e.Piece(), |
| 443 | static_cast<const AlphaNum&>(args).Piece()...}); |
| 444 | } |
| 445 |
| 446 | // Helper function for the future StrCat default floating-point format, %.6g |
| 447 | // This is fast. |
| 448 | inline strings_internal::AlphaNumBuffer< |
| 449 | numbers_internal::kSixDigitsToBufferSize> |
| 450 | SixDigits(double d) { |
| 451 | strings_internal::AlphaNumBuffer<numbers_internal::kSixDigitsToBufferSize> |
| 452 | result; |
| 453 | result.size = numbers_internal::SixDigitsToBuffer(d, &result.data[0]); |
| 454 | return result; |
| 455 | } |
| 456 |
| 457 | ABSL_NAMESPACE_END |
| 458 | } // namespace absl |
| 459 |
| 460 | #endif // ABSL_STRINGS_STR_CAT_H_ |
| 461 |