C语言去除相邻重复字符函数的实现方法语言去除相邻重复字符函数的实现方法
C语言去除相邻重复字符函数的实现方法语言去除相邻重复字符函数的实现方法
字符去重函数
功能:去重字符串相邻重复的字符,不相邻的不用去重
参数:
arg1 — 输入字符串
arg2 — 字符串开始位置
arg3 — 字符串结束位置
要求:
输入参数为arg1时, 对这个字符串去重
输入参数为arg1,arg2时, 从arg2位置到字符串结束,去重
输入参数为arg1,arg2,arg3时,从arg2到arg3位置,去重
src/include/catalog/pg_proc.h
DATA(insert OID = 6669 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 25 "25" _null_ _null_ _null_ _null_ _null_ remove_dup_char_arg1
_null_ _null_ _null_ ));
DESCR("Remove duplicate characters.");
DATA(insert OID = 6670 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 25 "25 23" _null_ _null_ _null_ _null_ _null_ remove_dup_char_arg2
_null_ _null_ _null_ ));
DESCR("Remove duplicate characters.");
DATA(insert OID = 6671 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 3 0 25 "25 23 23" _null_ _null_ _null_ _null_ _null_
remove_dup_char_arg3 _null_ _null_ _null_ ));
DESCR("Remove duplicate characters.");
src/backend/utils/adt/myfuncs.c
/*
* Remove duplicate characters
* author:young
*/
Datum
remove_dup_char_arg1 (PG_FUNCTION_ARGS)
{
int n = 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)
{
int n = 0;
text *arg0 = PG_GETARG_TEXT_P(0);
int32 arg1 = 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("out of range")));
}
remove_dup(str, arg1 - 1, n);