恳请各位前辈来帮我分析一下这个我自己写的函数
- C/C++ code
#include"stdafx.h"#include"StrCuter.h"#include<malloc.h> //realloc函数的依赖项/******************************************\ * 字符串截取函数 * * 备注: * * 此函数是我在火车站等火车的时候的作品 * * 字符串截取一直是程序员常常要用到的操作 * * 时间地点: * * 2012年6月30日 黄石东——>武昌 * * 所有者:暗夜之鹰 *\******************************************/#ifndef USE_WCHARchar * strCuterA(char str[],char identifier[],bool isLeft)/***********参数列表的含义(依次) *****************\ * 原字符串 str * * 截取标识字符串 identifier * * 是否从左边截取 isLeft *\*************************************************/{ int i=0, temp_i=0, i_length=strlen(identifier); char* temp=new char[i_length]; while(str[i]!=NULL) { temp[temp_i]=str[i]; i++; if(temp[temp_i]!=identifier[temp_i]) //如果在临时字符串中有与标识字符串中不相符的字符,则移动索引至0 temp_i=0; else if(temp[temp_i]==identifier[temp_i]&&temp_i<i_length-1) temp_i++; else if(temp[temp_i]==identifier[temp_i]&&temp_i==(i_length-1)) { if(isLeft==true) { int target_i=i-i_length; realloc(temp,target_i+1); //重新分配内存,释放多余内存? for(i=0;i<target_i;i++) temp[i]=str[i]; temp[i]=NULL; } else temp=&str[i]; //先前new出来的内存到哪里去了,很明显,内存泄露了 return temp; } } return NULL;/**************返回值说明***************\* 如果返回值为NULL,则表明该 ** 字符串中不存在指定的标示符 *\***************************************/}#elsewchar_t * strCuterW(wchar_t str[],wchar_t identifier[],bool isLeft){ int i=0, temp_i=0, i_length=wcslen(identifier); wchar_t* temp=new wchar_t[i_length]; while(str[i]!=NULL) { temp[temp_i]=str[i]; i++; if(temp[temp_i]!=identifier[temp_i]) //如果在临时字符串中有与标识字符串中不相符的字符,则移动索引至0 temp_i=0; else if(temp[temp_i]==identifier[temp_i]&&temp_i<i_length-1) temp_i++; else if(temp[temp_i]==identifier[temp_i]&&temp_i==(i_length-1)) { if(isLeft==true) { int target_i=i-i_length; realloc(temp,target_i+1); //重新分配内存 for(i=0;i<target_i;i++) temp[i]=str[i]; temp[i]=NULL; } else temp=&str[i]; return temp; } } return NULL;}#endif#ifdef USE_TEMPLATEtemplate<typename returnType,typename parameterType>returnType * strCuter(parameterType* str,parameterType* identifier,int i_length,bool isLeft){ int i=0,temp_i=0; parameterType* temp=new parameterType[i_length]; while(str[i]!=NULL) { temp[temp_i]=str[i]; i++; if(temp[temp_i]!=identifier[temp_i]) //如果在临时字符串中有与标识字符串中不相符的字符,则移动索引至0 temp_i=0; else if(temp[temp_i]==identifier[temp_i]&&temp_i<i_length-1) temp_i++; else if(temp[temp_i]==identifier[temp_i]&&temp_i==(i_length-1)) { if(isLeft==true) { int target_i=i-i_length; realloc(temp,target_i+1); for(i=0;i<target_i;i++) temp[i]=str[i]; temp[i]=NULL; } else temp=&str[i]; return temp; } } return NULL;}#endif[解决办法]
这个没必要在函数里面new吧, 还要跑到外面去释放;
还有那个宏跨度太大了, 把函数声明放到头文件,这样宏只需要判断函数声明就可以了。
[解决办法]
- C/C++ code
char * strCuterA(char str[],char identifier[],bool isLeft)可以改为:bool strCuterA(const char *src, const char *indent, bool isLeft, char *dst);dst即截取后的字符串, 让用的人在外面分配空间
[解决办法]
其他程序用模板函数的话连.h和.cpp一起包含
[解决办法]
函数没对identifier检查,如果NULL的话就开心了
[解决办法]
说一些可读性的吧
1。命名比较乱。
或者我不知道这种命名法则。我用匈牙利命名法,这个似乎在模板时代已经被抛弃了。暂时还没发现一种比较通用的命名法。
2。宏跨度太大
那么长的跨度,楼上说了。
3。一个int 对应一个变量,简单的代码更容易维护。
4。注释。
这个和(1)类似
5。以上只是个人观点。
[解决办法]
- C/C++ code
// 我用C语言的风格写一个bool strCuterA(const char *str, const char *pSub, const char **ppDst, bool bIsLeft/* = true*/){ const char *s = str; const char *pTmp = pSub; if( !(str && pSub && *str && *pSub && ppDst) ) { return false; } while(*s && *pTmp) { if(*s++ != *pTmp++) { pTmp = pSub; s = ++str; } } if( *pTmp ) { return false; } *ppDst = bIsLeft ? str : (str+strlen(pSub)); return true;}