| 1688 | template <typename Char> |
| 1689 | template <typename Spec> |
| 1690 | typename BasicWriter<Char>::CharPtr |
| 1691 | BasicWriter<Char>::prepare_int_buffer( |
| 1692 | unsigned num_digits, const Spec &spec, |
| 1693 | const char *prefix, unsigned prefix_size) { |
| 1694 | unsigned width = spec.width(); |
| 1695 | Alignment align = spec.align(); |
| 1696 | Char fill = static_cast<Char>(spec.fill()); |
| 1697 | if (spec.precision() > static_cast<int>(num_digits)) { |
| 1698 | // Octal prefix '0' is counted as a digit, so ignore it if precision |
| 1699 | // is specified. |
| 1700 | if (prefix_size > 0 && prefix[prefix_size - 1] == '0') |
| 1701 | --prefix_size; |
| 1702 | unsigned number_size = prefix_size + spec.precision(); |
| 1703 | AlignSpec subspec(number_size, '0', ALIGN_NUMERIC); |
| 1704 | if (number_size >= width) |
| 1705 | return prepare_int_buffer(num_digits, subspec, prefix, prefix_size); |
| 1706 | buffer_.reserve(width); |
| 1707 | unsigned fill_size = width - number_size; |
| 1708 | if (align != ALIGN_LEFT) { |
| 1709 | CharPtr p = grow_buffer(fill_size); |
| 1710 | std::fill(p, p + fill_size, fill); |
| 1711 | } |
| 1712 | CharPtr result = prepare_int_buffer( |
| 1713 | num_digits, subspec, prefix, prefix_size); |
| 1714 | if (align == ALIGN_LEFT) { |
| 1715 | CharPtr p = grow_buffer(fill_size); |
| 1716 | std::fill(p, p + fill_size, fill); |
| 1717 | } |
| 1718 | return result; |
| 1719 | } |
| 1720 | unsigned size = prefix_size + num_digits; |
| 1721 | if (width <= size) { |
| 1722 | CharPtr p = grow_buffer(size); |
| 1723 | std::copy(prefix, prefix + prefix_size, p); |
| 1724 | return p + size - 1; |
| 1725 | } |
| 1726 | CharPtr p = grow_buffer(width); |
| 1727 | CharPtr end = p + width; |
| 1728 | if (align == ALIGN_LEFT) { |
| 1729 | std::copy(prefix, prefix + prefix_size, p); |
| 1730 | p += size; |
| 1731 | std::fill(p, end, fill); |
| 1732 | } else if (align == ALIGN_CENTER) { |
| 1733 | p = fill_padding(p, width, size, fill); |
| 1734 | std::copy(prefix, prefix + prefix_size, p); |
| 1735 | p += size; |
| 1736 | } else { |
| 1737 | if (align == ALIGN_NUMERIC) { |
| 1738 | if (prefix_size != 0) { |
| 1739 | p = std::copy(prefix, prefix + prefix_size, p); |
| 1740 | size -= prefix_size; |
| 1741 | } |
| 1742 | } else { |
| 1743 | std::copy(prefix, prefix + prefix_size, end - size); |
| 1744 | } |
| 1745 | std::fill(p, end - size, fill); |
| 1746 | p = end; |
| 1747 | } |