C语言去除相邻重复字符函数的实现方法
C语言去除相邻重复字符函数的实现方法
字符去重函数
功能:去重字符串相邻重复的字符,不相邻的不用去重
参数:
arg1--输入字符串
arg2--字符串开始位置
arg3--字符串结束位置
要求:
输入参数为arg1时,对这个字符串去重
输入参数为arg1,arg2时,从arg2位置到字符串结束,去重
输入参数为arg1,arg2,arg3时,从arg2到arg3位置,去重
src/include/catalog/pg_proc.h
DATA(insertOID=6669(remove_dup_charPGNSPPGUID121000fffftfi1025"25"_null__null__null__null__null_remove_dup_char_arg1_null__null__null_)); DESCR("Removeduplicatecharacters."); DATA(insertOID=6670(remove_dup_charPGNSPPGUID121000fffftfi2025"2523"_null__null__null__null__null_remove_dup_char_arg2_null__null__null_)); DESCR("Removeduplicatecharacters."); DATA(insertOID=6671(remove_dup_charPGNSPPGUID121000fffftfi3025"252323"_null__null__null__null__null_remove_dup_char_arg3_null__null__null_)); DESCR("Removeduplicatecharacters.");
src/backend/utils/adt/myfuncs.c
/* *Removeduplicatecharacters *author:young */ Datum remove_dup_char_arg1(PG_FUNCTION_ARGS) { intn=0; text*arg0=PG_GETARG_TEXT_P(0); char*str=text_to_cstring(arg0); n=strlen(str); remove_dup(str,0,n); PG_RETURN_TEXT_P(cstring_to_text(str)); } Datum remove_dup_char_arg2(PG_FUNCTION_ARGS) { intn=0; text*arg0=PG_GETARG_TEXT_P(0); int32arg1=PG_GETARG_INT32(1); char*str=text_to_cstring(arg0); n=strlen(str); if(!(1<=arg1&&arg1<=n)) { ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("outofrange"))); } remove_dup(str,arg1-1,n); PG_RETURN_TEXT_P(cstring_to_text(str)); } Datum remove_dup_char_arg3(PG_FUNCTION_ARGS) { intn=0; text*arg0=PG_GETARG_TEXT_P(0); int32arg1=PG_GETARG_INT32(1); int32arg2=PG_GETARG_INT32(2); char*str=text_to_cstring(arg0); n=strlen(str); if(!(1<=arg1&&arg1<=arg2&&arg2<=n)) { ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("outofrange"))); } remove_dup(str,arg1-1,arg2-1); PG_RETURN_TEXT_P(cstring_to_text(str)); } void remove_dup(char*str,intstart,intend) { inti=start,k=start; for(i=start;i<=end;i++) { if(str[i+1]&&str[i+1]==str[i]&&i+1<=end) { k++; } else { str[i-k]=str[i]; } } str[i-k]='\0'; }
比较繁琐,再做一下修改,三个函数放到一个中
src/include/catalog/pg_proc.h
DATA(insertOID=6669(remove_dup_charPGNSPPGUID121000fffftfi1025"25"_null__null__null__null__null_remove_dup_char_null__null__null_)); DESCR("Removeduplicatecharacters."); DATA(insertOID=6670(remove_dup_charPGNSPPGUID121000fffftfi2025"2523"_null__null__null__null__null_remove_dup_char_null__null__null_)); DESCR("Removeduplicatecharacters."); DATA(insertOID=6671(remove_dup_charPGNSPPGUID121000fffftfi3025"252323"_null__null__null__null__null_remove_dup_char_null__null__null_)); DESCR("Removeduplicatecharacters.");
src/backend/utils/adt/myfuncs.c
添加定义:
#definePG_GETARG_IF_EXISTS(n,type,defval)\ ((PG_NARGS()>(n)&&!PG_ARGISNULL(n))?PG_GETARG_##type(n):(defval))
修改方法:
/* *Removeduplicatecharacters *author:yangjie */ Datum remove_dup_char(PG_FUNCTION_ARGS) { text*arg0=PG_GETARG_IF_EXISTS(0,TEXT_P,NULL); int32arg1=PG_GETARG_IF_EXISTS(1,INT32,0); int32arg2=PG_GETARG_IF_EXISTS(2,INT32,0); intn=0; char*str=text_to_cstring(arg0); n=strlen(str); if(PG_NARGS()==1) { remove_dup(str,0,n); } if(PG_NARGS()==2) { if(!(1<=arg1&&arg1<=n)) { ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("outofrange"))); } remove_dup(str,arg1-1,n); } if(PG_NARGS()==3) { if(!(1<=arg1&&arg1<=arg2&&arg2<=n)) { ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("outofrange"))); } remove_dup(str,arg1-1,arg2-1); } PG_RETURN_TEXT_P(cstring_to_text(str)); }
再修改一下,如果有输入参数就用没有就用默认值 最后再去重处理减少代码重用
/* *Removeduplicatecharacters *author:yangjie */ Datum remove_dup_char(PG_FUNCTION_ARGS) { text*arg0=PG_GETARG_IF_EXISTS(0,TEXT_P,NULL); intn=0; char*str=text_to_cstring(arg0); n=strlen(str); int32arg1=PG_GETARG_IF_EXISTS(1,INT32,0); int32arg2=PG_GETARG_IF_EXISTS(2,INT32,n); if(!(1<=arg1&&arg1<=n)) { ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("1<=arg1&&arg1<=n"))); } if(!(1<=arg1&&arg1<=arg2&&arg2<=n)) { ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("1<=arg1&&arg1<=arg2&&arg2<=n"))); } remove_dup(str,arg1,arg2); PG_RETURN_TEXT_P(cstring_to_text(str)); } void remove_dup(char*str,intstart,intend) { inti=start-1,k=start-1; for(i=start-1;i<=end-1;i++) { if(str[i+1]&&str[i+1]==str[i]&&i+1<=end-1) { k++; } else { str[i-k]=str[i]; } } str[i-k]='\0'; }
以上就是C语言去除相邻重复字符函数的实现方法,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!