maths: prefer int for `digits' types

This commit is contained in:
Danny Robson 2017-10-05 12:45:48 +11:00
parent 32f3240186
commit a36991fc83
2 changed files with 8 additions and 6 deletions

View File

@ -60,7 +60,7 @@ namespace util::format::detail {
// //
// other values are theoretically supportable, but do not form part of // other values are theoretically supportable, but do not form part of
// the printf specification. // the printf specification.
unsigned base = 10; int base = 10;
enum class repr { enum class repr {
FIXED, FIXED,
@ -81,7 +81,7 @@ namespace util::format::detail {
OSTREAM OSTREAM
} k; } k;
unsigned width = 0; // field width, ie: how many characters int width = 0; // field width, ie: how many characters
int precision = -1; // how many digits after the decimal int precision = -1; // how many digits after the decimal
size_t length = 0; // bytesize of underlying type size_t length = 0; // bytesize of underlying type
@ -619,7 +619,7 @@ namespace util::format::detail {
const auto len = spec.precision < 0 ? spec.precision : const auto len = spec.precision < 0 ? spec.precision :
(size_t)spec.precision < strlen (t) ? spec.precision : (size_t)spec.precision < strlen (t) ? spec.precision :
strlen (t); (int)strlen (t);
// perform left padding // perform left padding
if (spec.width > len && !spec.left_adjusted) if (spec.width > len && !spec.left_adjusted)

View File

@ -347,15 +347,17 @@ namespace util {
template <typename ValueT, typename BaseT> template <typename ValueT, typename BaseT>
constexpr constexpr
std::enable_if_t< std::enable_if_t<
std::is_integral<ValueT>::value && std::is_unsigned<BaseT>::value, std::is_integral_v<ValueT> && std::is_integral_v<BaseT>,
unsigned int
> >
digits (ValueT value, BaseT base) noexcept digits (ValueT value, BaseT base) noexcept
{ {
assert (base > 0);
if (value < 0) if (value < 0)
value *= -1; value *= -1;
unsigned tally = 1; int tally = 1;
while (value /= base) while (value /= base)
++tally; ++tally;