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
 
@@ -368,24 +368,31 @@ void SetDParamStr(size_t n, const std::s
 
/**
 
 * This function is used to "bind" the std::string to a OpenTTD dparam slot.
 
 * Contrary to the other \c SetDParamStr functions, this moves the string into
 
 * the parameter slot.
 
 * @param n slot of the string
 
 * @param str string to bind
 
 */
 
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
 
 * @param zerofill  minimum number of digits to print for the integer part. The number will be filled with zeros at the front if necessary.
 
 * @param fractional_digits number of fractional digits to display after a decimal separator. The decimal separator is inserted
 
 *                          in front of the \a fractional_digits last digit of \a number.
 
 */
 
static void FormatNumber(StringBuilder &builder, int64_t number, const char *separator, int zerofill = 1, int fractional_digits = 0)
 
{
 
@@ -394,27 +401,25 @@ static void FormatNumber(StringBuilder &
 
	zerofill += fractional_digits;
 
	int thousands_offset = (max_digits - fractional_digits - 1) % 3;
 

	
 
	if (number < 0) {
 
		builder += '-';
 
		number = -number;
 
	}
 

	
 
	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;
 
		}
 
		if ((tot |= quot) || i >= max_digits - zerofill) {
 
			builder += '0' + quot; // quot is a single digit
 
			if ((i % 3) == thousands_offset && i < max_digits - 1 - fractional_digits) builder += separator;
 
		}
 

	
 
@@ -452,34 +457,31 @@ static void FormatHexNumber(StringBuilde
 
static void FormatBytes(StringBuilder &builder, int64_t number)
 
{
 
	assert(number >= 0);
 

	
 
	/*                                   1   2^10  2^20  2^30  2^40  2^50  2^60 */
 
	const char * const iec_prefixes[] = {"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei"};
 
	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));
 
	fmt::format_to(builder, NBSP "{}B", iec_prefixes[id]);
 
}
 

	
 
static void FormatYmdString(StringBuilder &builder, TimerGameCalendar::Date date, uint case_index)
 
{
 
	TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(date);
0 comments (0 inline, 0 general)