2 * Copyright 2014 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),
51 precision(kDefaultPrecision),
52 presentation(kDefaultPresentation),
53 nextKeyMode_(NextKeyMode::NONE) {
65 * Validate the argument for the given type; throws on error.
67 void validate(Type type) const;
70 * Throw an exception if the first argument is false. The exception
71 * message will contain the argument string as well as any passed-in
72 * arguments to enforce, formatted using folly::to<std::string>.
74 template <typename... Args>
75 void enforce(bool v, Args&&... args) const {
77 error(std::forward<Args>(args)...);
81 template <typename... Args>
82 std::string errorStr(Args&&... args) const;
83 template <typename... Args>
84 FOLLY_NORETURN void error(Args&&... args) const;
87 * Full argument string, as passed in to the constructor.
89 StringPiece fullArgString;
94 static constexpr char kDefaultFill = '\0';
100 enum class Align : uint8_t {
113 enum class Sign : uint8_t {
123 * Output base prefix (0 for octal, 0x for hex)
128 * Output thousands separator (comma)
130 bool thousandsSeparator;
133 * Force a trailing decimal on doubles which could be rendered as ints
140 static constexpr int kDefaultWidth = -1;
146 static constexpr int kDefaultPrecision = -1;
152 static constexpr char kDefaultPresentation = '\0';
156 * Split a key component from "key", which must be non-empty (an exception
157 * is thrown otherwise).
159 template <bool emptyOk=false>
160 StringPiece splitKey();
163 * Is the entire key empty?
165 bool keyEmpty() const {
166 return nextKeyMode_ == NextKeyMode::NONE && key_.empty();
170 * Split an key component from "key", which must be non-empty and a valid
171 * integer (an exception is thrown otherwise).
175 void setNextIntKey(int val) {
176 assert(nextKeyMode_ == NextKeyMode::NONE);
177 nextKeyMode_ = NextKeyMode::INT;
181 void setNextKey(StringPiece val) {
182 assert(nextKeyMode_ == NextKeyMode::NONE);
183 nextKeyMode_ = NextKeyMode::STRING;
189 template <bool emptyOk>
190 StringPiece doSplitKey();
194 StringPiece nextKey_;
195 enum class NextKeyMode {
200 NextKeyMode nextKeyMode_;
203 template <typename... Args>
204 inline std::string FormatArg::errorStr(Args&&... args) const {
205 return to<std::string>(
206 "invalid format argument {", fullArgString, "}: ",
207 std::forward<Args>(args)...);
210 template <typename... Args>
211 inline void FormatArg::error(Args&&... args) const {
212 throw BadFormatArg(errorStr(std::forward<Args>(args)...));
215 template <bool emptyOk>
216 inline StringPiece FormatArg::splitKey() {
217 enforce(nextKeyMode_ != NextKeyMode::INT, "integer key expected");
218 return doSplitKey<emptyOk>();
221 template <bool emptyOk>
222 inline StringPiece FormatArg::doSplitKey() {
223 if (nextKeyMode_ == NextKeyMode::STRING) {
224 nextKeyMode_ = NextKeyMode::NONE;
225 if (!emptyOk) { // static
226 enforce(!nextKey_.empty(), "non-empty key required");
232 if (!emptyOk) { // static
233 error("non-empty key required");
235 return StringPiece();
238 const char* b = key_.begin();
239 const char* e = key_.end();
243 p = static_cast<const char*>(memchr(b, '[', e - b));
244 enforce(p, "unmatched ']'");
246 p = static_cast<const char*>(memchr(b, '.', e - b));
249 key_.assign(p + 1, e);
254 if (!emptyOk) { // static
255 enforce(b != p, "non-empty key required");
257 return StringPiece(b, p);
260 inline int FormatArg::splitIntKey() {
261 if (nextKeyMode_ == NextKeyMode::INT) {
262 nextKeyMode_ = NextKeyMode::NONE;
266 return to<int>(doSplitKey<true>());
267 } catch (const std::out_of_range& e) {
268 error("integer key required");
269 return 0; // unreached
275 #endif /* FOLLY_FORMATARG_H_ */