From bf536ce7b7ff4086a5311c3acbc005632588d706 Mon Sep 17 00:00:00 2001 From: Jonathan Haas Date: Sun, 20 Dec 2015 11:20:01 +0100 Subject: [PATCH 1/2] Add one decimal place to SI velocities --- data/language/english_uk.txt | 2 +- src/localisation/format_codes.h | 1 + src/localisation/localisation.c | 81 +++++++++++++++++++++++++++++++-- src/util/util.c | 6 +-- src/util/util.h | 2 +- 5 files changed, 83 insertions(+), 9 deletions(-) diff --git a/data/language/english_uk.txt b/data/language/english_uk.txt index 8d6275d510..0872b0d03b 100644 --- a/data/language/english_uk.txt +++ b/data/language/english_uk.txt @@ -3921,7 +3921,7 @@ STR_5579 :Window scale factor: STR_5580 :Czech koruna (Kc) STR_5581 :Show FPS STR_5582 :Trap mouse cursor in window -STR_5583 :{COMMA16}ms{POWERNEGATIVEONE} +STR_5583 :{COMMA1DP16}ms{POWERNEGATIVEONE} STR_5584 :SI ##################### diff --git a/src/localisation/format_codes.h b/src/localisation/format_codes.h index 4f312be760..fd7d42c3f0 100644 --- a/src/localisation/format_codes.h +++ b/src/localisation/format_codes.h @@ -134,6 +134,7 @@ enum { FORMAT_SYMBOL_RAILWAY = 20001, FORMAT_SYMBOL_ROAD = 20002, FORMAT_SYMBOL_FLAG = 20003, + FORMAT_COMMA1DP16 = 20004 }; #endif diff --git a/src/localisation/localisation.c b/src/localisation/localisation.c index 5e13882fc2..a5a8993685 100644 --- a/src/localisation/localisation.c +++ b/src/localisation/localisation.c @@ -109,7 +109,8 @@ format_code_token format_code_tokens[] = { { FORMAT_SMALLUP, "SMALLUP" }, { FORMAT_SMALLDOWN, "SMALLDOWN" }, { FORMAT_LEFT, "LEFT" }, - { FORMAT_INVERTEDQUESTION, "INVERTEDQUESTION" } + { FORMAT_INVERTEDQUESTION, "INVERTEDQUESTION" }, + { FORMAT_COMMA1DP16, "COMMA1DP16" } }; uint32 format_get_code(const char *token) @@ -275,6 +276,71 @@ void format_comma_separated_integer(char **dest, long long value) *dest = finish; } +void format_comma_separated_fixed_1dp(char **dest, long long value) +{ + int digit, groupIndex; + char *dst = *dest; + char *finish; + char tmp; + const char *commaMark = language_get_string(5151); + const char *decimalMark = language_get_string(5152); + const char *ch; + + // Negative sign + if (value < 0) { + *dst++ = '-'; + value = -value; + } + + *dest = dst; + + // One decimal place + digit = value % 10; + value /= 10; + *dst++ = '0' + digit; + + ch = decimalMark; + while (*ch != 0) { + *dst++ = *ch++; + } + + if (value == 0) { + *dst++ = '0'; + } else { + // Groups of three digits, right to left + groupIndex = 0; + while (value > 0) { + // Append group separator + if (groupIndex == 3) { + groupIndex = 0; + + ch = commaMark; + while (*ch != 0) { + *dst++ = *ch++; + } + } + + digit = value % 10; + value /= 10; + + *dst++ = '0' + digit; + groupIndex++; + } + } + finish = dst; + + // Reverse string + dst--; + while (*dest < dst) { + tmp = **dest; + **dest = *dst; + *dst = tmp; + (*dest)++; + dst--; + } + *dest = finish; +} + void format_comma_separated_fixed_2dp(char **dest, long long value) { int digit, groupIndex; @@ -461,7 +527,7 @@ void format_velocity(char **dest, uint16 value) stringId = STR_UNIT_SUFFIX_KILOMETRES_PER_HOUR; break; case MEASUREMENT_FORMAT_SI: - value = mph_to_mps(value); + value = mph_to_dmps(value); stringId = STR_UNIT_SUFFIX_METRES_PER_SECOND; break; } @@ -517,7 +583,7 @@ void format_realtime(char **dest, uint16 value) (*dest)--; } -void format_string_code(unsigned char format_code, char **dest, char **args) +void format_string_code(unsigned int format_code, char **dest, char **args) { int value; @@ -543,6 +609,13 @@ void format_string_code(unsigned char format_code, char **dest, char **args) format_comma_separated_fixed_2dp(dest, value); break; + case FORMAT_COMMA1DP16: + // Pop argument + value = *((sint16*)*args); + *args += 2; + + format_comma_separated_fixed_1dp(dest, value); + break; case FORMAT_COMMA16: // Pop argument value = *((sint16*)*args); @@ -676,7 +749,7 @@ void format_string_part_from_raw(utf8 **dest, const utf8 *src, char **args) } } else if (code <= 'z') { *(*dest)++ = code; - } else if (code < 142) { + } else if (code < 142 || code == FORMAT_COMMA1DP16) { format_string_code(code, dest, args); } else { *dest = utf8_write_codepoint(*dest, code); diff --git a/src/util/util.c b/src/util/util.c index 635224fcf1..c0191fc819 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -43,10 +43,10 @@ int mph_to_kmph(int mph) return (mph * 1648) >> 10; } -int mph_to_mps(int mph) +int mph_to_dmps(int mph) { - // 1 mph = 0.44704 m/s - return (mph * 58594) >> 17; + // 1 mph = 4.4704 decimeters/s + return (mph * 73243) >> 14; } bool filename_valid_characters(const utf8 *filename) diff --git a/src/util/util.h b/src/util/util.h index af0be0595b..9dff48bc3d 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -26,7 +26,7 @@ int squaredmetres_to_squaredfeet(int squaredMetres); int metres_to_feet(int metres); int mph_to_kmph(int mph); -int mph_to_mps(int mph); +int mph_to_dmps(int mph); bool filename_valid_characters(const utf8 *filename); From 7149e1a4f550438a599a3e642aadb5d108e0f01d Mon Sep 17 00:00:00 2001 From: Jonathan Haas Date: Sun, 20 Dec 2015 12:28:44 +0100 Subject: [PATCH 2/2] Fix utf8_is_format_code --- src/localisation/localisation.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/localisation/localisation.c b/src/localisation/localisation.c index a5a8993685..4fb0e14b1b 100644 --- a/src/localisation/localisation.c +++ b/src/localisation/localisation.c @@ -136,6 +136,7 @@ bool utf8_is_format_code(int codepoint) if (codepoint < 32) return true; if (codepoint >= FORMAT_ARGUMENT_CODE_START && codepoint <= FORMAT_ARGUMENT_CODE_END) return true; if (codepoint >= FORMAT_COLOUR_CODE_START && codepoint <= FORMAT_COLOUR_CODE_END) return true; + if (codepoint == FORMAT_COMMA1DP16) return true; return false; }