Remove all non-double versions of si_string().

This fixes lack-of-significant-digits problems.  Remove default
arguments to si_string() to encourage people to use translatable
strings.
This commit is contained in:
Alexander van Gessel 2011-09-28 18:49:00 +01:00
parent c932e8dd88
commit 62b0f4c120
2 changed files with 7 additions and 71 deletions

View file

@ -357,8 +357,7 @@ std::string signed_value(int val)
return oss.str();
}
template <typename T>
void si_string_impl_stream_write(std::stringstream &ss, T input) {
void si_string_impl_stream_write(std::stringstream &ss, double input) {
#ifdef _MSC_VER
// Visual C++ makes 'precision' set the number of decimal places.
// Other platforms make it set the number of significant figures
@ -370,21 +369,9 @@ void si_string_impl_stream_write(std::stringstream &ss, T input) {
ss << input;
#endif
}
template <> void si_string_impl_stream_write(std::stringstream &ss, int input) {
ss << input;
}
template <> void si_string_impl_stream_write(std::stringstream &ss, unsigned input) {
ss << input;
}
template <> void si_string_impl_stream_write(std::stringstream &ss, long input) {
ss << input;
}
template <> void si_string_impl_stream_write(std::stringstream &ss, long long input) {
ss << input;
}
template <typename T>
std::string si_string_impl2(T input, bool base2, std::string unit) {
const T multiplier = base2 ? 1024 : 1000;
std::string si_string(double input, bool base2, std::string unit) {
const double multiplier = base2 ? 1024 : 1000;
typedef boost::array<std::string, 9> strings9;
@ -439,51 +426,6 @@ std::string si_string_impl2(T input, bool base2, std::string unit) {
<< unit;
return ss.str();
}
// Two layers of indirection, for the specialisation below
template <typename T>
std::string si_string_impl(T input, bool base2, std::string unit) {
// (input == -input) can happen if we're at the minimum of the native signed integer type, so make sure we don't recurse infinitely
// It will still give bad output, but it won't overflow the stack
if (input < 0 && input != -input)
return unicode_minus + si_string(-input, base2, unit);
return si_string_impl2(input, base2, unit);
}
// Specialisation for unsigned types as the template causes warnings otherwise
template <> std::string si_string_impl(unsigned input, bool base2, std::string unit) {
return si_string_impl2(input, base2, unit);
}
template <> std::string si_string_impl(unsigned long input, bool base2, std::string unit) {
return si_string_impl2(input, base2, unit);
}
template <> std::string si_string_impl(unsigned long long input, bool base2, std::string unit) {
return si_string_impl2(input, base2, unit);
}
// Public functions
std::string si_string(double input, bool base2, std::string unit) {
return si_string_impl(input, base2, unit);
}
std::string si_string(int input, bool base2, std::string unit) {
return si_string_impl(input, base2, unit);
}
std::string si_string(unsigned input, bool base2, std::string unit) {
return si_string_impl(input, base2, unit);
}
std::string si_string(long input, bool base2, std::string unit) {
return si_string_impl(input, base2, unit);
}
std::string si_string(unsigned long input, bool base2, std::string unit) {
return si_string_impl(input, base2, unit);
}
std::string si_string(long long input, bool base2, std::string unit) {
return si_string_impl(input, base2, unit);
}
std::string si_string(unsigned long long input, bool base2, std::string unit) {
return si_string_impl(input, base2, unit);
}
std::string si_string(long double input, bool base2, std::string unit) {
return si_string_impl(input, base2, unit);
}
static bool is_username_char(char c) {
return ((c == '_') || (c == '-'));

View file

@ -154,16 +154,10 @@ inline std::string signed_percent(int val) {return signed_value(val) + "%";}
* If the unit is to be translatable,
* a t_string should be passed as the third argument.
* _("unit_byte^B") is suggested as standard.
*
* There are no default values because they would not be translatable.
*/
std::string si_string(double input, bool base2=true, std::string unit="B");
std::string si_string(int input, bool base2=true, std::string unit="B");
std::string si_string(unsigned input, bool base2=true, std::string unit="B");
/* Some extra-long versions */
std::string si_string(long input, bool base2=true, std::string unit="B");
std::string si_string(unsigned long input, bool base2=true, std::string unit="B");
std::string si_string(long long input, bool base2=true, std::string unit="B");
std::string si_string(unsigned long long input, bool base2=true, std::string unit="B");
std::string si_string(long double input, bool base2=true, std::string unit="B");
std::string si_string(double input, bool base2, std::string unit);
/**
* Try to complete the last word of 'text' with the 'wordlist'.