修改 strtrim()

This commit is contained in:
Liu Chao
2015-12-20 23:18:37 +08:00
parent 67751a2717
commit a005ca0835
2 changed files with 28 additions and 36 deletions
+9 -11
View File
@@ -42,6 +42,7 @@
#ifdef LCUI_BUILD_IN_WIN32
#include <tchar.h>
#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
+19 -25
View File
@@ -40,27 +40,23 @@
#include <LCUI_Build.h>
#include <LCUI/LCUI.h>
#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;