From 75d076b3bccc9dab9e355cd4ce4fe45125c2944f Mon Sep 17 00:00:00 2001 From: KillaBilla Date: Sun, 8 Sep 2013 13:09:41 +0200 Subject: [PATCH] changed str_clean_whitespace implementation to pkoerner's one (cherry picked from commit https://github.com/teeworlds/teeworlds/commit/a9cc1e8de2aca298b68342c509373a539c84c3cc) --- src/base/system.c | 51 ++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/src/base/system.c b/src/base/system.c index fa436991b..7d1ad6019 100644 --- a/src/base/system.c +++ b/src/base/system.c @@ -2423,37 +2423,30 @@ void str_sanitize_filename(char *str_in) /* removes leading and trailing spaces and limits the use of multiple spaces */ void str_clean_whitespaces(char *str_in) { - int Len = strlen(str_in); - int FirstIndex; - int LastIndex; - int SpaceStart = -1; - int i; - - // remove leading and trailing spaces - for(FirstIndex = 0; FirstIndex < Len; FirstIndex++) - if(str_in[FirstIndex] != ' ') - break; - - for(LastIndex = Len - 1; LastIndex > FirstIndex; LastIndex--) - if(str_in[LastIndex] != ' ') - break; - - str_copy(str_in, str_in + FirstIndex, LastIndex - FirstIndex + 2); - - // remove multiple spaces - Len = strlen(str_in); - - for(i = 0; i < Len; i++) + char* r = str_in; + int c = 0; + + while(*r == ' ') + ++r; + + while(*str_in) { - if(str_in[i] == ' ' && SpaceStart == -1) - SpaceStart = i; - else if(str_in[i] != ' ' && SpaceStart != -1) + for(; *r == ' '; ++r) + c = 1; + + if(*r) { - str_copy(str_in + SpaceStart + 1, str_in + i, Len - i + 1); - i = SpaceStart + 1; - SpaceStart = -1; - } - } + if(c) + { + *str_in++ = ' '; + c = 0; + } + + *str_in++ = *r++; + } + else + *str_in = 0; + } } char *str_skip_to_whitespace(char *str)