mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-11 05:30:05 +00:00
Use ArgInfo instead of Arg where possible.
This commit is contained in:
parent
5b8126f84d
commit
656a8378d1
18
format.cc
18
format.cc
@ -388,7 +388,7 @@ unsigned fmt::BasicFormatter<Char>::ParseUInt(const Char *&s) const {
|
||||
}
|
||||
|
||||
template <typename Char>
|
||||
inline const typename fmt::BasicFormatter<Char>::Arg
|
||||
inline const typename fmt::BasicFormatter<Char>::ArgInfo
|
||||
&fmt::BasicFormatter<Char>::ParseArgIndex(const Char *&s) {
|
||||
unsigned arg_index = 0;
|
||||
if (*s < '0' || *s > '9') {
|
||||
@ -409,11 +409,11 @@ inline const typename fmt::BasicFormatter<Char>::Arg
|
||||
}
|
||||
if (arg_index >= args_.size())
|
||||
ReportError(s, "argument index is out of range in format");
|
||||
return *args_[arg_index];
|
||||
return args_[arg_index];
|
||||
}
|
||||
|
||||
template <typename Char>
|
||||
void fmt::BasicFormatter<Char>::CheckSign(const Char *&s, const Arg &arg) {
|
||||
void fmt::BasicFormatter<Char>::CheckSign(const Char *&s, const ArgInfo &arg) {
|
||||
char sign = static_cast<char>(*s);
|
||||
if (arg.type > LAST_NUMERIC_TYPE) {
|
||||
ReportError(s,
|
||||
@ -446,7 +446,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
|
||||
num_open_braces_= 1;
|
||||
writer.buffer_.append(start, s - 1);
|
||||
|
||||
const Arg &arg = ParseArgIndex(s);
|
||||
const ArgInfo &arg = ParseArgIndex(s);
|
||||
|
||||
FormatSpec spec;
|
||||
int precision = -1;
|
||||
@ -537,7 +537,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
|
||||
} else if (*s == '{') {
|
||||
++s;
|
||||
++num_open_braces_;
|
||||
const Arg &precision_arg = ParseArgIndex(s);
|
||||
const ArgInfo &precision_arg = ParseArgIndex(s);
|
||||
ULongLong value = 0;
|
||||
switch (precision_arg.type) {
|
||||
case INT:
|
||||
@ -702,11 +702,11 @@ template void fmt::BasicFormatter<char>::ReportError(
|
||||
|
||||
template unsigned fmt::BasicFormatter<char>::ParseUInt(const char *&s) const;
|
||||
|
||||
template const fmt::BasicFormatter<char>::Arg
|
||||
template const fmt::BasicFormatter<char>::ArgInfo
|
||||
&fmt::BasicFormatter<char>::ParseArgIndex(const char *&s);
|
||||
|
||||
template void fmt::BasicFormatter<char>::CheckSign(
|
||||
const char *&s, const Arg &arg);
|
||||
const char *&s, const ArgInfo &arg);
|
||||
|
||||
template void fmt::BasicFormatter<char>::DoFormat();
|
||||
|
||||
@ -732,11 +732,11 @@ template void fmt::BasicFormatter<wchar_t>::ReportError(
|
||||
template unsigned fmt::BasicFormatter<wchar_t>::ParseUInt(
|
||||
const wchar_t *&s) const;
|
||||
|
||||
template const fmt::BasicFormatter<wchar_t>::Arg
|
||||
template const fmt::BasicFormatter<wchar_t>::ArgInfo
|
||||
&fmt::BasicFormatter<wchar_t>::ParseArgIndex(const wchar_t *&s);
|
||||
|
||||
template void fmt::BasicFormatter<wchar_t>::CheckSign(
|
||||
const wchar_t *&s, const Arg &arg);
|
||||
const wchar_t *&s, const ArgInfo &arg);
|
||||
|
||||
template void fmt::BasicFormatter<wchar_t>::DoFormat();
|
||||
|
||||
|
22
format.h
22
format.h
@ -1031,7 +1031,15 @@ class BasicFormatter {
|
||||
};
|
||||
|
||||
// A wrapper around a format argument used to ensure that the formatting
|
||||
// is performed before the argument is destroyed.
|
||||
// is performed before the argument is destroyed. It is private so that
|
||||
// its objects are only created by automatic conversions and not by users.
|
||||
// Example:
|
||||
//
|
||||
// Format("{}") << std::string("test");
|
||||
//
|
||||
// Here an Arg object that wraps a temporary string is automatically
|
||||
// created. It triggers formatting when destroyed which makes sure that
|
||||
// the temporary string is still alive at the time of the formatting.
|
||||
class Arg : public ArgInfo {
|
||||
private:
|
||||
// This method is private to disallow formatting of arbitrary pointers.
|
||||
@ -1118,7 +1126,7 @@ class BasicFormatter {
|
||||
// Format is called here to make sure that a referred object is
|
||||
// still alive, for example:
|
||||
//
|
||||
// Print("{0}") << std::string("test");
|
||||
// Print("{}") << std::string("test");
|
||||
//
|
||||
// Here an Arg object refers to a temporary std::string which is
|
||||
// destroyed at the end of the statement. Since the string object is
|
||||
@ -1132,7 +1140,7 @@ class BasicFormatter {
|
||||
};
|
||||
|
||||
enum { NUM_INLINE_ARGS = 10 };
|
||||
internal::Array<const Arg*, NUM_INLINE_ARGS> args_; // Format arguments.
|
||||
internal::Array<ArgInfo, NUM_INLINE_ARGS> args_; // Format arguments.
|
||||
|
||||
const Char *format_; // Format string.
|
||||
int num_open_braces_;
|
||||
@ -1153,9 +1161,9 @@ class BasicFormatter {
|
||||
unsigned ParseUInt(const Char *&s) const;
|
||||
|
||||
// Parses argument index and returns an argument with this index.
|
||||
const Arg &ParseArgIndex(const Char *&s);
|
||||
const ArgInfo &ParseArgIndex(const Char *&s);
|
||||
|
||||
void CheckSign(const Char *&s, const Arg &arg);
|
||||
void CheckSign(const Char *&s, const ArgInfo &arg);
|
||||
|
||||
// Parses the format string and performs the actual formatting,
|
||||
// writing the output to writer_.
|
||||
@ -1194,7 +1202,7 @@ class BasicFormatter {
|
||||
// TODO: don't copy arguments
|
||||
args_.reserve(args.size());
|
||||
for (const Arg &arg: args)
|
||||
args_.push_back(&arg);
|
||||
args_.push_back(arg);
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -1211,7 +1219,7 @@ class BasicFormatter {
|
||||
// Feeds an argument to a formatter.
|
||||
BasicFormatter &operator<<(const Arg &arg) {
|
||||
arg.formatter = this;
|
||||
args_.push_back(&arg);
|
||||
args_.push_back(arg);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user