diff options
author | FlavioJS <FlavioJS@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2007-04-09 20:21:17 +0000 |
---|---|---|
committer | FlavioJS <FlavioJS@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2007-04-09 20:21:17 +0000 |
commit | e0a18f0853dd7a0cf0533ae0d10da9d19b116767 (patch) | |
tree | 2f0adc6661ce4597106033a18e33d8e774a76459 /src/common/strlib.c | |
parent | faf6d705ec5b1f8a95a06d31635704adefcca99b (diff) | |
download | hercules-e0a18f0853dd7a0cf0533ae0d10da9d19b116767.tar.gz hercules-e0a18f0853dd7a0cf0533ae0d10da9d19b116767.tar.bz2 hercules-e0a18f0853dd7a0cf0533ae0d10da9d19b116767.tar.xz hercules-e0a18f0853dd7a0cf0533ae0d10da9d19b116767.zip |
* Recoded and renamed the trim function in strlib to normalize_name. (didn't behave like a standard trim function, see function comment for what it does)
* Added a proper trim function to strlib.
* Other minor cleanups.
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@10199 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/common/strlib.c')
-rw-r--r-- | src/common/strlib.c | 75 |
1 files changed, 59 insertions, 16 deletions
diff --git a/src/common/strlib.c b/src/common/strlib.c index bd49e7b61..9cac2ca4f 100644 --- a/src/common/strlib.c +++ b/src/common/strlib.c @@ -123,27 +123,70 @@ int remove_control_chars(char* str) return change; } -//Trims a string, also removes illegal characters such as \t and reduces continous spaces to a single one. by [Foruken] -char* trim(char* str, const char* delim) +// Removes characters identified by ISSPACE from the start and end of the string +// NOTE: make sure the string is not const!! +char* trim(char* str) { - char* strp = strtok(str,delim); - char buf[1024]; - char* bufp = buf; - memset(buf,0,sizeof buf); - - while(strp) { - strcpy(bufp, strp); - bufp = bufp + strlen(strp); - strp = strtok(NULL, delim); - if (strp) { - strcpy(bufp," "); - bufp++; - } + size_t start; + size_t end; + + if( str == NULL ) + return str; + + // get start position + for( start = 0; str[start] && ISSPACE(str[start]); ++start ) + ; + // get end position + for( end = strlen(str); start < end && str[end-1] && ISSPACE(str[end-1]); --end ) + ; + // trim + if( start == end ) + *str = '\0';// empty string + else + {// move string with nul terminator + str[end] = '\0'; + memmove(str,str+start,end-start+1); } - strcpy(str,buf); return str; } +// Converts one or more consecutive occurences of the delimiters into a single space +// and removes such occurences from the beginning and end of string +// NOTE: make sure the string is not const!! +char* normalize_name(char* str,const char* delims) +{ + char* in = str; + char* out = str; + int put_space = 0; + + if( str == NULL || delims == NULL ) + return str; + + // trim start of string + while( *in && strchr(delims,*in) ) + ++in; + while( *in ) + { + if( put_space ) + {// replace trim characters with a single space + *out = ' '; + ++out; + } + // copy non trim characters + while( *in && !strchr(delims,*in) ) + { + *out = *in; + ++out; + ++in; + } + // skip trim characters + while( *in && strchr(delims,*in) ) + ++in; + put_space = 1; + } + *out = '\0'; + return str; +} //stristr: Case insensitive version of strstr, code taken from //http://www.daniweb.com/code/snippet313.html, Dave Sinkula |