2 * Copyright 2015 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>
28 class BadFormatArg : public std::invalid_argument {
30 explicit BadFormatArg(const std::string& msg)
31 : std::invalid_argument(msg) {}
35 * Parsed format argument.
39 * Parse a format argument from a string. Keeps a reference to the
40 * passed-in string -- does not copy the given characters.
42 explicit FormatArg(StringPiece sp)
45 align(Align::DEFAULT),
48 thousandsSeparator(false),
52 precision(kDefaultPrecision),
53 presentation(kDefaultPresentation),
54 nextKeyMode_(NextKeyMode::NONE) {
66 * Validate the argument for the given type; throws on error.
68 void validate(Type type) const;
71 * Throw an exception if the first argument is false. The exception
72 * message will contain the argument string as well as any passed-in
73 * arguments to enforce, formatted using folly::to<std::string>.
75 template <typename... Args>
76 void enforce(bool v, Args&&... args) const {
78 error(std::forward<Args>(args)...);
82 template <typename... Args>
83 std::string errorStr(Args&&... args) const;
84 template <typename... Args>
85 FOLLY_NORETURN void error(Args&&... args) const;
88 * Full argument string, as passed in to the constructor.
90 StringPiece fullArgString;
95 static constexpr char kDefaultFill = '\0';
101 enum class Align : uint8_t {
114 enum class Sign : uint8_t {
124 * Output base prefix (0 for octal, 0x for hex)
129 * Output thousands separator (comma)
131 bool thousandsSeparator;
134 * Force a trailing decimal on doubles which could be rendered as ints
139 * Field width and optional argument index
141 static constexpr int kDefaultWidth = -1;
142 static constexpr int kDynamicWidth = -2;
143 static constexpr int kNoIndex = -1;
150 static constexpr int kDefaultPrecision = -1;
156 static constexpr char kDefaultPresentation = '\0';
160 * Split a key component from "key", which must be non-empty (an exception
161 * is thrown otherwise).
163 template <bool emptyOk=false>
164 StringPiece splitKey();
167 * Is the entire key empty?
169 bool keyEmpty() const {
170 return nextKeyMode_ == NextKeyMode::NONE && key_.empty();
174 * Split an key component from "key", which must be non-empty and a valid
175 * integer (an exception is thrown otherwise).
179 void setNextIntKey(int val) {
180 assert(nextKeyMode_ == NextKeyMode::NONE);
181 nextKeyMode_ = NextKeyMode::INT;
185 void setNextKey(StringPiece val) {
186 assert(nextKeyMode_ == NextKeyMode::NONE);
187 nextKeyMode_ = NextKeyMode::STRING;
193 template <bool emptyOk>
194 StringPiece doSplitKey();
198 StringPiece nextKey_;
199 enum class NextKeyMode {
204 NextKeyMode nextKeyMode_;
207 template <typename... Args>
208 inline std::string FormatArg::errorStr(Args&&... args) const {
209 return to<std::string>(
210 "invalid format argument {", fullArgString, "}: ",
211 std::forward<Args>(args)...);
214 template <typename... Args>
215 inline void FormatArg::error(Args&&... args) const {
216 throw BadFormatArg(errorStr(std::forward<Args>(args)...));
219 template <bool emptyOk>
220 inline StringPiece FormatArg::splitKey() {
221 enforce(nextKeyMode_ != NextKeyMode::INT, "integer key expected");
222 return doSplitKey<emptyOk>();
225 template <bool emptyOk>
226 inline StringPiece FormatArg::doSplitKey() {
227 if (nextKeyMode_ == NextKeyMode::STRING) {
228 nextKeyMode_ = NextKeyMode::NONE;
229 if (!emptyOk) { // static
230 enforce(!nextKey_.empty(), "non-empty key required");
236 if (!emptyOk) { // static
237 error("non-empty key required");
239 return StringPiece();
242 const char* b = key_.begin();
243 const char* e = key_.end();
247 p = static_cast<const char*>(memchr(b, '[', e - b));
248 enforce(p, "unmatched ']'");
250 p = static_cast<const char*>(memchr(b, '.', e - b));
253 key_.assign(p + 1, e);
258 if (!emptyOk) { // static
259 enforce(b != p, "non-empty key required");
261 return StringPiece(b, p);
264 inline int FormatArg::splitIntKey() {
265 if (nextKeyMode_ == NextKeyMode::INT) {
266 nextKeyMode_ = NextKeyMode::NONE;
270 return to<int>(doSplitKey<true>());
271 } catch (const std::out_of_range& e) {
272 error("integer key required");
273 return 0; // unreached
279 #endif /* FOLLY_FORMATARG_H_ */