1997-03-04 06:32:26 +01:00
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Edmund Mergl <E.Mergl@bawue.de>
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
1998-08-11 20:38:07 +02:00
|
|
|
* $Id: oracle_compat.c,v 1.15 1998/08/11 18:38:07 momjian Exp $
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
|
1997-09-08 23:56:23 +02:00
|
|
|
text *lower(text *string);
|
|
|
|
text *upper(text *string);
|
|
|
|
text *initcap(text *string);
|
|
|
|
text *lpad(text *string1, int4 len, text *string2);
|
|
|
|
text *rpad(text *string1, int4 len, text *string2);
|
|
|
|
text *btrim(text *string, text *set);
|
|
|
|
text *ltrim(text *string, text *set);
|
|
|
|
text *rtrim(text *string, text *set);
|
|
|
|
text *substr(text *string, int4 m, int4 n);
|
|
|
|
text *translate(text *string, char from, char to);
|
1997-03-16 21:40:52 +01:00
|
|
|
|
|
|
|
|
1997-03-04 06:32:26 +01:00
|
|
|
/********************************************************************
|
|
|
|
*
|
1997-03-16 21:40:52 +01:00
|
|
|
* lower
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* text *lower(text *string)
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns string, with all letters forced to lowercase.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
text *
|
1997-09-08 23:56:23 +02:00
|
|
|
lower(text *string)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
|
|
|
char *ptr,
|
|
|
|
*ptr_ret;
|
|
|
|
int m;
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if ((string == (text *) NULL) || ((m = VARSIZE(string) - VARHDRSZ) <= 0))
|
|
|
|
return string;
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
ret = (text *) palloc(VARSIZE(string));
|
|
|
|
VARSIZE(ret) = VARSIZE(string);
|
1997-03-15 07:00:19 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
ptr = VARDATA(string);
|
|
|
|
ptr_ret = VARDATA(ret);
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
while (m--)
|
1998-04-27 19:10:50 +02:00
|
|
|
*ptr_ret++ = tolower((unsigned char)*ptr++);
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return ret;
|
1997-03-04 06:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
*
|
1997-03-16 21:40:52 +01:00
|
|
|
* upper
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* text *upper(text *string)
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns string, with all letters forced to uppercase.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
text *
|
1997-09-08 23:56:23 +02:00
|
|
|
upper(text *string)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
|
|
|
char *ptr,
|
|
|
|
*ptr_ret;
|
|
|
|
int m;
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if ((string == (text *) NULL) || ((m = VARSIZE(string) - VARHDRSZ) <= 0))
|
|
|
|
return string;
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
ret = (text *) palloc(VARSIZE(string));
|
|
|
|
VARSIZE(ret) = VARSIZE(string);
|
1997-03-15 07:00:19 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
ptr = VARDATA(string);
|
|
|
|
ptr_ret = VARDATA(ret);
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
while (m--)
|
1998-04-27 19:10:50 +02:00
|
|
|
*ptr_ret++ = toupper((unsigned char)*ptr++);
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return ret;
|
1997-03-04 06:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
*
|
1997-03-16 21:40:52 +01:00
|
|
|
* initcap
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* text *initcap(text *string)
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns string, with first letter of each word in uppercase,
|
|
|
|
* all other letters in lowercase. A word is delimited by white
|
|
|
|
* space.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
text *
|
1997-09-08 23:56:23 +02:00
|
|
|
initcap(text *string)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
|
|
|
char *ptr,
|
|
|
|
*ptr_ret;
|
|
|
|
int m;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if ((string == (text *) NULL) || ((m = VARSIZE(string) - VARHDRSZ) <= 0))
|
|
|
|
return string;
|
|
|
|
|
|
|
|
ret = (text *) palloc(VARSIZE(string));
|
|
|
|
VARSIZE(ret) = VARSIZE(string);
|
|
|
|
|
|
|
|
ptr = VARDATA(string);
|
|
|
|
ptr_ret = VARDATA(ret);
|
|
|
|
|
1998-04-27 19:10:50 +02:00
|
|
|
*ptr_ret++ = toupper((unsigned char)*ptr++);
|
1997-09-07 07:04:48 +02:00
|
|
|
--m;
|
|
|
|
|
|
|
|
while (m--)
|
|
|
|
{
|
|
|
|
if (*(ptr_ret - 1) == ' ' || *(ptr_ret - 1) == ' ')
|
1998-04-27 19:10:50 +02:00
|
|
|
*ptr_ret++ = toupper((unsigned char)*ptr++);
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
1998-04-27 19:10:50 +02:00
|
|
|
*ptr_ret++ = tolower((unsigned char)*ptr++);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
1997-03-04 06:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
*
|
1997-03-16 21:40:52 +01:00
|
|
|
* lpad
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* text *lpad(text *string1, int4 len, text *string2)
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns string1, left-padded to length len with the sequence of
|
|
|
|
* characters in string2.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
text *
|
1997-09-08 23:56:23 +02:00
|
|
|
lpad(text *string1, int4 len, text *string2)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
|
|
|
char *ptr1,
|
|
|
|
*ptr2,
|
|
|
|
*ptr_ret;
|
|
|
|
int m,
|
|
|
|
n;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if ((string1 == (text *) NULL) ||
|
|
|
|
(len <= (VARSIZE(string1) - VARHDRSZ)) ||
|
|
|
|
((m = len - VARSIZE(string1) + VARHDRSZ) <= 0) ||
|
|
|
|
(string2 == (text *) NULL) ||
|
|
|
|
((VARSIZE(string2) - VARHDRSZ) <= 0))
|
|
|
|
return string1;
|
|
|
|
|
|
|
|
ret = (text *) palloc(VARHDRSZ + len);
|
|
|
|
VARSIZE(ret) = VARHDRSZ + len;
|
|
|
|
|
|
|
|
ptr2 = VARDATA(string2);
|
|
|
|
ptr_ret = VARDATA(ret);
|
|
|
|
|
|
|
|
while (m--)
|
|
|
|
{
|
|
|
|
*ptr_ret++ = *ptr2;
|
|
|
|
ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = VARSIZE(string1) - VARHDRSZ;
|
|
|
|
ptr1 = VARDATA(string1);
|
|
|
|
|
|
|
|
while (n--)
|
|
|
|
*ptr_ret++ = *ptr1++;
|
|
|
|
|
|
|
|
return ret;
|
1997-03-04 06:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
*
|
1997-03-16 21:40:52 +01:00
|
|
|
* rpad
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* text *rpad(text *string1, int4 len, text *string2)
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns string1, right-padded to length len with the sequence of
|
|
|
|
* characters in string2.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
text *
|
1997-09-08 23:56:23 +02:00
|
|
|
rpad(text *string1, int4 len, text *string2)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
|
|
|
char *ptr1,
|
|
|
|
*ptr2,
|
|
|
|
*ptr_ret;
|
|
|
|
int m,
|
|
|
|
n;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if ((string1 == (text *) NULL) ||
|
|
|
|
(len <= (VARSIZE(string1) - VARHDRSZ)) ||
|
|
|
|
((m = len - VARSIZE(string1) + VARHDRSZ) <= 0) ||
|
|
|
|
(string2 == (text *) NULL) ||
|
|
|
|
((VARSIZE(string2) - VARHDRSZ) <= 0))
|
|
|
|
return string1;
|
|
|
|
|
|
|
|
ret = (text *) palloc(VARHDRSZ + len);
|
|
|
|
VARSIZE(ret) = VARHDRSZ + len;
|
|
|
|
|
|
|
|
n = VARSIZE(string1) - VARHDRSZ;
|
|
|
|
ptr1 = VARDATA(string1);
|
|
|
|
ptr_ret = VARDATA(ret);
|
|
|
|
|
|
|
|
while (n--)
|
|
|
|
*ptr_ret++ = *ptr1++;
|
|
|
|
|
|
|
|
ptr2 = VARDATA(string2);
|
|
|
|
|
|
|
|
while (m--)
|
|
|
|
{
|
|
|
|
*ptr_ret++ = *ptr2;
|
|
|
|
ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
1997-03-04 06:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-07-29 18:12:07 +02:00
|
|
|
/********************************************************************
|
|
|
|
*
|
|
|
|
* btrim
|
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* text *btrim(text *string, text *set)
|
1997-07-29 18:12:07 +02:00
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns string with characters removed from the front and back
|
|
|
|
* up to the first character not in set.
|
1997-07-29 18:12:07 +02:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
text *
|
1997-09-08 23:56:23 +02:00
|
|
|
btrim(text *string, text *set)
|
1997-07-29 18:12:07 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
|
|
|
char *ptr,
|
|
|
|
*end,
|
|
|
|
*ptr2,
|
|
|
|
*end2;
|
|
|
|
int m;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if ((string == (text *) NULL) ||
|
|
|
|
((m = VARSIZE(string) - VARHDRSZ) <= 0) ||
|
|
|
|
(set == (text *) NULL) ||
|
|
|
|
((VARSIZE(set) - VARHDRSZ) <= 0))
|
|
|
|
return string;
|
|
|
|
|
|
|
|
ptr = VARDATA(string);
|
|
|
|
ptr2 = VARDATA(set);
|
|
|
|
end2 = VARDATA(set) + VARSIZE(set) - VARHDRSZ - 1;
|
|
|
|
|
|
|
|
while (m--)
|
|
|
|
{
|
|
|
|
while (ptr2 <= end2)
|
|
|
|
{
|
|
|
|
if (*ptr == *ptr2)
|
|
|
|
break;
|
|
|
|
++ptr2;
|
|
|
|
}
|
1998-08-11 20:38:07 +02:00
|
|
|
if (ptr2 > end2)
|
1997-09-07 07:04:48 +02:00
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
ptr2 = VARDATA(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
++m;
|
|
|
|
|
|
|
|
end = VARDATA(string) + VARSIZE(string) - VARHDRSZ - 1;
|
|
|
|
ptr2 = VARDATA(set);
|
|
|
|
|
|
|
|
while (m--)
|
|
|
|
{
|
|
|
|
while (ptr2 <= end2)
|
|
|
|
{
|
|
|
|
if (*end == *ptr2)
|
|
|
|
break;
|
|
|
|
++ptr2;
|
|
|
|
}
|
1998-08-11 20:38:07 +02:00
|
|
|
if (ptr2 > end2)
|
1997-09-07 07:04:48 +02:00
|
|
|
break;
|
|
|
|
--end;
|
|
|
|
ptr2 = VARDATA(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
++m;
|
|
|
|
|
|
|
|
ret = (text *) palloc(VARHDRSZ + m);
|
|
|
|
VARSIZE(ret) = VARHDRSZ + m;
|
|
|
|
memcpy(VARDATA(ret), ptr, m);
|
|
|
|
|
|
|
|
return ret;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* btrim() */
|
1997-07-29 18:12:07 +02:00
|
|
|
|
|
|
|
|
1997-03-04 06:32:26 +01:00
|
|
|
/********************************************************************
|
|
|
|
*
|
1997-03-16 21:40:52 +01:00
|
|
|
* ltrim
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* text *ltrim(text *string, text *set)
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns string with initial characters removed up to the first
|
|
|
|
* character not in set.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
text *
|
1997-09-08 23:56:23 +02:00
|
|
|
ltrim(text *string, text *set)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
|
|
|
char *ptr,
|
|
|
|
*ptr2,
|
|
|
|
*end2;
|
|
|
|
int m;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if ((string == (text *) NULL) ||
|
|
|
|
((m = VARSIZE(string) - VARHDRSZ) <= 0) ||
|
|
|
|
(set == (text *) NULL) ||
|
|
|
|
((VARSIZE(set) - VARHDRSZ) <= 0))
|
|
|
|
return string;
|
|
|
|
|
|
|
|
ptr = VARDATA(string);
|
|
|
|
ptr2 = VARDATA(set);
|
|
|
|
end2 = VARDATA(set) + VARSIZE(set) - VARHDRSZ - 1;
|
|
|
|
|
|
|
|
while (m--)
|
|
|
|
{
|
|
|
|
while (ptr2 <= end2)
|
|
|
|
{
|
|
|
|
if (*ptr == *ptr2)
|
|
|
|
break;
|
|
|
|
++ptr2;
|
|
|
|
}
|
1998-08-11 20:38:07 +02:00
|
|
|
if (ptr2 > end2)
|
1997-09-07 07:04:48 +02:00
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
ptr2 = VARDATA(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
++m;
|
|
|
|
|
|
|
|
ret = (text *) palloc(VARHDRSZ + m);
|
|
|
|
VARSIZE(ret) = VARHDRSZ + m;
|
|
|
|
|
|
|
|
memcpy(VARDATA(ret), ptr, m);
|
|
|
|
|
|
|
|
return ret;
|
1997-03-04 06:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
*
|
1997-03-16 21:40:52 +01:00
|
|
|
* rtrim
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* text *rtrim(text *string, text *set)
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns string with final characters removed after the last
|
|
|
|
* character not in set.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
text *
|
1997-09-08 23:56:23 +02:00
|
|
|
rtrim(text *string, text *set)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
|
|
|
char *ptr,
|
|
|
|
*ptr2,
|
|
|
|
*end2,
|
|
|
|
*ptr_ret;
|
|
|
|
int m;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if ((string == (text *) NULL) ||
|
|
|
|
((m = VARSIZE(string) - VARHDRSZ) <= 0) ||
|
|
|
|
(set == (text *) NULL) ||
|
|
|
|
((VARSIZE(set) - VARHDRSZ) <= 0))
|
|
|
|
return string;
|
|
|
|
|
|
|
|
ptr = VARDATA(string) + VARSIZE(string) - VARHDRSZ - 1;
|
|
|
|
ptr2 = VARDATA(set);
|
|
|
|
end2 = VARDATA(set) + VARSIZE(set) - VARHDRSZ - 1;
|
|
|
|
|
|
|
|
while (m--)
|
|
|
|
{
|
|
|
|
while (ptr2 <= end2)
|
|
|
|
{
|
|
|
|
if (*ptr == *ptr2)
|
|
|
|
break;
|
|
|
|
++ptr2;
|
|
|
|
}
|
1998-08-11 20:38:07 +02:00
|
|
|
if (ptr2 > end2)
|
1997-09-07 07:04:48 +02:00
|
|
|
break;
|
|
|
|
--ptr;
|
|
|
|
ptr2 = VARDATA(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
++m;
|
|
|
|
|
|
|
|
ret = (text *) palloc(VARHDRSZ + m);
|
|
|
|
VARSIZE(ret) = VARHDRSZ + m;
|
1997-07-29 18:12:07 +02:00
|
|
|
#if FALSE
|
1997-09-07 07:04:48 +02:00
|
|
|
memcpy(VARDATA(ret), ptr - VARSIZE(ret) + m, m);
|
1997-07-29 18:12:07 +02:00
|
|
|
#endif
|
1997-03-15 07:00:19 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
ptr_ret = VARDATA(ret) + m - 1;
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
while (m--)
|
|
|
|
*ptr_ret-- = *ptr--;
|
1997-03-15 07:00:19 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return ret;
|
1997-03-04 06:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
*
|
1997-03-16 21:40:52 +01:00
|
|
|
* substr
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* text *substr(text *string, int4 m, int4 n)
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns a portion of string, beginning at character m, n
|
|
|
|
* characters long. The first position of string is 1.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
text *
|
1997-09-08 23:56:23 +02:00
|
|
|
substr(text *string, int4 m, int4 n)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
|
|
|
char *ptr,
|
|
|
|
*ptr_ret;
|
|
|
|
int len;
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if ((string == (text *) NULL) ||
|
|
|
|
(m <= 0) || (n <= 0) ||
|
1998-01-13 04:49:51 +01:00
|
|
|
((len = VARSIZE(string) - VARHDRSZ - m + 1) <= 0))
|
1997-09-07 07:04:48 +02:00
|
|
|
return string;
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
len = len + 1 < n ? len + 1 : n;
|
1997-03-15 07:00:19 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
ret = (text *) palloc(VARHDRSZ + len);
|
|
|
|
VARSIZE(ret) = VARHDRSZ + len;
|
1997-03-15 07:00:19 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
ptr = VARDATA(string) + m - 1;
|
|
|
|
ptr_ret = VARDATA(ret);
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
while (len--)
|
|
|
|
*ptr_ret++ = *ptr++;
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return ret;
|
1997-03-04 06:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
*
|
1997-03-16 21:40:52 +01:00
|
|
|
* translate
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* text *translate(text *string, char from, char to)
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns string after replacing all occurences of from with
|
|
|
|
* the corresponding character in to. TRANSLATE will not remove
|
|
|
|
* characters.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
text *
|
1997-09-08 23:56:23 +02:00
|
|
|
translate(text *string, char from, char to)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
|
|
|
char *ptr,
|
|
|
|
*ptr_ret;
|
|
|
|
int m;
|
1997-03-04 06:32:26 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if ((string == (text *) NULL) ||
|
|
|
|
((m = VARSIZE(string) - VARHDRSZ) <= 0))
|
|
|
|
return string;
|
1997-03-15 07:00:19 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
ret = (text *) palloc(VARSIZE(string));
|
|
|
|
VARSIZE(ret) = VARSIZE(string);
|
1997-03-15 07:00:19 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
ptr = VARDATA(string);
|
|
|
|
ptr_ret = VARDATA(ret);
|
1997-03-15 07:00:19 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
while (m--)
|
|
|
|
{
|
|
|
|
*ptr_ret++ = *ptr == from ? to : *ptr;
|
|
|
|
ptr++;
|
|
|
|
}
|
1997-03-15 07:00:19 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return ret;
|
1997-03-15 07:00:19 +01:00
|
|
|
}
|
|
|
|
|
1997-03-04 06:32:26 +01:00
|
|
|
|
|
|
|
/* EOF */
|