Commit 4d049cf5 authored by Victor Zverovich's avatar Victor Zverovich
parent 75b5eb4b
......@@ -443,17 +443,17 @@ class fmt::internal::ArgFormatter :
CharPtr out = CharPtr();
if (spec_.width_ > 1) {
Char fill = static_cast<Char>(spec_.fill());
out = writer_.GrowBuffer(spec_.width_);
out = writer_.grow_buffer(spec_.width_);
if (spec_.align_ == fmt::ALIGN_RIGHT) {
std::fill_n(out, spec_.width_ - 1, fill);
out += spec_.width_ - 1;
} else if (spec_.align_ == fmt::ALIGN_CENTER) {
out = writer_.FillPadding(out, spec_.width_, 1, fill);
out = writer_.fill_padding(out, spec_.width_, 1, fill);
} else {
std::fill_n(out + 1, spec_.width_ - 1, fill);
}
} else {
out = writer_.GrowBuffer(1);
out = writer_.grow_buffer(1);
}
*out = static_cast<Char>(value);
}
......@@ -489,7 +489,7 @@ void fmt::internal::FormatErrorReporter<Char>::operator()(
// content area.
template <typename Char>
typename fmt::BasicWriter<Char>::CharPtr
fmt::BasicWriter<Char>::FillPadding(CharPtr buffer,
fmt::BasicWriter<Char>::fill_padding(CharPtr buffer,
unsigned total_size, std::size_t content_size, wchar_t fill) {
std::size_t padding = total_size - content_size;
std::size_t left_padding = padding / 2;
......@@ -631,9 +631,9 @@ void fmt::BasicWriter<Char>::write_double(T value, const FormatSpec &spec) {
if (spec.align() == ALIGN_CENTER &&
spec.width() > static_cast<unsigned>(n)) {
unsigned width = spec.width();
CharPtr p = GrowBuffer(width);
CharPtr p = grow_buffer(width);
std::copy(p, p + n, p + (width - n) / 2);
FillPadding(p, spec.width(), n, fill);
fill_padding(p, spec.width(), n, fill);
return;
}
if (spec.fill() != ' ' || sign) {
......@@ -642,7 +642,7 @@ void fmt::BasicWriter<Char>::write_double(T value, const FormatSpec &spec) {
if (sign)
*(start - 1) = sign;
}
GrowBuffer(n);
grow_buffer(n);
return;
}
// If n is negative we ask to increase the capacity by at least 1,
......@@ -672,7 +672,7 @@ void fmt::BasicWriter<Char>::write_str(
template <typename Char>
inline const Arg
&fmt::BasicFormatter<Char>::ParseArgIndex(const Char *&s) {
&fmt::BasicFormatter<Char>::parse_arg_index(const Char *&s) {
unsigned arg_index = 0;
if (*s < '0' || *s > '9') {
if (*s != '}' && *s != ':')
......@@ -696,7 +696,7 @@ inline const Arg
}
template <typename Char>
void fmt::BasicFormatter<Char>::CheckSign(
void fmt::BasicFormatter<Char>::check_sign(
const Char *&s, const Arg &arg) {
char sign = static_cast<char>(*s);
if (arg.type > Arg::LAST_NUMERIC_TYPE) {
......@@ -742,7 +742,7 @@ const Arg &fmt::internal::FormatterBase::handle_arg_index(unsigned arg_index) {
}
template <typename Char>
void fmt::internal::PrintfFormatter<Char>::ParseFlags(
void fmt::internal::PrintfFormatter<Char>::parse_flags(
FormatSpec &spec, const Char *&s) {
for (;;) {
switch (*s++) {
......@@ -769,7 +769,7 @@ void fmt::internal::PrintfFormatter<Char>::ParseFlags(
}
template <typename Char>
unsigned fmt::internal::PrintfFormatter<Char>::ParseHeader(
unsigned fmt::internal::PrintfFormatter<Char>::parse_header(
const Char *&s, FormatSpec &spec) {
unsigned arg_index = UINT_MAX;
Char c = *s;
......@@ -791,7 +791,7 @@ unsigned fmt::internal::PrintfFormatter<Char>::ParseHeader(
}
}
}
ParseFlags(spec, s);
parse_flags(spec, s);
// Parse width.
if (*s >= '0' && *s <= '9') {
spec.width_ = ParseNonnegativeInt(s, error_);
......@@ -803,7 +803,7 @@ unsigned fmt::internal::PrintfFormatter<Char>::ParseHeader(
}
template <typename Char>
void fmt::internal::PrintfFormatter<Char>::Format(
void fmt::internal::PrintfFormatter<Char>::format(
BasicWriter<Char> &writer, BasicStringRef<Char> format,
const ArgList &args) {
const Char *start = format.c_str();
......@@ -835,7 +835,7 @@ void fmt::internal::PrintfFormatter<Char>::Format(
// is OK for both cases.
// Parse argument index, flags and width.
unsigned arg_index = ParseHeader(s, spec);
unsigned arg_index = parse_header(s, spec);
// Parse precision.
if (*s == '.') {
......@@ -906,7 +906,7 @@ void fmt::internal::PrintfFormatter<Char>::Format(
CharPtr out = CharPtr();
if (spec.width_ > 1) {
Char fill = ' ';
out = writer.GrowBuffer(spec.width_);
out = writer.grow_buffer(spec.width_);
if (spec.align_ != ALIGN_LEFT) {
std::fill_n(out, spec.width_ - 1, fill);
out += spec.width_ - 1;
......@@ -914,7 +914,7 @@ void fmt::internal::PrintfFormatter<Char>::Format(
std::fill_n(out + 1, spec.width_ - 1, fill);
}
} else {
out = writer.GrowBuffer(1);
out = writer.grow_buffer(1);
}
*out = static_cast<Char>(arg.int_value);
break;
......@@ -1000,15 +1000,15 @@ const Char *fmt::BasicFormatter<Char>::format(
// Parse sign.
switch (*s) {
case '+':
CheckSign(s, arg);
check_sign(s, arg);
spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
break;
case '-':
CheckSign(s, arg);
check_sign(s, arg);
spec.flags_ |= MINUS_FLAG;
break;
case ' ':
CheckSign(s, arg);
check_sign(s, arg);
spec.flags_ |= SIGN_FLAG;
break;
}
......@@ -1046,7 +1046,7 @@ const Char *fmt::BasicFormatter<Char>::format(
} else if (*s == '{') {
++s;
++report_error_.num_open_braces;
const Arg &precision_arg = ParseArgIndex(s);
const Arg &precision_arg = parse_arg_index(s);
ULongLong value = 0;
switch (precision_arg.type) {
case Arg::INT:
......@@ -1098,7 +1098,7 @@ const Char *fmt::BasicFormatter<Char>::format(
}
template <typename Char>
void fmt::BasicFormatter<Char>::Format(
void fmt::BasicFormatter<Char>::format(
BasicStringRef<Char> format_str, const ArgList &args) {
const Char *s = start_ = format_str.c_str();
args_ = args;
......@@ -1115,20 +1115,20 @@ void fmt::BasicFormatter<Char>::Format(
throw FormatError("unmatched '}' in format");
report_error_.num_open_braces = 1;
write(writer_, start_, s - 1);
Arg arg = ParseArgIndex(s);
Arg arg = parse_arg_index(s);
s = format(s, arg);
}
write(writer_, start_, s);
}
void fmt::ReportSystemError(
void fmt::report_system_error(
int error_code, fmt::StringRef message) FMT_NOEXCEPT(true) {
// FIXME: format_system_error may throw
ReportError(internal::format_system_error, error_code, message);
}
#ifdef _WIN32
void fmt::ReportWinError(
void fmt::report_windows_error(
int error_code, fmt::StringRef message) FMT_NOEXCEPT(true) {
// FIXME: format_windows_error may throw
ReportError(internal::format_windows_error, error_code, message);
......@@ -1170,25 +1170,25 @@ void fmt::printf(StringRef format, const ArgList &args) {
// Explicit instantiations for char.
template fmt::BasicWriter<char>::CharPtr
fmt::BasicWriter<char>::FillPadding(CharPtr buffer,
fmt::BasicWriter<char>::fill_padding(CharPtr buffer,
unsigned total_size, std::size_t content_size, wchar_t fill);
template void fmt::BasicFormatter<char>::Format(
template void fmt::BasicFormatter<char>::format(
BasicStringRef<char> format, const ArgList &args);
template void fmt::internal::PrintfFormatter<char>::Format(
template void fmt::internal::PrintfFormatter<char>::format(
BasicWriter<char> &writer, BasicStringRef<char> format, const ArgList &args);
// Explicit instantiations for wchar_t.
template fmt::BasicWriter<wchar_t>::CharPtr
fmt::BasicWriter<wchar_t>::FillPadding(CharPtr buffer,
fmt::BasicWriter<wchar_t>::fill_padding(CharPtr buffer,
unsigned total_size, std::size_t content_size, wchar_t fill);
template void fmt::BasicFormatter<wchar_t>::Format(
template void fmt::BasicFormatter<wchar_t>::format(
BasicStringRef<wchar_t> format, const ArgList &args);
template void fmt::internal::PrintfFormatter<wchar_t>::Format(
template void fmt::internal::PrintfFormatter<wchar_t>::format(
BasicWriter<wchar_t> &writer, BasicStringRef<wchar_t> format,
const ArgList &args);
......
......@@ -867,14 +867,14 @@ protected:
template <typename Char>
class PrintfFormatter : private FormatterBase {
private:
void ParseFlags(FormatSpec &spec, const Char *&s);
void parse_flags(FormatSpec &spec, const Char *&s);
// Parses argument index, flags and width and returns the parsed
// argument index.
unsigned ParseHeader(const Char *&s, FormatSpec &spec);
unsigned parse_header(const Char *&s, FormatSpec &spec);
public:
void Format(BasicWriter<Char> &writer,
void format(BasicWriter<Char> &writer,
BasicStringRef<Char> format, const ArgList &args);
};
} // namespace internal
......@@ -888,16 +888,16 @@ private:
internal::FormatErrorReporter<Char> report_error_;
// Parses argument index and returns an argument with this index.
const internal::Arg &ParseArgIndex(const Char *&s);
const internal::Arg &parse_arg_index(const Char *&s);
void CheckSign(const Char *&s, const internal::Arg &arg);
void check_sign(const Char *&s, const internal::Arg &arg);
public:
explicit BasicFormatter(BasicWriter<Char> &w) : writer_(w) {}
BasicWriter<Char> &writer() { return writer_; }
void Format(BasicStringRef<Char> format_str, const ArgList &args);
void format(BasicStringRef<Char> format_str, const ArgList &args);
const Char *format(const Char *format_str, const internal::Arg &arg);
};
......@@ -1299,33 +1299,34 @@ class BasicWriter {
typedef typename internal::CharTraits<Char>::CharPtr CharPtr;
#if _SECURE_SCL
static Char *GetBase(CharPtr p) { return p.base(); }
// Returns pointer value.
static Char *get(CharPtr p) { return p.base(); }
#else
static Char *GetBase(Char *p) { return p; }
static Char *get(Char *p) { return p; }
#endif
static CharPtr FillPadding(CharPtr buffer,
static CharPtr fill_padding(CharPtr buffer,
unsigned total_size, std::size_t content_size, wchar_t fill);
// Grows the buffer by n characters and returns a pointer to the newly
// allocated area.
CharPtr GrowBuffer(std::size_t n) {
CharPtr grow_buffer(std::size_t n) {
std::size_t size = buffer_.size();
buffer_.resize(size + n);
return internal::make_ptr(&buffer_[size], n);
}
// Prepare a buffer for integer formatting.
CharPtr PrepareBufferForInt(unsigned num_digits,
CharPtr prepare_int_buffer(unsigned num_digits,
const EmptySpec &, const char *prefix, unsigned prefix_size) {
unsigned size = prefix_size + num_digits;
CharPtr p = GrowBuffer(size);
CharPtr p = grow_buffer(size);
std::copy(prefix, prefix + prefix_size, p);
return p + size - 1;
}
template <typename Spec>
CharPtr PrepareBufferForInt(unsigned num_digits,
CharPtr prepare_int_buffer(unsigned num_digits,
const Spec &spec, const char *prefix, unsigned prefix_size);
// Formats an integer.
......@@ -1432,7 +1433,7 @@ class BasicWriter {
\endrst
*/
void write(BasicStringRef<Char> format, const ArgList &args) {
BasicFormatter<Char>(*this).Format(format, args);
BasicFormatter<Char>(*this).format(format, args);
}
FMT_VARIADIC_VOID(write, fmt::BasicStringRef<Char>)
......@@ -1519,18 +1520,18 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
const StrChar *s, std::size_t size, const AlignSpec &spec) {
CharPtr out = CharPtr();
if (spec.width() > size) {
out = GrowBuffer(spec.width());
out = grow_buffer(spec.width());
Char fill = static_cast<Char>(spec.fill());
if (spec.align() == ALIGN_RIGHT) {
std::fill_n(out, spec.width() - size, fill);
out += spec.width() - size;
} else if (spec.align() == ALIGN_CENTER) {
out = FillPadding(out, spec.width(), size, fill);
out = fill_padding(out, spec.width(), size, fill);
} else {
std::fill_n(out + size, spec.width() - size, fill);
}
} else {
out = GrowBuffer(size);
out = grow_buffer(size);
}
std::copy(s, s + size, out);
return out;
......@@ -1539,7 +1540,7 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
template <typename Char>
template <typename Spec>
typename fmt::BasicWriter<Char>::CharPtr
fmt::BasicWriter<Char>::PrepareBufferForInt(
fmt::BasicWriter<Char>::prepare_int_buffer(
unsigned num_digits, const Spec &spec,
const char *prefix, unsigned prefix_size) {
unsigned width = spec.width();
......@@ -1553,35 +1554,35 @@ typename fmt::BasicWriter<Char>::CharPtr
unsigned number_size = prefix_size + spec.precision();
AlignSpec subspec(number_size, '0', ALIGN_NUMERIC);
if (number_size >= width)
return PrepareBufferForInt(num_digits, subspec, prefix, prefix_size);
return prepare_int_buffer(num_digits, subspec, prefix, prefix_size);
buffer_.reserve(width);
unsigned fill_size = width - number_size;
if (align != ALIGN_LEFT) {
CharPtr p = GrowBuffer(fill_size);
CharPtr p = grow_buffer(fill_size);
std::fill(p, p + fill_size, fill);
}
CharPtr result = PrepareBufferForInt(
CharPtr result = prepare_int_buffer(
num_digits, subspec, prefix, prefix_size);
if (align == ALIGN_LEFT) {
CharPtr p = GrowBuffer(fill_size);
CharPtr p = grow_buffer(fill_size);
std::fill(p, p + fill_size, fill);
}
return result;
}
unsigned size = prefix_size + num_digits;
if (width <= size) {
CharPtr p = GrowBuffer(size);
CharPtr p = grow_buffer(size);
std::copy(prefix, prefix + prefix_size, p);
return p + size - 1;
}
CharPtr p = GrowBuffer(width);
CharPtr p = grow_buffer(width);
CharPtr end = p + width;
if (align == ALIGN_LEFT) {
std::copy(prefix, prefix + prefix_size, p);
p += size;
std::fill(p, end, fill);
} else if (align == ALIGN_CENTER) {
p = FillPadding(p, width, size, fill);
p = fill_padding(p, width, size, fill);
std::copy(prefix, prefix + prefix_size, p);
p += size;
} else {
......@@ -1617,9 +1618,9 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
switch (spec.type()) {
case 0: case 'd': {
unsigned num_digits = internal::count_digits(abs_value);
CharPtr p = PrepareBufferForInt(
CharPtr p = prepare_int_buffer(
num_digits, spec, prefix, prefix_size) + 1 - num_digits;
internal::format_decimal(GetBase(p), abs_value, num_digits);
internal::format_decimal(get(p), abs_value, num_digits);
break;
}
case 'x': case 'X': {
......@@ -1632,7 +1633,7 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
do {
++num_digits;
} while ((n >>= 4) != 0);
Char *p = GetBase(PrepareBufferForInt(
Char *p = get(prepare_int_buffer(
num_digits, spec, prefix, prefix_size));
n = abs_value;
const char *digits = spec.type() == 'x' ?
......@@ -1652,8 +1653,7 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
do {
++num_digits;
} while ((n >>= 1) != 0);
Char *p = GetBase(PrepareBufferForInt(
num_digits, spec, prefix, prefix_size));
Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
n = abs_value;
do {
*p-- = '0' + (n & 1);
......@@ -1668,8 +1668,7 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
do {
++num_digits;
} while ((n >>= 3) != 0);
Char *p = GetBase(PrepareBufferForInt(
num_digits, spec, prefix, prefix_size));
Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
n = abs_value;
do {
*p-- = '0' + (n & 7);
......@@ -1693,7 +1692,7 @@ void format(BasicFormatter<Char> &f, const Char *format_str, const T &value) {
// Reports a system error without throwing an exception.
// Can be used to report errors from destructors.
void ReportSystemError(int error_code, StringRef message) FMT_NOEXCEPT(true);
void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT(true);
#ifdef _WIN32
......@@ -1722,7 +1721,7 @@ class WindowsError : public SystemError {
// Reports a Windows error without throwing an exception.
// Can be used to report errors from destructors.
void ReportWinError(int error_code, StringRef message) FMT_NOEXCEPT(true);
void report_windows_error(int error_code, StringRef message) FMT_NOEXCEPT(true);
#endif
......@@ -1803,7 +1802,7 @@ void print(std::ostream &os, StringRef format, const ArgList &args);
template <typename Char>
void printf(BasicWriter<Char> &w,
BasicStringRef<Char> format, const ArgList &args) {
internal::PrintfFormatter<Char>().Format(w, format, args);
internal::PrintfFormatter<Char>().format(w, format, args);
}
inline std::string sprintf(StringRef format, const ArgList &args) {
......
......@@ -67,7 +67,7 @@ inline std::size_t convert_rwcount(std::size_t count) { return count; }
fmt::BufferedFile::~BufferedFile() FMT_NOEXCEPT(true) {
if (file_ && FMT_SYSTEM(fclose(file_)) != 0)
fmt::ReportSystemError(errno, "cannot close file");
fmt::report_system_error(errno, "cannot close file");
}
void fmt::BufferedFile::close() {
......@@ -102,7 +102,7 @@ fmt::File::~File() FMT_NOEXCEPT(true) {
// Don't retry close in case of EINTR!
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
if (fd_ != -1 && FMT_POSIX_CALL(close(fd_)) != 0)
fmt::ReportSystemError(errno, "cannot close file");
fmt::report_system_error(errno, "cannot close file");
}
void fmt::File::close() {
......
......@@ -448,12 +448,6 @@ TEST(UtilTest, StrError) {
#endif
}
TEST(UtilTest, SystemError) {
fmt::SystemError e(EDOM, "test");
EXPECT_EQ(fmt::format("test: {}", GetSystemErrorMessage(EDOM)), e.what());
EXPECT_EQ(EDOM, e.error_code());
}
typedef void (*FormatErrorMessage)(
fmt::Writer &out, int error_code, StringRef message);
......@@ -478,7 +472,10 @@ TEST(UtilTest, FormatSystemError) {
GetSystemErrorMessage(EDOM)), message.str());
}
TEST(UtilTest, ThrowSystemError) {
TEST(UtilTest, SystemError) {
fmt::SystemError e(EDOM, "test");
EXPECT_EQ(fmt::format("test: {}", GetSystemErrorMessage(EDOM)), e.what());
EXPECT_EQ(EDOM, e.error_code());
CheckThrowError<fmt::SystemError>(EDOM, fmt::internal::format_system_error);
}
......@@ -486,12 +483,12 @@ TEST(UtilTest, ReportSystemError) {
fmt::Writer out;
fmt::internal::format_system_error(out, EDOM, "test error");
out << '\n';
EXPECT_WRITE(stderr, fmt::ReportSystemError(EDOM, "test error"), out.str());
EXPECT_WRITE(stderr, fmt::report_system_error(EDOM, "test error"), out.str());
}
#ifdef _WIN32
TEST(UtilTest, FormatWinErrorMessage) {
TEST(UtilTest, FormatWindowsError) {
LPWSTR message = 0;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
......@@ -506,17 +503,17 @@ TEST(UtilTest, FormatWinErrorMessage) {
actual_message.str());
}
TEST(UtilTest, ThrowWinError) {
TEST(UtilTest, WindowsError) {
CheckThrowError<fmt::WindowsError>(
ERROR_FILE_EXISTS, fmt::internal::format_windows_error);
}
TEST(UtilTest, ReportWinError) {
TEST(UtilTest, ReportWindowsError) {
fmt::Writer out;
fmt::internal::format_windows_error(out, ERROR_FILE_EXISTS, "test error");
out << '\n';
EXPECT_WRITE(stderr,
fmt::ReportWinError(ERROR_FILE_EXISTS, "test error"), out.str());
fmt::report_windows_error(ERROR_FILE_EXISTS, "test error"), out.str());
}
#endif // _WIN32
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment