2 * Copyright 2012-present 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.
21 #include <folly/Conv.h>
22 #include <folly/Likely.h>
23 #include <folly/Portability.h>
24 #include <folly/Range.h>
28 class BadFormatArg : public std::invalid_argument {
29 using invalid_argument::invalid_argument;
32 [[noreturn]] void throwBadFormatArg(char const* msg);
33 [[noreturn]] void throwBadFormatArg(std::string const& msg);
36 * Parsed format argument.
40 * Parse a format argument from a string. Keeps a reference to the
41 * passed-in string -- does not copy the given characters.
43 explicit FormatArg(StringPiece sp)
46 align(Align::DEFAULT),
49 thousandsSeparator(false),
53 precision(kDefaultPrecision),
54 presentation(kDefaultPresentation),
55 nextKeyMode_(NextKeyMode::NONE) {
67 * Validate the argument for the given type; throws on error.
69 void validate(Type type) const;
72 * Throw an exception if the first argument is false. The exception
73 * message will contain the argument string as well as any passed-in
74 * arguments to enforce, formatted using folly::to<std::string>.
76 template <typename... Args>
77 void enforce(bool v, Args&&... args) const {
79 error(std::forward<Args>(args)...);
83 template <typename... Args>
84 std::string errorStr(Args&&... args) const;
85 template <typename... Args>
86 [[noreturn]] void error(Args&&... args) const;
89 * Full argument string, as passed in to the constructor.
91 StringPiece fullArgString;
96 static constexpr char kDefaultFill = '\0';
102 enum class Align : uint8_t {
115 enum class Sign : uint8_t {
125 * Output base prefix (0 for octal, 0x for hex)
130 * Output thousands separator (comma)
132 bool thousandsSeparator;
135 * Force a trailing decimal on doubles which could be rendered as ints
140 * Field width and optional argument index
142 static constexpr int kDefaultWidth = -1;
143 static constexpr int kDynamicWidth = -2;
144 static constexpr int kNoIndex = -1;
151 static constexpr int kDefaultPrecision = -1;
157 static constexpr char kDefaultPresentation = '\0';
161 * Split a key component from "key", which must be non-empty (an exception
162 * is thrown otherwise).
164 template <bool emptyOk=false>
165 StringPiece splitKey();
168 * Is the entire key empty?
170 bool keyEmpty() const {
171 return nextKeyMode_ == NextKeyMode::NONE && key_.empty();
175 * Split an key component from "key", which must be non-empty and a valid
176 * integer (an exception is thrown otherwise).
180 void setNextIntKey(int val) {
181 assert(nextKeyMode_ == NextKeyMode::NONE);
182 nextKeyMode_ = NextKeyMode::INT;
186 void setNextKey(StringPiece val) {
187 assert(nextKeyMode_ == NextKeyMode::NONE);
188 nextKeyMode_ = NextKeyMode::STRING;
194 template <bool emptyOk>
195 StringPiece doSplitKey();
199 StringPiece nextKey_;
200 enum class NextKeyMode {
205 NextKeyMode nextKeyMode_;
208 template <typename... Args>
209 inline std::string FormatArg::errorStr(Args&&... args) const {
210 return to<std::string>(
211 "invalid format argument {", fullArgString, "}: ",
212 std::forward<Args>(args)...);
215 template <typename... Args>
216 [[noreturn]] inline void FormatArg::error(Args&&... args) const {
217 throwBadFormatArg(errorStr(std::forward<Args>(args)...));
220 template <bool emptyOk>
221 inline StringPiece FormatArg::splitKey() {
222 enforce(nextKeyMode_ != NextKeyMode::INT, "integer key expected");
223 return doSplitKey<emptyOk>();
226 template <bool emptyOk>
227 inline StringPiece FormatArg::doSplitKey() {
228 if (nextKeyMode_ == NextKeyMode::STRING) {
229 nextKeyMode_ = NextKeyMode::NONE;
230 if (!emptyOk) { // static
231 enforce(!nextKey_.empty(), "non-empty key required");
237 if (!emptyOk) { // static
238 error("non-empty key required");
240 return StringPiece();
243 const char* b = key_.begin();
244 const char* e = key_.end();
248 p = static_cast<const char*>(memchr(b, '[', size_t(e - b)));
249 enforce(p != nullptr, "unmatched ']'");
251 p = static_cast<const char*>(memchr(b, '.', size_t(e - b)));
254 key_.assign(p + 1, e);
259 if (!emptyOk) { // static
260 enforce(b != p, "non-empty key required");
262 return StringPiece(b, p);
265 inline int FormatArg::splitIntKey() {
266 if (nextKeyMode_ == NextKeyMode::INT) {
267 nextKeyMode_ = NextKeyMode::NONE;
271 return to<int>(doSplitKey<true>());
272 } catch (const std::out_of_range&) {
273 error("integer key required");
274 return 0; // unreached