mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-06 14:27:16 +00:00
(svn r18181) -Add: the concept of zero filled numbers to strgen
This commit is contained in:
parent
4987d97810
commit
2f0b1ef5f1
@ -185,29 +185,24 @@ void InjectDParam(uint amount)
|
||||
memmove(_decode_parameters + amount, _decode_parameters, sizeof(_decode_parameters) - amount * sizeof(uint64));
|
||||
}
|
||||
|
||||
static char *FormatNumber(char *buff, int64 number, const char *last, const char *separator)
|
||||
static char *FormatNumber(char *buff, int64 number, const char *last, const char *separator, int zerofill_from = 19)
|
||||
{
|
||||
uint64 divisor = 10000000000000000000ULL;
|
||||
uint64 quot;
|
||||
int i;
|
||||
uint64 tot;
|
||||
uint64 num;
|
||||
|
||||
if (number < 0) {
|
||||
buff += seprintf(buff, last, "-");
|
||||
number = -number;
|
||||
}
|
||||
|
||||
num = number;
|
||||
|
||||
tot = 0;
|
||||
for (i = 0; i < 20; i++) {
|
||||
quot = 0;
|
||||
uint64 num = number;
|
||||
uint64 tot = 0;
|
||||
for (int i = 0; i < 20; i++) {
|
||||
uint64 quot = 0;
|
||||
if (num >= divisor) {
|
||||
quot = num / divisor;
|
||||
num = num % divisor;
|
||||
}
|
||||
if (tot |= quot || i == 19) {
|
||||
if (tot |= quot || i >= zerofill_from) {
|
||||
buff += seprintf(buff, last, "%i", (int)quot);
|
||||
if ((i % 3) == 1 && i != 19) buff = strecpy(buff, separator, last);
|
||||
}
|
||||
@ -232,6 +227,11 @@ static char *FormatNoCommaNumber(char *buff, int64 number, const char *last)
|
||||
return FormatNumber(buff, number, last, "");
|
||||
}
|
||||
|
||||
static char *FormatZerofillNumber(char *buff, int64 number, int64 count, const char *last)
|
||||
{
|
||||
return FormatNumber(buff, number, last, "", 20 - count);
|
||||
}
|
||||
|
||||
static char *FormatHexNumber(char *buff, int64 number, const char *last)
|
||||
{
|
||||
return buff + seprintf(buff, last, "0x%x", (uint32)number);
|
||||
@ -839,6 +839,11 @@ static char *FormatString(char *buff, const char *str, int64 *argv, uint casei,
|
||||
buff = FormatNoCommaNumber(buff, GetInt64(&argv), last);
|
||||
break;
|
||||
|
||||
case SCC_ZEROFILL_NUM: { // {ZEROFILL_NUM}
|
||||
int64 num = GetInt64(&argv);
|
||||
buff = FormatZerofillNumber(buff, num, GetInt64(&argv), last);
|
||||
} break;
|
||||
|
||||
case SCC_HEX: // {HEX}
|
||||
buff = FormatHexNumber(buff, GetInt64(&argv), last);
|
||||
break;
|
||||
|
@ -72,6 +72,7 @@ enum StringControlCode {
|
||||
SCC_STRING,
|
||||
SCC_COMMA,
|
||||
SCC_NUM,
|
||||
SCC_ZEROFILL_NUM,
|
||||
SCC_HEX,
|
||||
SCC_BYTES,
|
||||
|
||||
|
@ -99,6 +99,7 @@ static const CmdStruct _cmd_structs[] = {
|
||||
/* Numbers */
|
||||
{"COMMA", EmitSingleChar, SCC_COMMA, 1, C_NONE}, // Number with comma
|
||||
{"NUM", EmitSingleChar, SCC_NUM, 1, C_NONE}, // Signed number
|
||||
{"ZEROFILL_NUM", EmitSingleChar, SCC_ZEROFILL_NUM, 2, C_NONE}, // Unsigned number with zero fill, e.g. "02". First parameter is number, second minimum length
|
||||
{"BYTES", EmitSingleChar, SCC_BYTES, 1, C_NONE}, // Unsigned number with "bytes", i.e. "1.02 MiB or 123 KiB"
|
||||
|
||||
{"CURRENCY", EmitSingleChar, SCC_CURRENCY, 1, C_NONE},
|
||||
|
Loading…
Reference in New Issue
Block a user