diff --git a/include/LCUI/misc/string.h b/include/LCUI/misc/string.h index 73131b0c..2bfef8cb 100755 --- a/include/LCUI/misc/string.h +++ b/include/LCUI/misc/string.h @@ -42,6 +42,7 @@ #ifdef LCUI_BUILD_IN_WIN32 #include +#define strcasecmp stricmp #else #ifdef _UNICODE #define __T(x) L ## x @@ -59,19 +60,16 @@ typedef char TCHAR; #define _TCHAR_DEFINED #endif -#ifdef LCUI_BUILD_IN_LINUX -#define stricmp strcasecmp -#endif - LCUI_BEGIN_HEADER -/** - * 清除字符串首尾的空白符 - * @param[out] outstr 处理后的字符串 - * @param[in] instr 需要处理的字符串 - * @return 处理后的字符串的长度 - */ -LCUI_API int strtrim( char *outstr, const char *instr ); +/** +* 清除字符串首尾的字符 +* @param[out] outstr 处理后的字符串 +* @param[in] instr 需要处理的字符串 +* @param[in] charlist 需要清除的字符列表,当为NULL时,默认清除空白符 +* @return 处理后的字符串的长度 +*/ +LCUI_API int strtrim( char *outstr, const char *instr, const char *charlist ); LCUI_END_HEADER diff --git a/src/misc/string.c b/src/misc/string.c index 340f6c8e..3620c87a 100755 --- a/src/misc/string.c +++ b/src/misc/string.c @@ -40,27 +40,23 @@ #include #include -#define case_in_blank_char \ - case '\t': \ - case ' ': \ - case '\n': \ - case '\r' - -/** - * 清除字符串首尾的空白符 - * @param[out] outstr 处理后的字符串 - * @param[in] instr 需要处理的字符串 - * @return 处理后的字符串的长度 - */ -int strtrim( char *outstr, const char *instr ) +int strtrim( char *outstr, const char *instr, const char *charlist ) { + LCUI_BOOL clear, clear_left = TRUE; char *op = outstr, *last_blank = NULL; - LCUI_BOOL clear_left = 1; - const char *ip = instr; + const char *default_char_list = "\t\n\r ", *ip = instr, *c; + if( !charlist ) { + charlist = default_char_list; + } for( ; *ip; ip++ ) { - switch( *ip ) { - case_in_blank_char: + for( clear = FALSE, c = charlist; *c; ++c ) { + if( *ip == *c ) { + clear = TRUE; + break; + } + } + if( clear ) { if( !clear_left ) { *op = *ip; if( !last_blank ) { @@ -69,15 +65,13 @@ int strtrim( char *outstr, const char *instr ) ++op; } continue; - default: - if( clear_left ) { - clear_left = FALSE; - } - last_blank = NULL; - *op = *ip; - ++op; - break; } + if( clear_left ) { + clear_left = FALSE; + } + last_blank = NULL; + *op = *ip; + ++op; } if( last_blank ) { *last_blank = 0;