From 1089f02e155f874990470ef9848b83a7d45ab270 Mon Sep 17 00:00:00 2001 From: Hielke Morsink <123mannetje@gmail.com> Date: Sun, 22 May 2016 21:36:08 +0200 Subject: [PATCH] Moved logically string compare function to utils --- src/peep/peep.c | 27 +-------------------------- src/util/util.c | 31 +++++++++++++++++++++++++++++++ src/util/util.h | 1 + 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/peep/peep.c b/src/peep/peep.c index 167dc502fd..5ad7775b7a 100644 --- a/src/peep/peep.c +++ b/src/peep/peep.c @@ -10306,31 +10306,6 @@ static void peep_give_real_name(rct_peep *peep) peep->name_string_idx = dx; } -static int peep_compare_name(utf8 const *peep_a_name, utf8 const *peep_b_name) -{ - for (;; peep_a_name++, peep_b_name++) { - int result = tolower(*peep_a_name) - tolower(*peep_b_name); - bool both_numeric = *peep_a_name >= '0' && *peep_a_name <= '9' && *peep_b_name >= '0' && *peep_b_name <= '9'; - if (result != 0 || !*peep_a_name || both_numeric) { // difference found || end of string - if (both_numeric) { // a and b both start with a number - // Get the numbers in the string at current positions - int na = 0 , nb = 0; - for (; *peep_a_name >= '0' && *peep_a_name <= '9'; peep_a_name++) { na *= 10; na += *peep_a_name - '0'; } - for (; *peep_b_name >= '0' && *peep_b_name <= '9'; peep_b_name++) { nb *= 10; nb += *peep_b_name - '0'; } - // In case the numbers are the same - if (na == nb) - continue; - return na - nb; - } - else { - return result; - } - } - } - - assert(false); -} - static int peep_compare(uint16 const *sprite_index_a, uint16 const *sprite_index_b) { rct_peep const *peep_a = GET_PEEP(*sprite_index_a); @@ -10366,7 +10341,7 @@ static int peep_compare(uint16 const *sprite_index_a, uint16 const *sprite_index format_string(name_a, peep_a->name_string_idx, &peepIndex); peepIndex = peep_b->id; format_string(name_b, peep_b->name_string_idx, &peepIndex); - return peep_compare_name(name_a, name_b); + return strlogicalcmp(name_a, name_b); } /** diff --git a/src/util/util.c b/src/util/util.c index c43cdb9760..4cf82c7d7e 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -202,6 +202,37 @@ int strcicmp(char const *a, char const *b) } } +/* Case insensitive logical compare */ +// Example: +// - Guest 10 +// - Guest 99 +// - Guest 100 +// - John v2.0 +// - John v2.1 +int strlogicalcmp(char const *a, char const *b) { + for (;; a++, b++) { + int result = tolower(*a) - tolower(*b); + bool both_numeric = *a >= '0' && *a <= '9' && *b >= '0' && *b <= '9'; + if (result != 0 || !*a || both_numeric) { // difference found || end of string + if (both_numeric) { // a and b both start with a number + // Get the numbers in the string at current positions + int na = 0 , nb = 0; + for (; *a >= '0' && *a <= '9'; a++) { na *= 10; na += *a - '0'; } + for (; *b >= '0' && *b <= '9'; b++) { nb *= 10; nb += *b - '0'; } + // In case the numbers are the same + if (na == nb) + continue; + return na - nb; + } + else { + return result; + } + } + } + + assert(false); +} + utf8 * safe_strtrunc(utf8 * text, size_t size) { assert(text != NULL); diff --git a/src/util/util.h b/src/util/util.h index c22c0663fb..d33e4e4ddd 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -39,6 +39,7 @@ int bitscanforward(int source); int bitcount(int source); bool strequals(const char *a, const char *b, int length, bool caseInsensitive); int strcicmp(char const *a, char const *b); +int strlogicalcmp(char const *a, char const *b); utf8 * safe_strtrunc(utf8 * text, size_t size); char *safe_strcpy(char * destination, const char * source, size_t num); char *safe_strcat(char *destination, const char *source, size_t size);