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/Conv.h"
22 #include "folly/Likely.h"
23 #include "folly/Portability.h"
24 #include "folly/Range.h"
29 * Parsed format argument.
33 * Parse a format argument from a string. Keeps a reference to the
34 * passed-in string -- does not copy the given characters.
36 explicit FormatArg(StringPiece sp)
39 align(Align::DEFAULT),
42 thousandsSeparator(false),
44 precision(kDefaultPrecision),
45 presentation(kDefaultPresentation),
46 nextKeyMode_(NextKeyMode::NONE) {
58 * Validate the argument for the given type; throws on error.
60 void validate(Type type) const;
63 * Throw an exception if the first argument is false. The exception
64 * message will contain the argument string as well as any passed-in
65 * arguments to enforce, formatted using folly::to<std::string>.
67 template <typename... Args>
68 void enforce(bool v, Args&&... args) const {
70 error(std::forward<Args>(args)...);
74 template <typename... Args>
75 void error(Args&&... args) const FOLLY_NORETURN;
77 * Full argument string, as passed in to the constructor.
79 StringPiece fullArgString;
84 static constexpr char kDefaultFill = '\0';
90 enum class Align : uint8_t {
103 enum class Sign : uint8_t {
113 * Output base prefix (0 for octal, 0x for hex)
118 * Output thousands separator (comma)
120 bool thousandsSeparator;
125 static constexpr int kDefaultWidth = -1;
131 static constexpr int kDefaultPrecision = -1;
137 static constexpr char kDefaultPresentation = '\0';
141 * Split a key component from "key", which must be non-empty (an exception
142 * is thrown otherwise).
144 template <bool emptyOk=false>
145 StringPiece splitKey();
148 * Is the entire key empty?
150 bool keyEmpty() const {
151 return nextKeyMode_ == NextKeyMode::NONE && key_.empty();
155 * Split an key component from "key", which must be non-empty and a valid
156 * integer (an exception is thrown otherwise).
160 void setNextIntKey(int val) {
161 assert(nextKeyMode_ == NextKeyMode::NONE);
162 nextKeyMode_ = NextKeyMode::INT;
166 void setNextKey(StringPiece val) {
167 assert(nextKeyMode_ == NextKeyMode::NONE);
168 nextKeyMode_ = NextKeyMode::STRING;
174 template <bool emptyOk>
175 StringPiece doSplitKey();
179 StringPiece nextKey_;
180 enum class NextKeyMode {
185 NextKeyMode nextKeyMode_;
188 template <typename... Args>
189 inline void FormatArg::error(Args&&... args) const {
190 throw std::invalid_argument(to<std::string>(
191 "folly::format: invalid format argument {", fullArgString, "}: ",
192 std::forward<Args>(args)...));
195 template <bool emptyOk>
196 inline StringPiece FormatArg::splitKey() {
197 enforce(nextKeyMode_ != NextKeyMode::INT, "integer key expected");
198 return doSplitKey<emptyOk>();
201 template <bool emptyOk>
202 inline StringPiece FormatArg::doSplitKey() {
203 if (nextKeyMode_ == NextKeyMode::STRING) {
204 nextKeyMode_ = NextKeyMode::NONE;
205 if (!emptyOk) { // static
206 enforce(!nextKey_.empty(), "non-empty key required");
212 if (!emptyOk) { // static
213 error("non-empty key required");
215 return StringPiece();
218 const char* b = key_.begin();
219 const char* e = key_.end();
223 p = static_cast<const char*>(memchr(b, '[', e - b));
224 enforce(p, "unmatched ']'");
226 p = static_cast<const char*>(memchr(b, '.', e - b));
229 key_.assign(p + 1, e);
234 if (!emptyOk) { // static
235 enforce(b != p, "non-empty key required");
237 return StringPiece(b, p);
240 inline int FormatArg::splitIntKey() {
241 if (nextKeyMode_ == NextKeyMode::INT) {
242 nextKeyMode_ = NextKeyMode::NONE;
246 return to<int>(doSplitKey<true>());
247 } catch (const std::out_of_range& e) {
248 error("integer key required");
249 return 0; // unreached
255 #endif /* FOLLY_FORMATARG_H_ */