The implementation of the C language to remove adjacent repeating character functions

  • 2020-05-26 09:54:36
  • OfStack

The implementation of the C language to remove adjacent repeating character functions

Character deduplication function

Function: deduplication string adjacent to the repeated characters, non - adjacent do not have to deduplication

Parameters:

arg1 -- input string
arg2 -- string start position
arg3 -- end of string position

Requirements:

When the input parameter is arg1, the string is deduplicated
When the input parameter is arg1, arg2, from the arg2 position to the end of the string, deduplication
When the input parameters are arg1, arg2 and arg3, the position from arg2 to arg3 is deweighted

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);

 PG_RETURN_TEXT_P(cstring_to_text(str));
}

Datum 
remove_dup_char_arg3 (PG_FUNCTION_ARGS)
{
 int n = 0;
 text *arg0 = PG_GETARG_TEXT_P(0);
 int32 arg1 = PG_GETARG_INT32(1);
 int32 arg2 = 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("out of range")));
 }

 remove_dup(str, arg1 - 1, arg2 - 1);

 PG_RETURN_TEXT_P(cstring_to_text(str));
}

void 
remove_dup(char *str, int start, int end)
{
 int i = 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';
}

It's a little bit more complicated, so I'm going to do 1, and I'm going to put 3 functions in 1

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 _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 _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 _null_ _null_ _null_ ));
DESCR("Remove duplicate characters.");

src/backend/utils/adt/myfuncs.c

Add definition:


#define PG_GETARG_IF_EXISTS(n, type, defval) \
 ((PG_NARGS() > (n) && !PG_ARGISNULL(n)) ? PG_GETARG_##type(n) : (defval)) 

Modification method:


/* 
 * Remove duplicate characters 
 * author:yangjie
 */
Datum 
remove_dup_char (PG_FUNCTION_ARGS)
{
 text *arg0 = PG_GETARG_IF_EXISTS(0, TEXT_P, NULL);
 int32 arg1 = PG_GETARG_IF_EXISTS(1, INT32, 0);
 int32 arg2 = PG_GETARG_IF_EXISTS(2, INT32, 0);
 int n = 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("out of range")));
 }
 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("out of range")));
 }
 remove_dup(str, arg1 - 1, arg2 - 1);
 }

 PG_RETURN_TEXT_P(cstring_to_text(str));
}

Change 1, if you have an input parameter, you use it, you don't use it, you use the default value and then you reprocess it to reduce code reuse


/* 
 * Remove duplicate characters 
 * author:yangjie
 */
Datum 
remove_dup_char (PG_FUNCTION_ARGS)
{
 text *arg0 = PG_GETARG_IF_EXISTS(0, TEXT_P, NULL);
 int n = 0;
 char *str = text_to_cstring(arg0);
 n = strlen(str);
 int32 arg1 = PG_GETARG_IF_EXISTS(1, INT32, 0);
 int32 arg2 = 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, int start, int end)
{
 int i = 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';
}
 

The above is the C language to remove adjacent repeated character function method, if you have any questions please leave a message or to the site community exchange discussion, thank you for reading, hope to help you, thank you for the support of the site!


Related articles: