Implement runtime precision.
This commit is contained in:
parent
db306cfb22
commit
363352754c
117
format.cc
117
format.cc
@ -36,6 +36,7 @@
|
||||
#include <algorithm>
|
||||
|
||||
using std::size_t;
|
||||
using fmt::Formatter;
|
||||
|
||||
#if _MSC_VER
|
||||
# define snprintf _snprintf
|
||||
@ -46,21 +47,6 @@ namespace {
|
||||
// Flags.
|
||||
enum { PLUS_FLAG = 1, ZERO_FLAG = 2, HEX_PREFIX_FLAG = 4 };
|
||||
|
||||
// Throws Exception(message) if format contains '}', otherwise throws
|
||||
// FormatError reporting unmatched '{'. The idea is that unmatched '{'
|
||||
// should override other errors.
|
||||
void ReportError(const char *s, const std::string &message) {
|
||||
for (int num_open_braces = 1; *s; ++s) {
|
||||
if (*s == '{') {
|
||||
++num_open_braces;
|
||||
} else if (*s == '}') {
|
||||
if (--num_open_braces == 0)
|
||||
throw fmt::FormatError(message);
|
||||
}
|
||||
}
|
||||
throw fmt::FormatError("unmatched '{' in format");
|
||||
}
|
||||
|
||||
void ReportUnknownType(char code, const char *type) {
|
||||
if (std::isprint(code)) {
|
||||
throw fmt::FormatError(
|
||||
@ -71,20 +57,6 @@ void ReportUnknownType(char code, const char *type) {
|
||||
<< static_cast<unsigned>(code) << type));
|
||||
}
|
||||
|
||||
// Parses an unsigned integer advancing s to the end of the parsed input.
|
||||
// This function assumes that the first character of s is a digit.
|
||||
unsigned ParseUInt(const char *&s) {
|
||||
assert('0' <= *s && *s <= '9');
|
||||
unsigned value = 0;
|
||||
do {
|
||||
unsigned new_value = value * 10 + (*s++ - '0');
|
||||
if (new_value < value) // Check if value wrapped around.
|
||||
ReportError(s, "number is too big in format");
|
||||
value = new_value;
|
||||
} while ('0' <= *s && *s <= '9');
|
||||
return value;
|
||||
}
|
||||
|
||||
// Information about an integer type.
|
||||
template <typename T>
|
||||
struct IntTraits {
|
||||
@ -111,8 +83,23 @@ template <>
|
||||
struct IsLongDouble<long double> { enum {VALUE = 1}; };
|
||||
}
|
||||
|
||||
// Throws Exception(message) if format contains '}', otherwise throws
|
||||
// FormatError reporting unmatched '{'. The idea is that unmatched '{'
|
||||
// should override other errors.
|
||||
void Formatter::ReportError(const char *s, const std::string &message) const {
|
||||
for (int num_open_braces = num_open_braces_; *s; ++s) {
|
||||
if (*s == '{') {
|
||||
++num_open_braces;
|
||||
} else if (*s == '}') {
|
||||
if (--num_open_braces == 0)
|
||||
throw fmt::FormatError(message);
|
||||
}
|
||||
}
|
||||
throw fmt::FormatError("unmatched '{' in format");
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void fmt::Formatter::FormatInt(T value, unsigned flags, int width, char type) {
|
||||
void Formatter::FormatInt(T value, unsigned flags, int width, char type) {
|
||||
int size = 0;
|
||||
char sign = 0;
|
||||
typedef typename IntTraits<T>::UnsignedType UnsignedType;
|
||||
@ -189,7 +176,7 @@ void fmt::Formatter::FormatInt(T value, unsigned flags, int width, char type) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void fmt::Formatter::FormatDouble(
|
||||
void Formatter::FormatDouble(
|
||||
T value, unsigned flags, int width, int precision, char type) {
|
||||
// Check type.
|
||||
switch (type) {
|
||||
@ -199,7 +186,10 @@ void fmt::Formatter::FormatDouble(
|
||||
case 'e': case 'E': case 'f': case 'g': case 'G':
|
||||
break;
|
||||
case 'F':
|
||||
#ifdef _MSC_VER
|
||||
// MSVC's printf doesn't support 'F'.
|
||||
type = 'f';
|
||||
#endif
|
||||
break;
|
||||
default:
|
||||
ReportUnknownType(type, "double");
|
||||
@ -248,7 +238,30 @@ void fmt::Formatter::FormatDouble(
|
||||
}
|
||||
}
|
||||
|
||||
void fmt::Formatter::DoFormat() {
|
||||
// Parses an unsigned integer advancing s to the end of the parsed input.
|
||||
// This function assumes that the first character of s is a digit.
|
||||
unsigned Formatter::ParseUInt(const char *&s) const {
|
||||
assert('0' <= *s && *s <= '9');
|
||||
unsigned value = 0;
|
||||
do {
|
||||
unsigned new_value = value * 10 + (*s++ - '0');
|
||||
if (new_value < value) // Check if value wrapped around.
|
||||
ReportError(s, "number is too big in format");
|
||||
value = new_value;
|
||||
} while ('0' <= *s && *s <= '9');
|
||||
return value;
|
||||
}
|
||||
|
||||
const Formatter::Arg &Formatter::ParseArgIndex(const char *&s) const {
|
||||
if (*s < '0' || *s > '9')
|
||||
ReportError(s, "missing argument index in format string");
|
||||
unsigned arg_index = ParseUInt(s);
|
||||
if (arg_index >= args_.size())
|
||||
ReportError(s, "argument index is out of range in format");
|
||||
return *args_[arg_index];
|
||||
}
|
||||
|
||||
void Formatter::DoFormat() {
|
||||
const char *start = format_;
|
||||
format_ = 0;
|
||||
const char *s = start;
|
||||
@ -262,15 +275,10 @@ void fmt::Formatter::DoFormat() {
|
||||
}
|
||||
if (c == '}')
|
||||
throw FormatError("unmatched '}' in format");
|
||||
num_open_braces_= 1;
|
||||
buffer_.append(start, s - 1);
|
||||
|
||||
// Parse argument index.
|
||||
if (*s < '0' || *s > '9')
|
||||
ReportError(s, "missing argument index in format string");
|
||||
unsigned arg_index = ParseUInt(s);
|
||||
if (arg_index >= args_.size())
|
||||
ReportError(s, "argument index is out of range in format");
|
||||
const Arg &arg = *args_[arg_index];
|
||||
const Arg &arg = ParseArgIndex(s);
|
||||
|
||||
unsigned flags = 0;
|
||||
int width = 0;
|
||||
@ -312,6 +320,37 @@ void fmt::Formatter::DoFormat() {
|
||||
if (value > INT_MAX)
|
||||
ReportError(s, "number is too big in format");
|
||||
precision = value;
|
||||
} else if (*s == '{') {
|
||||
++s;
|
||||
++num_open_braces_;
|
||||
const Arg &precision_arg = ParseArgIndex(s);
|
||||
unsigned long value = 0;
|
||||
switch (precision_arg.type) {
|
||||
case INT:
|
||||
if (precision_arg.int_value < 0)
|
||||
ReportError(s, "negative precision in format");
|
||||
value = precision_arg.int_value;
|
||||
break;
|
||||
case UINT:
|
||||
value = precision_arg.uint_value;
|
||||
break;
|
||||
case LONG:
|
||||
if (precision_arg.long_value < 0)
|
||||
ReportError(s, "negative precision in format");
|
||||
value = precision_arg.long_value;
|
||||
break;
|
||||
case ULONG:
|
||||
value = precision_arg.ulong_value;
|
||||
break;
|
||||
default:
|
||||
ReportError(s, "precision is not integer");
|
||||
}
|
||||
if (value > INT_MAX)
|
||||
ReportError(s, "number is too big in format");
|
||||
precision = value;
|
||||
if (*s++ != '}')
|
||||
throw FormatError("unmatched '{' in format");
|
||||
--num_open_braces_;
|
||||
} else {
|
||||
ReportError(s, "missing precision in format");
|
||||
}
|
||||
|
8
format.h
8
format.h
@ -244,6 +244,7 @@ class Formatter {
|
||||
internal::Array<const Arg*, NUM_INLINE_ARGS> args_; // Format arguments.
|
||||
|
||||
const char *format_; // Format string.
|
||||
int num_open_braces_;
|
||||
|
||||
friend class internal::ArgInserter;
|
||||
|
||||
@ -251,6 +252,8 @@ class Formatter {
|
||||
args_.push_back(&arg);
|
||||
}
|
||||
|
||||
void ReportError(const char *s, const std::string &message) const;
|
||||
|
||||
// Formats an integer.
|
||||
template <typename T>
|
||||
void FormatInt(T value, unsigned flags, int width, char type);
|
||||
@ -264,6 +267,11 @@ class Formatter {
|
||||
template <typename T>
|
||||
void FormatCustomArg(const void *arg, int width);
|
||||
|
||||
unsigned ParseUInt(const char *&s) const;
|
||||
|
||||
// Parses argument index and returns an argument with this index.
|
||||
const Arg &ParseArgIndex(const char *&s) const;
|
||||
|
||||
void DoFormat();
|
||||
|
||||
void Format() {
|
||||
|
102
format_test.cc
102
format_test.cc
@ -45,13 +45,13 @@ using fmt::FormatError;
|
||||
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
||||
if (::testing::internal::ConstCharPtr gtest_msg = "") { \
|
||||
bool gtest_caught_expected = false; \
|
||||
std::string actual_message; \
|
||||
try { \
|
||||
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
|
||||
} \
|
||||
catch (expected_exception const& e) { \
|
||||
gtest_caught_expected = true; \
|
||||
actual_message = e.what(); \
|
||||
if (std::strcmp(message, e.what()) != 0) \
|
||||
throw; \
|
||||
} \
|
||||
catch (...) { \
|
||||
gtest_msg.value = \
|
||||
@ -64,11 +64,6 @@ using fmt::FormatError;
|
||||
"Expected: " #statement " throws an exception of type " \
|
||||
#expected_exception ".\n Actual: it throws nothing."; \
|
||||
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
||||
} else if (actual_message != message) {\
|
||||
gtest_msg.value = \
|
||||
"Expected: " #statement " throws an exception of type " \
|
||||
#expected_exception "."; \
|
||||
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
||||
} \
|
||||
} else \
|
||||
GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
|
||||
@ -380,6 +375,99 @@ TEST(FormatterTest, Precision) {
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
}
|
||||
|
||||
TEST(FormatterTest, RuntimePrecision) {
|
||||
char format[256];
|
||||
if (ULONG_MAX > UINT_MAX) {
|
||||
std::sprintf(format, "{0:.{%lu", INT_MAX + 1l);
|
||||
EXPECT_THROW_MSG(Format(format) << 0,
|
||||
FormatError, "unmatched '{' in format");
|
||||
std::sprintf(format, "{0:.{%lu}", INT_MAX + 1l);
|
||||
EXPECT_THROW_MSG(Format(format) << 0,
|
||||
FormatError, "unmatched '{' in format");
|
||||
std::sprintf(format, "{0:.{%lu}}", UINT_MAX + 1l);
|
||||
EXPECT_THROW_MSG(Format(format) << 0,
|
||||
FormatError, "number is too big in format");
|
||||
} else {
|
||||
std::sprintf(format, "{0:.{%u0", UINT_MAX);
|
||||
EXPECT_THROW_MSG(Format(format) << 0,
|
||||
FormatError, "unmatched '{' in format");
|
||||
std::sprintf(format, "{0:.{%u0}", UINT_MAX);
|
||||
EXPECT_THROW_MSG(Format(format) << 0,
|
||||
FormatError, "unmatched '{' in format");
|
||||
std::sprintf(format, "{0:.{%u0}}", UINT_MAX);
|
||||
EXPECT_THROW_MSG(Format(format) << 0,
|
||||
FormatError, "number is too big in format");
|
||||
}
|
||||
|
||||
EXPECT_THROW_MSG(Format("{0:.{") << 0,
|
||||
FormatError, "unmatched '{' in format");
|
||||
EXPECT_THROW_MSG(Format("{0:.{}") << 0,
|
||||
FormatError, "unmatched '{' in format");
|
||||
EXPECT_THROW_MSG(Format("{0:.{}}") << 0,
|
||||
FormatError, "missing argument index in format string");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}") << 0 << 0,
|
||||
FormatError, "unmatched '{' in format");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << 0,
|
||||
FormatError, "argument index is out of range in format");
|
||||
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << 0 << -1,
|
||||
FormatError, "negative precision in format");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << 0 << (INT_MAX + 1u),
|
||||
FormatError, "number is too big in format");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << 0 << -1l,
|
||||
FormatError, "negative precision in format");
|
||||
if (sizeof(long) > sizeof(int)) {
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << 0 << (INT_MAX + 1l),
|
||||
FormatError, "number is too big in format");
|
||||
}
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << 0 << (INT_MAX + 1ul),
|
||||
FormatError, "number is too big in format");
|
||||
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << 0 << '0',
|
||||
FormatError, "precision is not integer");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << 0 << 0.0,
|
||||
FormatError, "precision is not integer");
|
||||
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << 42 << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}f}") << 42 << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << 42u << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}f}") << 42u << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << 42l << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}f}") << 42l << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << 42ul << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}f}") << 42ul << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
EXPECT_EQ("1.2", str(Format("{0:.{1}}") << 1.2345 << 2));
|
||||
EXPECT_EQ("1.2", str(Format("{1:.{0}}") << 2 << 1.2345l));
|
||||
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << reinterpret_cast<void*>(0xcafe) << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}f}") << reinterpret_cast<void*>(0xcafe) << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << 'x' << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}f}") << 'x' << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << "str" << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}f}") << "str" << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}}") << TestString() << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
EXPECT_THROW_MSG(Format("{0:.{1}f}") << TestString() << 2,
|
||||
FormatError, "precision specifier requires floating-point argument");
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void CheckUnknownTypes(
|
||||
const T &value, const char *types, const char *type_name) {
|
||||
|
Loading…
Reference in New Issue
Block a user