|
| 1 | +/* |
| 2 | + * Copyright 2018 Google |
| 3 | + * |
| 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 |
| 7 | + * |
| 8 | + * http://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 | +#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_FORMAT_H_ |
| 18 | +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_FORMAT_H_ |
| 19 | + |
| 20 | +#include <initializer_list> |
| 21 | +#include <string> |
| 22 | +#include <utility> |
| 23 | + |
| 24 | +#include "absl/base/attributes.h" |
| 25 | +#include "absl/strings/str_cat.h" |
| 26 | +#include "absl/strings/string_view.h" |
| 27 | + |
| 28 | +namespace firebase { |
| 29 | +namespace firestore { |
| 30 | +namespace util { |
| 31 | + |
| 32 | +namespace internal { |
| 33 | + |
| 34 | +std::string StringFormatPieces(const char* format, |
| 35 | + std::initializer_list<absl::string_view> pieces); |
| 36 | + |
| 37 | +/** |
| 38 | + * Explicit ranking for formatting choices. Only useful as an implementation |
| 39 | + * detail of `FormatArg`. |
| 40 | + */ |
| 41 | +template <int I> |
| 42 | +struct FormatChoice : FormatChoice<I + 1> {}; |
| 43 | + |
| 44 | +template <> |
| 45 | +struct FormatChoice<4> {}; |
| 46 | + |
| 47 | +} // namespace internal |
| 48 | + |
| 49 | +/** |
| 50 | + * Acts as the main value parameter to StringFormat and related functions. |
| 51 | + * |
| 52 | + * Chooses a conversion to a text form in this order: |
| 53 | + * * If the value is exactly of `bool` type (without implicit conversions) |
| 54 | + * the text will the "true" or "false". |
| 55 | + * * If the value is of type `const char*`, the text will be the value |
| 56 | + * interpreted as a C string. To show the address of a single char or to |
| 57 | + * show the `const char*` as an address, cast to `void*`. |
| 58 | + * * If the value is any other pointer type, the text will be the hexidecimal |
| 59 | + * formatting of the value as an unsigned integer. |
| 60 | + * * Otherwise the value is interpreted as anything absl::AlphaNum accepts. |
| 61 | + */ |
| 62 | +class FormatArg : public absl::AlphaNum { |
| 63 | + public: |
| 64 | + template <typename T> |
| 65 | + FormatArg(T&& value) // NOLINT(runtime/explicit) |
| 66 | + : FormatArg{std::forward<T>(value), internal::FormatChoice<0>{}} { |
| 67 | + } |
| 68 | + |
| 69 | + private: |
| 70 | + /** |
| 71 | + * Creates a FormatArg from a boolean value, representing the string |
| 72 | + * "true" or "false". |
| 73 | + * |
| 74 | + * This overload only applies if the type of the argument is exactly `bool`. |
| 75 | + * No implicit conversions to bool are accepted. |
| 76 | + */ |
| 77 | + template <typename T, |
| 78 | + typename = typename std::enable_if<std::is_same<bool, T>{}>::type> |
| 79 | + FormatArg(T bool_value, internal::FormatChoice<0>) |
| 80 | + : AlphaNum{bool_value ? "true" : "false"} { |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Creates a FormatArg from a character string literal. This is |
| 85 | + * handled specially to avoid ambiguity with generic pointers, which are |
| 86 | + * handled differently. |
| 87 | + */ |
| 88 | + FormatArg(std::nullptr_t, internal::FormatChoice<1>) : AlphaNum{"null"} { |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Creates a FormatArg from a character string literal. This is |
| 93 | + * handled specially to avoid ambiguity with generic pointers, which are |
| 94 | + * handled differently. |
| 95 | + */ |
| 96 | + FormatArg(const char* string_value, internal::FormatChoice<2>) |
| 97 | + : AlphaNum{string_value == nullptr ? "null" : string_value} { |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Creates a FormatArg from an arbitrary pointer, represented as a |
| 102 | + * hexidecimal integer literal. |
| 103 | + */ |
| 104 | + template <typename T> |
| 105 | + FormatArg(T* pointer_value, internal::FormatChoice<3>) |
| 106 | + : AlphaNum{absl::Hex{reinterpret_cast<uintptr_t>(pointer_value)}} { |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * As a final fallback, creates a FormatArg from any type of value that |
| 111 | + * absl::AlphaNum accepts. |
| 112 | + */ |
| 113 | + template <typename T> |
| 114 | + FormatArg(T&& value, internal::FormatChoice<4>) |
| 115 | + : AlphaNum{std::forward<T>(value)} { |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +/** |
| 120 | + * Formats a string using a simplified printf-like formatting mechanism that |
| 121 | + * does not rely on C varargs. |
| 122 | + * |
| 123 | + * The following format specifiers are recognized: |
| 124 | + * * "%%" - A literal "%" |
| 125 | + * * "%s" - The next parameter is copied through |
| 126 | + * |
| 127 | + * Note: |
| 128 | + * * If you pass fewer arguments than the format requires, StringFormat will |
| 129 | + * insert "<missing>". |
| 130 | + * * If you pass more arguments than the format requires, any excess arguments |
| 131 | + * are ignored. |
| 132 | + * * If you use an invalid format specifier, StringFormat will insert |
| 133 | + * <invalid>. |
| 134 | + */ |
| 135 | +template <typename... FA> |
| 136 | +std::string StringFormat(const char* format, const FA&... args) { |
| 137 | + return internal::StringFormatPieces( |
| 138 | + format, {static_cast<const FormatArg&>(args).Piece()...}); |
| 139 | +} |
| 140 | + |
| 141 | +inline std::string StringFormat() { |
| 142 | + return {}; |
| 143 | +} |
| 144 | + |
| 145 | +} // namespace util |
| 146 | +} // namespace firestore |
| 147 | +} // namespace firebase |
| 148 | + |
| 149 | +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_FORMAT_H_ |
0 commit comments