2 * Copyright 2013 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_FORMATARG_H_
18 #define FOLLY_FORMATARG_H_
21 #include "folly/Range.h"
22 #include "folly/Likely.h"
23 #include "folly/Conv.h"
28 * Parsed format argument.
32 * Parse a format argument from a string. Keeps a reference to the
33 * passed-in string -- does not copy the given characters.
35 explicit FormatArg(StringPiece sp)
38 align(Align::DEFAULT),
41 thousandsSeparator(false),
43 precision(kDefaultPrecision),
44 presentation(kDefaultPresentation),
45 nextKeyMode_(NextKeyMode::NONE) {
57 * Validate the argument for the given type; throws on error.
59 void validate(Type type) const;
62 * Throw an exception if the first argument is false. The exception
63 * message will contain the argument string as well as any passed-in
64 * arguments to enforce, formatted using folly::to<std::string>.
66 template <typename... Args>
67 void enforce(bool v, Args&&... args) const {
69 error(std::forward<Args>(args)...);
73 template <typename... Args>
74 void error(Args&&... args) const __attribute__((noreturn));
76 * Full argument string, as passed in to the constructor.
78 StringPiece fullArgString;
83 static constexpr char kDefaultFill = '\0';
89 enum class Align : uint8_t {
102 enum class Sign : uint8_t {
112 * Output base prefix (0 for octal, 0x for hex)
117 * Output thousands separator (comma)
119 bool thousandsSeparator;
124 static constexpr int kDefaultWidth = -1;
130 static constexpr int kDefaultPrecision = -1;
136 static constexpr char kDefaultPresentation = '\0';
140 * Split a key component from "key", which must be non-empty (an exception
141 * is thrown otherwise).
143 template <bool emptyOk=false>
144 StringPiece splitKey();
147 * Is the entire key empty?
149 bool keyEmpty() const {
150 return nextKeyMode_ == NextKeyMode::NONE && key_.empty();
154 * Split an key component from "key", which must be non-empty and a valid
155 * integer (an exception is thrown otherwise).
159 void setNextIntKey(int val) {
160 assert(nextKeyMode_ == NextKeyMode::NONE);
161 nextKeyMode_ = NextKeyMode::INT;
165 void setNextKey(StringPiece val) {
166 assert(nextKeyMode_ == NextKeyMode::NONE);
167 nextKeyMode_ = NextKeyMode::STRING;
173 template <bool emptyOk>
174 StringPiece doSplitKey();
178 StringPiece nextKey_;
179 enum class NextKeyMode {
184 NextKeyMode nextKeyMode_;
187 template <typename... Args>
188 inline void FormatArg::error(Args&&... args) const {
189 throw std::invalid_argument(to<std::string>(
190 "folly::format: invalid format argument {", fullArgString, "}: ",
191 std::forward<Args>(args)...));
194 template <bool emptyOk>
195 inline StringPiece FormatArg::splitKey() {
196 enforce(nextKeyMode_ != NextKeyMode::INT, "integer key expected");
197 return doSplitKey<emptyOk>();
200 template <bool emptyOk>
201 inline StringPiece FormatArg::doSplitKey() {
202 if (nextKeyMode_ == NextKeyMode::STRING) {
203 nextKeyMode_ = NextKeyMode::NONE;
204 if (!emptyOk) { // static
205 enforce(!nextKey_.empty(), "non-empty key required");
211 if (!emptyOk) { // static
212 error("non-empty key required");
214 return StringPiece();
217 const char* b = key_.begin();
218 const char* e = key_.end();
222 p = static_cast<const char*>(memchr(b, '[', e - b));
223 enforce(p, "unmatched ']'");
225 p = static_cast<const char*>(memchr(b, '.', e - b));
228 key_.assign(p + 1, e);
233 if (!emptyOk) { // static
234 enforce(b != p, "non-empty key required");
236 return StringPiece(b, p);
239 inline int FormatArg::splitIntKey() {
240 if (nextKeyMode_ == NextKeyMode::INT) {
241 nextKeyMode_ = NextKeyMode::NONE;
245 return to<int>(doSplitKey<true>());
246 } catch (const std::out_of_range& e) {
247 error("integer key required");
248 return 0; // unreached
254 #endif /* FOLLY_FORMATARG_H_ */