Changeset - r28767:a4db692758d3
[Not reviewed]
master
0 1 0
Rubidium - 10 months ago 2024-02-08 19:37:55
rubidium@openttd.org
Codechange: Move determining the decimal separator to a separate function
1 file changed with 10 insertions and 8 deletions:
0 comments (0 inline, 0 general)
src/strings.cpp
Show inline comments
 
@@ -374,12 +374,19 @@ void SetDParamStr(size_t n, const std::s
 
 */
 
void SetDParamStr(size_t n, std::string &&str)
 
{
 
	_global_string_params.SetParam(n, std::move(str));
 
}
 

	
 
static const char *GetDecimalSeparator()
 
{
 
	const char *decimal_separator = _settings_game.locale.digit_decimal_separator.c_str();
 
	if (StrEmpty(decimal_separator)) decimal_separator = _langpack.langpack->digit_decimal_separator;
 
	return decimal_separator;
 
}
 

	
 
/**
 
 * Format a number into a string.
 
 * @param builder   the string builder to write to
 
 * @param number    the number to write down
 
 * @param last      the last element in the buffer
 
 * @param separator the thousands-separator to use
 
@@ -400,15 +407,13 @@ static void FormatNumber(StringBuilder &
 
	}
 

	
 
	uint64_t num = number;
 
	uint64_t tot = 0;
 
	for (int i = 0; i < max_digits; i++) {
 
		if (i == max_digits - fractional_digits) {
 
			const char *decimal_separator = _settings_game.locale.digit_decimal_separator.c_str();
 
			if (StrEmpty(decimal_separator)) decimal_separator = _langpack.langpack->digit_decimal_separator;
 
			builder += decimal_separator;
 
			builder += GetDecimalSeparator();
 
		}
 

	
 
		uint64_t quot = 0;
 
		if (num >= divisor) {
 
			quot = num / divisor;
 
			num = num % divisor;
 
@@ -458,22 +463,19 @@ static void FormatBytes(StringBuilder &b
 
	uint id = 1;
 
	while (number >= 1024 * 1024) {
 
		number /= 1024;
 
		id++;
 
	}
 

	
 
	const char *decimal_separator = _settings_game.locale.digit_decimal_separator.c_str();
 
	if (StrEmpty(decimal_separator)) decimal_separator = _langpack.langpack->digit_decimal_separator;
 

	
 
	if (number < 1024) {
 
		id = 0;
 
		fmt::format_to(builder, "{}", number);
 
	} else if (number < 1024 * 10) {
 
		fmt::format_to(builder, "{}{}{:02}", number / 1024, decimal_separator, (number % 1024) * 100 / 1024);
 
		fmt::format_to(builder, "{}{}{:02}", number / 1024, GetDecimalSeparator(), (number % 1024) * 100 / 1024);
 
	} else if (number < 1024 * 100) {
 
		fmt::format_to(builder, "{}{}{:01}", number / 1024, decimal_separator, (number % 1024) * 10 / 1024);
 
		fmt::format_to(builder, "{}{}{:01}", number / 1024, GetDecimalSeparator(), (number % 1024) * 10 / 1024);
 
	} else {
 
		assert(number < 1024 * 1024);
 
		fmt::format_to(builder, "{}", number / 1024);
 
	}
 

	
 
	assert(id < lengthof(iec_prefixes));
0 comments (0 inline, 0 general)