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),
50 precision(kDefaultPrecision),
51 presentation(kDefaultPresentation),
52 nextKeyMode_(NextKeyMode::NONE) {
64 * Validate the argument for the given type; throws on error.
66 void validate(Type type) const;
69 * Throw an exception if the first argument is false. The exception
70 * message will contain the argument string as well as any passed-in
71 * arguments to enforce, formatted using folly::to<std::string>.
73 template <typename... Args>
74 void enforce(bool v, Args&&... args) const {
76 error(std::forward<Args>(args)...);
80 template <typename... Args>
81 std::string errorStr(Args&&... args) const;
82 template <typename... Args>
83 void error(Args&&... args) const FOLLY_NORETURN;
86 * Full argument string, as passed in to the constructor.
88 StringPiece fullArgString;
93 static constexpr char kDefaultFill = '\0';
99 enum class Align : uint8_t {
112 enum class Sign : uint8_t {
122 * Output base prefix (0 for octal, 0x for hex)
127 * Output thousands separator (comma)
129 bool thousandsSeparator;
134 static constexpr int kDefaultWidth = -1;
140 static constexpr int kDefaultPrecision = -1;
146 static constexpr char kDefaultPresentation = '\0';
150 * Split a key component from "key", which must be non-empty (an exception
151 * is thrown otherwise).
153 template <bool emptyOk=false>
154 StringPiece splitKey();
157 * Is the entire key empty?
159 bool keyEmpty() const {
160 return nextKeyMode_ == NextKeyMode::NONE && key_.empty();
164 * Split an key component from "key", which must be non-empty and a valid
165 * integer (an exception is thrown otherwise).
169 void setNextIntKey(int val) {
170 assert(nextKeyMode_ == NextKeyMode::NONE);
171 nextKeyMode_ = NextKeyMode::INT;
175 void setNextKey(StringPiece val) {
176 assert(nextKeyMode_ == NextKeyMode::NONE);
177 nextKeyMode_ = NextKeyMode::STRING;
183 template <bool emptyOk>
184 StringPiece doSplitKey();
188 StringPiece nextKey_;
189 enum class NextKeyMode {
194 NextKeyMode nextKeyMode_;
197 template <typename... Args>
198 inline std::string FormatArg::errorStr(Args&&... args) const {
199 return to<std::string>(
200 "invalid format argument {", fullArgString, "}: ",
201 std::forward<Args>(args)...);
204 template <typename... Args>
205 inline void FormatArg::error(Args&&... args) const {
206 throw BadFormatArg(errorStr(std::forward<Args>(args)...));
209 template <bool emptyOk>
210 inline StringPiece FormatArg::splitKey() {
211 enforce(nextKeyMode_ != NextKeyMode::INT, "integer key expected");
212 return doSplitKey<emptyOk>();
215 template <bool emptyOk>
216 inline StringPiece FormatArg::doSplitKey() {
217 if (nextKeyMode_ == NextKeyMode::STRING) {
218 nextKeyMode_ = NextKeyMode::NONE;
219 if (!emptyOk) { // static
220 enforce(!nextKey_.empty(), "non-empty key required");
226 if (!emptyOk) { // static
227 error("non-empty key required");
229 return StringPiece();
232 const char* b = key_.begin();
233 const char* e = key_.end();
237 p = static_cast<const char*>(memchr(b, '[', e - b));
238 enforce(p, "unmatched ']'");
240 p = static_cast<const char*>(memchr(b, '.', e - b));
243 key_.assign(p + 1, e);
248 if (!emptyOk) { // static
249 enforce(b != p, "non-empty key required");
251 return StringPiece(b, p);
254 inline int FormatArg::splitIntKey() {
255 if (nextKeyMode_ == NextKeyMode::INT) {
256 nextKeyMode_ = NextKeyMode::NONE;
260 return to<int>(doSplitKey<true>());
261 } catch (const std::out_of_range& e) {
262 error("integer key required");
263 return 0; // unreached
269 #endif /* FOLLY_FORMATARG_H_ */