Skip to content

Commit

Permalink
Move casts to CharTraits for custom character types
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jun 17, 2015
1 parent 270069b commit 147e5eb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ class BasicArgFormatter : public ArgVisitor<Impl, void> {
if (spec_.align_ == ALIGN_NUMERIC || spec_.flags_ != 0)
FMT_THROW(FormatError("invalid format specifier for char"));
typedef typename BasicWriter<Char>::CharPtr CharPtr;
Char fill = static_cast<Char>(spec_.fill());
Char fill = internal::CharTraits<Char>::cast(spec_.fill());
CharPtr out = CharPtr();
if (spec_.width_ > 1) {
out = writer_.grow_buffer(spec_.width_);
Expand All @@ -455,7 +455,7 @@ class BasicArgFormatter : public ArgVisitor<Impl, void> {
} else {
out = writer_.grow_buffer(1);
}
*out = static_cast<Char>(value);
*out = internal::CharTraits<Char>::cast(value);
}

void visit_string(Arg::StringValue<char> value) {
Expand Down
9 changes: 5 additions & 4 deletions format.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ class BasicCharTraits {
#else
typedef Char *CharPtr;
#endif
static Char cast(wchar_t value) { return static_cast<Char>(value); }
};

template <typename Char>
Expand Down Expand Up @@ -2036,7 +2037,7 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
CharPtr out = CharPtr();
if (spec.width() > size) {
out = grow_buffer(spec.width());
Char fill = static_cast<Char>(spec.fill());
Char fill = internal::CharTraits<Char>::cast(spec.fill());
if (spec.align() == ALIGN_RIGHT) {
std::fill_n(out, spec.width() - size, fill);
out += spec.width() - size;
Expand All @@ -2059,7 +2060,7 @@ typename BasicWriter<Char>::CharPtr
std::size_t content_size, wchar_t fill) {
std::size_t padding = total_size - content_size;
std::size_t left_padding = padding / 2;
Char fill_char = static_cast<Char>(fill);
Char fill_char = internal::CharTraits<Char>::cast(fill);
std::fill_n(buffer, left_padding, fill_char);
buffer += left_padding;
CharPtr content = buffer;
Expand All @@ -2075,7 +2076,7 @@ typename BasicWriter<Char>::CharPtr
const char *prefix, unsigned prefix_size) {
unsigned width = spec.width();
Alignment align = spec.align();
Char fill = static_cast<Char>(spec.fill());
Char fill = internal::CharTraits<Char>::cast(spec.fill());
if (spec.precision() > static_cast<int>(num_digits)) {
// Octal prefix '0' is counted as a digit, so ignore it if precision
// is specified.
Expand Down Expand Up @@ -2314,7 +2315,7 @@ void BasicWriter<Char>::write_double(
*format_ptr = '\0';

// Format using snprintf.
Char fill = static_cast<Char>(spec.fill());
Char fill = internal::CharTraits<Char>::cast(spec.fill());
for (;;) {
std::size_t buffer_size = buffer_.capacity() - offset;
#if _MSC_VER
Expand Down

0 comments on commit 147e5eb

Please sign in to comment.