2001-09-23 13:02:01 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* oracle_compat.c
|
|
|
|
* Oracle compatible functions.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
2016-01-02 19:33:40 +01:00
|
|
|
* Copyright (c) 1996-2016, PostgreSQL Global Development Group
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
2001-09-23 13:02:01 +02:00
|
|
|
* Author: Edmund Mergl <E.Mergl@bawue.de>
|
|
|
|
* Multibyte enhancement: Tatsuo Ishii <ishii@postgresql.org>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/utils/adt/oracle_compat.c
|
2001-09-23 13:02:01 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
1997-03-04 06:32:26 +01:00
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2000-07-06 07:48:31 +02:00
|
|
|
|
2000-03-15 00:06:59 +01:00
|
|
|
#include "utils/builtins.h"
|
2008-06-23 21:27:19 +02:00
|
|
|
#include "utils/formatting.h"
|
2001-09-22 05:26:30 +02:00
|
|
|
#include "mb/pg_wchar.h"
|
1997-03-16 21:40:52 +01:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
|
|
|
|
static text *dotrim(const char *string, int stringlen,
|
2003-08-04 02:43:34 +02:00
|
|
|
const char *set, int setlen,
|
|
|
|
bool doltrim, bool dortrim);
|
2003-05-24 00:33:23 +02: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:
|
|
|
|
*
|
2000-07-06 07:48:31 +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
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
|
|
|
lower(PG_FUNCTION_ARGS)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
text *in_string = PG_GETARG_TEXT_PP(0);
|
|
|
|
char *out_string;
|
|
|
|
text *result;
|
2004-05-22 02:34:51 +02:00
|
|
|
|
2008-07-12 02:44:38 +02:00
|
|
|
out_string = str_tolower(VARDATA_ANY(in_string),
|
2011-02-08 22:04:18 +01:00
|
|
|
VARSIZE_ANY_EXHDR(in_string),
|
|
|
|
PG_GET_COLLATION());
|
2008-06-23 21:27:19 +02:00
|
|
|
result = cstring_to_text(out_string);
|
|
|
|
pfree(out_string);
|
2004-05-22 02:34:51 +02:00
|
|
|
|
2008-06-23 21:27:19 +02:00
|
|
|
PG_RETURN_TEXT_P(result);
|
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:
|
|
|
|
*
|
2000-07-06 07:48:31 +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
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
|
|
|
upper(PG_FUNCTION_ARGS)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
text *in_string = PG_GETARG_TEXT_PP(0);
|
|
|
|
char *out_string;
|
|
|
|
text *result;
|
2004-05-22 02:34:51 +02:00
|
|
|
|
2008-07-12 02:44:38 +02:00
|
|
|
out_string = str_toupper(VARDATA_ANY(in_string),
|
2011-02-08 22:04:18 +01:00
|
|
|
VARSIZE_ANY_EXHDR(in_string),
|
|
|
|
PG_GET_COLLATION());
|
2008-06-23 21:27:19 +02:00
|
|
|
result = cstring_to_text(out_string);
|
|
|
|
pfree(out_string);
|
2004-05-22 02:34:51 +02:00
|
|
|
|
2008-06-23 21:27:19 +02:00
|
|
|
PG_RETURN_TEXT_P(result);
|
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:
|
|
|
|
*
|
2000-07-06 07:48:31 +02:00
|
|
|
* text initcap(text string)
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
2004-02-27 04:59:23 +01:00
|
|
|
* Returns string, with first letter of each word in uppercase, all
|
|
|
|
* other letters in lowercase. A word is defined as a sequence of
|
|
|
|
* alphanumeric characters, delimited by non-alphanumeric
|
|
|
|
* characters.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
|
|
|
initcap(PG_FUNCTION_ARGS)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
text *in_string = PG_GETARG_TEXT_PP(0);
|
|
|
|
char *out_string;
|
|
|
|
text *result;
|
2004-06-07 00:17:01 +02:00
|
|
|
|
2008-07-12 02:44:38 +02:00
|
|
|
out_string = str_initcap(VARDATA_ANY(in_string),
|
2011-02-08 22:04:18 +01:00
|
|
|
VARSIZE_ANY_EXHDR(in_string),
|
|
|
|
PG_GET_COLLATION());
|
2008-06-23 21:27:19 +02:00
|
|
|
result = cstring_to_text(out_string);
|
|
|
|
pfree(out_string);
|
2004-06-07 00:17:01 +02:00
|
|
|
|
2008-06-23 21:27:19 +02:00
|
|
|
PG_RETURN_TEXT_P(result);
|
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:
|
|
|
|
*
|
2000-07-06 07:48:31 +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
|
2000-12-08 00:22:56 +01:00
|
|
|
* characters in string2. If len is less than the length of string1,
|
|
|
|
* instead truncate (on the right) to len.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
Datum
|
|
|
|
lpad(PG_FUNCTION_ARGS)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
text *string1 = PG_GETARG_TEXT_PP(0);
|
2000-06-13 09:35:40 +02:00
|
|
|
int32 len = PG_GETARG_INT32(1);
|
2007-09-22 00:52:52 +02:00
|
|
|
text *string2 = PG_GETARG_TEXT_PP(2);
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
|
|
|
char *ptr1,
|
|
|
|
*ptr2,
|
2007-09-22 00:52:52 +02:00
|
|
|
*ptr2start,
|
2000-12-08 00:22:56 +01:00
|
|
|
*ptr2end,
|
1997-09-08 04:41:22 +02:00
|
|
|
*ptr_ret;
|
|
|
|
int m,
|
2000-12-08 00:22:56 +01:00
|
|
|
s1len,
|
|
|
|
s2len;
|
2001-10-25 07:50:21 +02:00
|
|
|
|
|
|
|
int bytelen;
|
2000-12-08 00:22:56 +01:00
|
|
|
|
|
|
|
/* Negative len is silently taken as zero */
|
|
|
|
if (len < 0)
|
|
|
|
len = 0;
|
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
s1len = VARSIZE_ANY_EXHDR(string1);
|
2000-12-08 00:22:56 +01:00
|
|
|
if (s1len < 0)
|
|
|
|
s1len = 0; /* shouldn't happen */
|
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
s2len = VARSIZE_ANY_EXHDR(string2);
|
2000-12-08 00:22:56 +01:00
|
|
|
if (s2len < 0)
|
|
|
|
s2len = 0; /* shouldn't happen */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
s1len = pg_mbstrlen_with_len(VARDATA_ANY(string1), s1len);
|
2002-08-29 09:22:30 +02:00
|
|
|
|
2000-12-08 00:22:56 +01:00
|
|
|
if (s1len > len)
|
|
|
|
s1len = len; /* truncate string1 to len chars */
|
|
|
|
|
|
|
|
if (s2len <= 0)
|
|
|
|
len = s1len; /* nothing to pad with, so don't pad */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-09-23 13:02:01 +02:00
|
|
|
bytelen = pg_database_encoding_max_length() * len;
|
2002-08-22 06:55:05 +02:00
|
|
|
|
|
|
|
/* check for integer overflow */
|
|
|
|
if (len != 0 && bytelen / pg_database_encoding_max_length() != len)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("requested length too large")));
|
2002-08-22 06:55:05 +02:00
|
|
|
|
2001-09-23 13:02:01 +02:00
|
|
|
ret = (text *) palloc(VARHDRSZ + bytelen);
|
2002-08-29 09:22:30 +02:00
|
|
|
|
2000-12-08 00:22:56 +01:00
|
|
|
m = len - s1len;
|
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
ptr2 = ptr2start = VARDATA_ANY(string2);
|
2000-12-08 00:22:56 +01:00
|
|
|
ptr2end = ptr2 + s2len;
|
1997-09-07 07:04:48 +02:00
|
|
|
ptr_ret = VARDATA(ret);
|
|
|
|
|
2001-09-23 13:02:01 +02:00
|
|
|
while (m--)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
int mlen = pg_mblen(ptr2);
|
|
|
|
|
|
|
|
memcpy(ptr_ret, ptr2, mlen);
|
|
|
|
ptr_ret += mlen;
|
|
|
|
ptr2 += mlen;
|
|
|
|
if (ptr2 == ptr2end) /* wrap around at end of s2 */
|
2007-09-22 00:52:52 +02:00
|
|
|
ptr2 = ptr2start;
|
2001-09-23 13:02:01 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
ptr1 = VARDATA_ANY(string1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-09-23 13:02:01 +02:00
|
|
|
while (s1len--)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
int mlen = pg_mblen(ptr1);
|
|
|
|
|
|
|
|
memcpy(ptr_ret, ptr1, mlen);
|
|
|
|
ptr_ret += mlen;
|
|
|
|
ptr1 += mlen;
|
2001-09-23 13:02:01 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(ret, ptr_ret - (char *) ret);
|
2002-01-08 18:03:41 +01:00
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
PG_RETURN_TEXT_P(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:
|
|
|
|
*
|
2000-07-06 07:48:31 +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
|
2000-12-08 00:22:56 +01:00
|
|
|
* characters in string2. If len is less than the length of string1,
|
|
|
|
* instead truncate (on the right) to len.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
Datum
|
|
|
|
rpad(PG_FUNCTION_ARGS)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
text *string1 = PG_GETARG_TEXT_PP(0);
|
2000-06-13 09:35:40 +02:00
|
|
|
int32 len = PG_GETARG_INT32(1);
|
2007-09-22 00:52:52 +02:00
|
|
|
text *string2 = PG_GETARG_TEXT_PP(2);
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
|
|
|
char *ptr1,
|
|
|
|
*ptr2,
|
2007-09-22 00:52:52 +02:00
|
|
|
*ptr2start,
|
2000-12-08 00:22:56 +01:00
|
|
|
*ptr2end,
|
1997-09-08 04:41:22 +02:00
|
|
|
*ptr_ret;
|
|
|
|
int m,
|
2000-12-08 00:22:56 +01:00
|
|
|
s1len,
|
|
|
|
s2len;
|
2001-10-25 07:50:21 +02:00
|
|
|
|
|
|
|
int bytelen;
|
2000-12-08 00:22:56 +01:00
|
|
|
|
|
|
|
/* Negative len is silently taken as zero */
|
|
|
|
if (len < 0)
|
|
|
|
len = 0;
|
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
s1len = VARSIZE_ANY_EXHDR(string1);
|
2000-12-08 00:22:56 +01:00
|
|
|
if (s1len < 0)
|
|
|
|
s1len = 0; /* shouldn't happen */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
s2len = VARSIZE_ANY_EXHDR(string2);
|
2000-12-08 00:22:56 +01:00
|
|
|
if (s2len < 0)
|
|
|
|
s2len = 0; /* shouldn't happen */
|
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
s1len = pg_mbstrlen_with_len(VARDATA_ANY(string1), s1len);
|
2001-09-23 13:02:01 +02:00
|
|
|
|
2000-12-08 00:22:56 +01:00
|
|
|
if (s1len > len)
|
|
|
|
s1len = len; /* truncate string1 to len chars */
|
|
|
|
|
|
|
|
if (s2len <= 0)
|
|
|
|
len = s1len; /* nothing to pad with, so don't pad */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-09-23 13:02:01 +02:00
|
|
|
bytelen = pg_database_encoding_max_length() * len;
|
2002-08-22 06:55:05 +02:00
|
|
|
|
|
|
|
/* Check for integer overflow */
|
|
|
|
if (len != 0 && bytelen / pg_database_encoding_max_length() != len)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("requested length too large")));
|
2002-08-22 06:55:05 +02:00
|
|
|
|
2001-09-23 13:02:01 +02:00
|
|
|
ret = (text *) palloc(VARHDRSZ + bytelen);
|
2000-12-08 00:22:56 +01:00
|
|
|
m = len - s1len;
|
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
ptr1 = VARDATA_ANY(string1);
|
1997-09-07 07:04:48 +02:00
|
|
|
ptr_ret = VARDATA(ret);
|
|
|
|
|
2001-09-23 13:02:01 +02:00
|
|
|
while (s1len--)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
int mlen = pg_mblen(ptr1);
|
|
|
|
|
|
|
|
memcpy(ptr_ret, ptr1, mlen);
|
|
|
|
ptr_ret += mlen;
|
|
|
|
ptr1 += mlen;
|
2001-09-23 13:02:01 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
ptr2 = ptr2start = VARDATA_ANY(string2);
|
2000-12-08 00:22:56 +01:00
|
|
|
ptr2end = ptr2 + s2len;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-09-23 13:02:01 +02:00
|
|
|
while (m--)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
int mlen = pg_mblen(ptr2);
|
|
|
|
|
|
|
|
memcpy(ptr_ret, ptr2, mlen);
|
|
|
|
ptr_ret += mlen;
|
|
|
|
ptr2 += mlen;
|
|
|
|
if (ptr2 == ptr2end) /* wrap around at end of s2 */
|
2007-09-22 00:52:52 +02:00
|
|
|
ptr2 = ptr2start;
|
2001-09-23 13:02:01 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(ret, ptr_ret - (char *) ret);
|
2002-01-08 18:03:41 +01:00
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
PG_RETURN_TEXT_P(ret);
|
1997-03-04 06:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-07-29 18:12:07 +02:00
|
|
|
/********************************************************************
|
|
|
|
*
|
|
|
|
* btrim
|
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
2000-07-06 07:48:31 +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
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
|
|
|
btrim(PG_FUNCTION_ARGS)
|
1997-07-29 18:12:07 +02:00
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
text *string = PG_GETARG_TEXT_PP(0);
|
|
|
|
text *set = PG_GETARG_TEXT_PP(1);
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
|
|
|
|
VARDATA_ANY(set), VARSIZE_ANY_EXHDR(set),
|
2003-05-24 00:33:23 +02:00
|
|
|
true, true);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
PG_RETURN_TEXT_P(ret);
|
|
|
|
}
|
2001-09-22 05:26:30 +02:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
/********************************************************************
|
|
|
|
*
|
|
|
|
* btrim1 --- btrim with set fixed as ' '
|
|
|
|
*
|
|
|
|
********************************************************************/
|
2001-09-22 05:26:30 +02:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
Datum
|
|
|
|
btrim1(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
text *string = PG_GETARG_TEXT_PP(0);
|
2003-05-24 00:33:23 +02:00
|
|
|
text *ret;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
|
2003-05-24 00:33:23 +02:00
|
|
|
" ", 1,
|
|
|
|
true, true);
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
PG_RETURN_TEXT_P(ret);
|
|
|
|
}
|
2001-09-22 05:26:30 +02:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
/*
|
|
|
|
* Common implementation for btrim, ltrim, rtrim
|
|
|
|
*/
|
|
|
|
static text *
|
|
|
|
dotrim(const char *string, int stringlen,
|
|
|
|
const char *set, int setlen,
|
|
|
|
bool doltrim, bool dortrim)
|
|
|
|
{
|
|
|
|
int i;
|
2001-09-22 05:26:30 +02:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
/* Nothing to do if either string or set is empty */
|
|
|
|
if (stringlen > 0 && setlen > 0)
|
2001-09-22 05:26:30 +02:00
|
|
|
{
|
2003-05-24 00:33:23 +02:00
|
|
|
if (pg_database_encoding_max_length() > 1)
|
2001-09-22 05:26:30 +02:00
|
|
|
{
|
2003-05-24 00:33:23 +02:00
|
|
|
/*
|
|
|
|
* In the multibyte-encoding case, build arrays of pointers to
|
2005-10-15 04:49:52 +02:00
|
|
|
* character starts, so that we can avoid inefficient checks in
|
|
|
|
* the inner loops.
|
2003-05-24 00:33:23 +02:00
|
|
|
*/
|
|
|
|
const char **stringchars;
|
|
|
|
const char **setchars;
|
|
|
|
int *stringmblen;
|
|
|
|
int *setmblen;
|
|
|
|
int stringnchars;
|
|
|
|
int setnchars;
|
|
|
|
int resultndx;
|
|
|
|
int resultnchars;
|
|
|
|
const char *p;
|
|
|
|
int len;
|
|
|
|
int mblen;
|
|
|
|
const char *str_pos;
|
|
|
|
int str_len;
|
|
|
|
|
|
|
|
stringchars = (const char **) palloc(stringlen * sizeof(char *));
|
|
|
|
stringmblen = (int *) palloc(stringlen * sizeof(int));
|
|
|
|
stringnchars = 0;
|
|
|
|
p = string;
|
|
|
|
len = stringlen;
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
stringchars[stringnchars] = p;
|
|
|
|
stringmblen[stringnchars] = mblen = pg_mblen(p);
|
|
|
|
stringnchars++;
|
|
|
|
p += mblen;
|
|
|
|
len -= mblen;
|
|
|
|
}
|
|
|
|
|
|
|
|
setchars = (const char **) palloc(setlen * sizeof(char *));
|
|
|
|
setmblen = (int *) palloc(setlen * sizeof(int));
|
|
|
|
setnchars = 0;
|
|
|
|
p = set;
|
|
|
|
len = setlen;
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
setchars[setnchars] = p;
|
|
|
|
setmblen[setnchars] = mblen = pg_mblen(p);
|
|
|
|
setnchars++;
|
|
|
|
p += mblen;
|
|
|
|
len -= mblen;
|
|
|
|
}
|
|
|
|
|
|
|
|
resultndx = 0; /* index in stringchars[] */
|
|
|
|
resultnchars = stringnchars;
|
|
|
|
|
|
|
|
if (doltrim)
|
|
|
|
{
|
|
|
|
while (resultnchars > 0)
|
|
|
|
{
|
|
|
|
str_pos = stringchars[resultndx];
|
|
|
|
str_len = stringmblen[resultndx];
|
|
|
|
for (i = 0; i < setnchars; i++)
|
|
|
|
{
|
|
|
|
if (str_len == setmblen[i] &&
|
|
|
|
memcmp(str_pos, setchars[i], str_len) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i >= setnchars)
|
|
|
|
break; /* no match here */
|
|
|
|
string += str_len;
|
|
|
|
stringlen -= str_len;
|
|
|
|
resultndx++;
|
|
|
|
resultnchars--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dortrim)
|
|
|
|
{
|
|
|
|
while (resultnchars > 0)
|
|
|
|
{
|
|
|
|
str_pos = stringchars[resultndx + resultnchars - 1];
|
|
|
|
str_len = stringmblen[resultndx + resultnchars - 1];
|
|
|
|
for (i = 0; i < setnchars; i++)
|
|
|
|
{
|
|
|
|
if (str_len == setmblen[i] &&
|
|
|
|
memcmp(str_pos, setchars[i], str_len) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i >= setnchars)
|
|
|
|
break; /* no match here */
|
|
|
|
stringlen -= str_len;
|
|
|
|
resultnchars--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pfree(stringchars);
|
|
|
|
pfree(stringmblen);
|
|
|
|
pfree(setchars);
|
|
|
|
pfree(setmblen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* In the single-byte-encoding case, we don't need such overhead.
|
2003-05-24 00:33:23 +02:00
|
|
|
*/
|
|
|
|
if (doltrim)
|
|
|
|
{
|
|
|
|
while (stringlen > 0)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
char str_ch = *string;
|
2003-05-24 00:33:23 +02:00
|
|
|
|
|
|
|
for (i = 0; i < setlen; i++)
|
|
|
|
{
|
|
|
|
if (str_ch == set[i])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i >= setlen)
|
|
|
|
break; /* no match here */
|
|
|
|
string++;
|
|
|
|
stringlen--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dortrim)
|
|
|
|
{
|
|
|
|
while (stringlen > 0)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
char str_ch = string[stringlen - 1];
|
2003-05-24 00:33:23 +02:00
|
|
|
|
|
|
|
for (i = 0; i < setlen; i++)
|
|
|
|
{
|
|
|
|
if (str_ch == set[i])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i >= setlen)
|
|
|
|
break; /* no match here */
|
|
|
|
stringlen--;
|
|
|
|
}
|
|
|
|
}
|
2001-09-22 05:26:30 +02:00
|
|
|
}
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
/* Return selected portion of string */
|
2008-03-25 23:42:46 +01:00
|
|
|
return cstring_to_text_with_len(string, stringlen);
|
2000-07-06 07:48:31 +02:00
|
|
|
}
|
1997-07-29 18:12:07 +02:00
|
|
|
|
2001-09-14 19:46:40 +02:00
|
|
|
/********************************************************************
|
|
|
|
*
|
|
|
|
* byteatrim
|
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
|
|
|
* bytea byteatrim(byta string, bytea set)
|
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
|
|
|
* Returns string with characters removed from the front and back
|
|
|
|
* up to the first character not in set.
|
|
|
|
*
|
|
|
|
* Cloned from btrim and modified as required.
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
Datum
|
|
|
|
byteatrim(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
bytea *string = PG_GETARG_BYTEA_PP(0);
|
|
|
|
bytea *set = PG_GETARG_BYTEA_PP(1);
|
2001-09-14 19:46:40 +02:00
|
|
|
bytea *ret;
|
|
|
|
char *ptr,
|
|
|
|
*end,
|
|
|
|
*ptr2,
|
2007-09-22 00:52:52 +02:00
|
|
|
*ptr2start,
|
2001-09-14 19:46:40 +02:00
|
|
|
*end2;
|
2007-11-15 22:14:46 +01:00
|
|
|
int m,
|
|
|
|
stringlen,
|
2007-09-22 00:52:52 +02:00
|
|
|
setlen;
|
2001-09-14 19:46:40 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
stringlen = VARSIZE_ANY_EXHDR(string);
|
|
|
|
setlen = VARSIZE_ANY_EXHDR(set);
|
2007-11-15 22:14:46 +01:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
if (stringlen <= 0 || setlen <= 0)
|
2001-09-14 19:46:40 +02:00
|
|
|
PG_RETURN_BYTEA_P(string);
|
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
m = stringlen;
|
|
|
|
ptr = VARDATA_ANY(string);
|
|
|
|
end = ptr + stringlen - 1;
|
|
|
|
ptr2start = VARDATA_ANY(set);
|
|
|
|
end2 = ptr2start + setlen - 1;
|
2001-09-14 19:46:40 +02:00
|
|
|
|
|
|
|
while (m > 0)
|
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
ptr2 = ptr2start;
|
2001-09-14 19:46:40 +02:00
|
|
|
while (ptr2 <= end2)
|
|
|
|
{
|
|
|
|
if (*ptr == *ptr2)
|
|
|
|
break;
|
|
|
|
++ptr2;
|
|
|
|
}
|
|
|
|
if (ptr2 > end2)
|
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
m--;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (m > 0)
|
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
ptr2 = ptr2start;
|
2001-09-14 19:46:40 +02:00
|
|
|
while (ptr2 <= end2)
|
|
|
|
{
|
|
|
|
if (*end == *ptr2)
|
|
|
|
break;
|
|
|
|
++ptr2;
|
|
|
|
}
|
|
|
|
if (ptr2 > end2)
|
|
|
|
break;
|
|
|
|
end--;
|
|
|
|
m--;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = (bytea *) palloc(VARHDRSZ + m);
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(ret, VARHDRSZ + m);
|
2001-09-14 19:46:40 +02:00
|
|
|
memcpy(VARDATA(ret), ptr, m);
|
|
|
|
|
|
|
|
PG_RETURN_BYTEA_P(ret);
|
|
|
|
}
|
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:
|
|
|
|
*
|
2000-07-06 07:48:31 +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
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
|
|
|
ltrim(PG_FUNCTION_ARGS)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
text *string = PG_GETARG_TEXT_PP(0);
|
|
|
|
text *set = PG_GETARG_TEXT_PP(1);
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
|
|
|
|
VARDATA_ANY(set), VARSIZE_ANY_EXHDR(set),
|
2003-05-24 00:33:23 +02:00
|
|
|
true, false);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
PG_RETURN_TEXT_P(ret);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
/********************************************************************
|
|
|
|
*
|
|
|
|
* ltrim1 --- ltrim with set fixed as ' '
|
|
|
|
*
|
|
|
|
********************************************************************/
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
Datum
|
|
|
|
ltrim1(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
text *string = PG_GETARG_TEXT_PP(0);
|
2003-05-24 00:33:23 +02:00
|
|
|
text *ret;
|
2001-09-22 05:26:30 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
|
2003-05-24 00:33:23 +02:00
|
|
|
" ", 1,
|
|
|
|
true, false);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
PG_RETURN_TEXT_P(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:
|
|
|
|
*
|
2000-07-06 07:48:31 +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
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
|
|
|
rtrim(PG_FUNCTION_ARGS)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
text *string = PG_GETARG_TEXT_PP(0);
|
|
|
|
text *set = PG_GETARG_TEXT_PP(1);
|
1997-09-08 04:41:22 +02:00
|
|
|
text *ret;
|
2001-09-22 05:26:30 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
|
|
|
|
VARDATA_ANY(set), VARSIZE_ANY_EXHDR(set),
|
2003-05-24 00:33:23 +02:00
|
|
|
false, true);
|
2001-09-22 05:26:30 +02:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
PG_RETURN_TEXT_P(ret);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
/********************************************************************
|
|
|
|
*
|
|
|
|
* rtrim1 --- rtrim with set fixed as ' '
|
|
|
|
*
|
|
|
|
********************************************************************/
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2003-05-24 00:33:23 +02:00
|
|
|
Datum
|
|
|
|
rtrim1(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
text *string = PG_GETARG_TEXT_PP(0);
|
2003-05-24 00:33:23 +02:00
|
|
|
text *ret;
|
2001-09-22 05:26:30 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
|
2003-05-24 00:33:23 +02:00
|
|
|
" ", 1,
|
|
|
|
false, true);
|
1997-03-15 07:00:19 +01:00
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
PG_RETURN_TEXT_P(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:
|
|
|
|
*
|
2000-07-06 07:48:31 +02:00
|
|
|
* text translate(text string, text from, text to)
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
2000-03-15 18:24:18 +01:00
|
|
|
* Returns string after replacing all occurrences of characters in from
|
|
|
|
* with the corresponding character in to. If from is longer than to,
|
|
|
|
* occurrences of the extra characters in from are deleted.
|
|
|
|
* Improved by Edwin Ramirez <ramirez@doc.mssm.edu>.
|
1997-03-04 06:32:26 +01:00
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
|
|
|
translate(PG_FUNCTION_ARGS)
|
1997-03-04 06:32:26 +01:00
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
text *string = PG_GETARG_TEXT_PP(0);
|
|
|
|
text *from = PG_GETARG_TEXT_PP(1);
|
|
|
|
text *to = PG_GETARG_TEXT_PP(2);
|
2000-04-12 19:17:23 +02:00
|
|
|
text *result;
|
|
|
|
char *from_ptr,
|
|
|
|
*to_ptr;
|
|
|
|
char *source,
|
|
|
|
*target;
|
|
|
|
int m,
|
|
|
|
fromlen,
|
|
|
|
tolen,
|
|
|
|
retlen,
|
|
|
|
i;
|
2007-09-22 07:35:42 +02:00
|
|
|
int worst_len;
|
2001-10-25 07:50:21 +02:00
|
|
|
int len;
|
|
|
|
int source_len;
|
|
|
|
int from_index;
|
1997-03-04 06:32:26 +01:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
m = VARSIZE_ANY_EXHDR(string);
|
|
|
|
if (m <= 0)
|
2000-07-06 07:48:31 +02:00
|
|
|
PG_RETURN_TEXT_P(string);
|
2007-09-22 07:35:42 +02:00
|
|
|
source = VARDATA_ANY(string);
|
1997-03-15 07:00:19 +01:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
fromlen = VARSIZE_ANY_EXHDR(from);
|
|
|
|
from_ptr = VARDATA_ANY(from);
|
|
|
|
tolen = VARSIZE_ANY_EXHDR(to);
|
|
|
|
to_ptr = VARDATA_ANY(to);
|
|
|
|
|
2007-09-22 07:35:42 +02:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* The worst-case expansion is to substitute a max-length character for a
|
|
|
|
* single-byte character at each position of the string.
|
2007-09-22 07:35:42 +02:00
|
|
|
*/
|
|
|
|
worst_len = pg_database_encoding_max_length() * m;
|
2000-03-15 18:24:18 +01:00
|
|
|
|
2007-09-22 07:35:42 +02:00
|
|
|
/* check for integer overflow */
|
|
|
|
if (worst_len / pg_database_encoding_max_length() != m)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("requested length too large")));
|
2000-03-15 18:24:18 +01:00
|
|
|
|
2007-09-22 07:35:42 +02:00
|
|
|
result = (text *) palloc(worst_len + VARHDRSZ);
|
2000-03-15 18:24:18 +01:00
|
|
|
target = VARDATA(result);
|
2000-03-15 00:06:59 +01:00
|
|
|
retlen = 0;
|
2000-03-15 18:24:18 +01:00
|
|
|
|
2001-09-23 13:02:01 +02:00
|
|
|
while (m > 0)
|
|
|
|
{
|
|
|
|
source_len = pg_mblen(source);
|
|
|
|
from_index = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < fromlen; i += len)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
len = pg_mblen(&from_ptr[i]);
|
|
|
|
if (len == source_len &&
|
|
|
|
memcmp(source, &from_ptr[i], len) == 0)
|
|
|
|
break;
|
2001-09-23 13:02:01 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
from_index++;
|
2001-09-23 13:02:01 +02:00
|
|
|
}
|
|
|
|
if (i < fromlen)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
/* substitute */
|
|
|
|
char *p = to_ptr;
|
|
|
|
|
|
|
|
for (i = 0; i < from_index; i++)
|
|
|
|
{
|
|
|
|
p += pg_mblen(p);
|
|
|
|
if (p >= (to_ptr + tolen))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p < (to_ptr + tolen))
|
|
|
|
{
|
|
|
|
len = pg_mblen(p);
|
|
|
|
memcpy(target, p, len);
|
|
|
|
target += len;
|
|
|
|
retlen += len;
|
|
|
|
}
|
2001-09-23 13:02:01 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no match, so copy */
|
2001-10-25 07:50:21 +02:00
|
|
|
memcpy(target, source, source_len);
|
|
|
|
target += source_len;
|
|
|
|
retlen += source_len;
|
2001-09-23 13:02:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
source += source_len;
|
|
|
|
m -= source_len;
|
|
|
|
}
|
1997-03-15 07:00:19 +01:00
|
|
|
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(result, retlen + VARHDRSZ);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-03-15 18:24:18 +01:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* The function result is probably much bigger than needed, if we're using
|
|
|
|
* a multibyte encoding, but it's not worth reallocating it; the result
|
|
|
|
* probably won't live long anyway.
|
2000-03-15 18:24:18 +01:00
|
|
|
*/
|
1997-03-15 07:00:19 +01:00
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
PG_RETURN_TEXT_P(result);
|
2000-03-15 18:24:18 +01:00
|
|
|
}
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2000-09-25 14:58:47 +02:00
|
|
|
/********************************************************************
|
|
|
|
*
|
|
|
|
* ascii
|
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
|
|
|
* int ascii(text string)
|
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
|
|
|
* Returns the decimal representation of the first character from
|
|
|
|
* string.
|
2007-11-15 22:14:46 +01:00
|
|
|
* If the string is empty we return 0.
|
|
|
|
* If the database encoding is UTF8, we return the Unicode codepoint.
|
|
|
|
* If the database encoding is any other multi-byte encoding, we
|
|
|
|
* return the value of the first byte if it is an ASCII character
|
|
|
|
* (range 1 .. 127), or raise an error.
|
|
|
|
* For all other encodings we return the value of the first byte,
|
|
|
|
* (range 1..255).
|
2000-09-25 14:58:47 +02:00
|
|
|
*
|
|
|
|
********************************************************************/
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
|
|
|
ascii(PG_FUNCTION_ARGS)
|
2000-04-07 15:40:45 +02:00
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
text *string = PG_GETARG_TEXT_PP(0);
|
2007-11-15 22:14:46 +01:00
|
|
|
int encoding = GetDatabaseEncoding();
|
2007-09-18 19:41:17 +02:00
|
|
|
unsigned char *data;
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
if (VARSIZE_ANY_EXHDR(string) <= 0)
|
2000-07-06 07:48:31 +02:00
|
|
|
PG_RETURN_INT32(0);
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
data = (unsigned char *) VARDATA_ANY(string);
|
2007-09-18 19:41:17 +02:00
|
|
|
|
|
|
|
if (encoding == PG_UTF8 && *data > 127)
|
|
|
|
{
|
|
|
|
/* return the code point for Unicode */
|
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
int result = 0,
|
|
|
|
tbytes = 0,
|
|
|
|
i;
|
2007-09-18 19:41:17 +02:00
|
|
|
|
|
|
|
if (*data >= 0xF0)
|
|
|
|
{
|
|
|
|
result = *data & 0x07;
|
|
|
|
tbytes = 3;
|
|
|
|
}
|
|
|
|
else if (*data >= 0xE0)
|
|
|
|
{
|
|
|
|
result = *data & 0x0F;
|
|
|
|
tbytes = 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Assert(*data > 0xC0);
|
2007-09-18 19:41:17 +02:00
|
|
|
result = *data & 0x1f;
|
|
|
|
tbytes = 1;
|
|
|
|
}
|
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
Assert(tbytes > 0);
|
2007-09-18 19:41:17 +02:00
|
|
|
|
|
|
|
for (i = 1; i <= tbytes; i++)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Assert((data[i] & 0xC0) == 0x80);
|
2007-09-18 19:41:17 +02:00
|
|
|
result = (result << 6) + (data[i] & 0x3f);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_INT32(result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pg_encoding_max_length(encoding) > 1 && *data > 127)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("requested character too large")));
|
|
|
|
|
|
|
|
|
|
|
|
PG_RETURN_INT32((int32) *data);
|
|
|
|
}
|
2000-07-06 07:48:31 +02:00
|
|
|
}
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2000-09-25 14:58:47 +02:00
|
|
|
/********************************************************************
|
|
|
|
*
|
|
|
|
* chr
|
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
|
|
|
* text chr(int val)
|
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
2007-09-18 19:41:17 +02:00
|
|
|
* Returns the character having the binary equivalent to val.
|
|
|
|
*
|
|
|
|
* For UTF8 we treat the argumwent as a Unicode code point.
|
|
|
|
* For other multi-byte encodings we raise an error for arguments
|
|
|
|
* outside the strict ASCII range (1..127).
|
|
|
|
*
|
|
|
|
* It's important that we don't ever return a value that is not valid
|
|
|
|
* in the database encoding, so that this doesn't become a way for
|
|
|
|
* invalid data to enter the database.
|
2000-09-25 14:58:47 +02:00
|
|
|
*
|
|
|
|
********************************************************************/
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
Datum
|
2007-11-15 22:14:46 +01:00
|
|
|
chr (PG_FUNCTION_ARGS)
|
2000-04-07 15:40:45 +02:00
|
|
|
{
|
2007-09-18 19:41:17 +02:00
|
|
|
uint32 cvalue = PG_GETARG_UINT32(0);
|
2001-03-22 05:01:46 +01:00
|
|
|
text *result;
|
2007-11-15 22:14:46 +01:00
|
|
|
int encoding = GetDatabaseEncoding();
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2007-09-18 19:41:17 +02:00
|
|
|
if (encoding == PG_UTF8 && cvalue > 127)
|
|
|
|
{
|
|
|
|
/* for Unicode we treat the argument as a code point */
|
2007-11-15 22:14:46 +01:00
|
|
|
int bytes;
|
2014-05-16 22:51:28 +02:00
|
|
|
unsigned char *wch;
|
2007-09-18 19:41:17 +02:00
|
|
|
|
2014-05-16 22:51:28 +02:00
|
|
|
/*
|
|
|
|
* We only allow valid Unicode code points; per RFC3629 that stops at
|
|
|
|
* U+10FFFF, even though 4-byte UTF8 sequences can hold values up to
|
|
|
|
* U+1FFFFF.
|
|
|
|
*/
|
|
|
|
if (cvalue > 0x0010ffff)
|
2007-09-18 19:41:17 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
2007-11-15 22:14:46 +01:00
|
|
|
errmsg("requested character too large for encoding: %d",
|
2007-09-18 19:41:17 +02:00
|
|
|
cvalue)));
|
|
|
|
|
|
|
|
if (cvalue > 0xffff)
|
|
|
|
bytes = 4;
|
|
|
|
else if (cvalue > 0x07ff)
|
|
|
|
bytes = 3;
|
|
|
|
else
|
|
|
|
bytes = 2;
|
|
|
|
|
|
|
|
result = (text *) palloc(VARHDRSZ + bytes);
|
|
|
|
SET_VARSIZE(result, VARHDRSZ + bytes);
|
2014-05-16 22:51:28 +02:00
|
|
|
wch = (unsigned char *) VARDATA(result);
|
2007-09-18 19:41:17 +02:00
|
|
|
|
|
|
|
if (bytes == 2)
|
|
|
|
{
|
|
|
|
wch[0] = 0xC0 | ((cvalue >> 6) & 0x1F);
|
2015-03-31 14:12:27 +02:00
|
|
|
wch[1] = 0x80 | (cvalue & 0x3F);
|
2007-09-18 19:41:17 +02:00
|
|
|
}
|
|
|
|
else if (bytes == 3)
|
|
|
|
{
|
|
|
|
wch[0] = 0xE0 | ((cvalue >> 12) & 0x0F);
|
|
|
|
wch[1] = 0x80 | ((cvalue >> 6) & 0x3F);
|
|
|
|
wch[2] = 0x80 | (cvalue & 0x3F);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wch[0] = 0xF0 | ((cvalue >> 18) & 0x07);
|
|
|
|
wch[1] = 0x80 | ((cvalue >> 12) & 0x3F);
|
|
|
|
wch[2] = 0x80 | ((cvalue >> 6) & 0x3F);
|
|
|
|
wch[3] = 0x80 | (cvalue & 0x3F);
|
|
|
|
}
|
2007-11-15 22:14:46 +01:00
|
|
|
|
2014-05-16 22:51:28 +02:00
|
|
|
/*
|
|
|
|
* The preceding range check isn't sufficient, because UTF8 excludes
|
|
|
|
* Unicode "surrogate pair" codes. Make sure what we created is valid
|
|
|
|
* UTF8.
|
|
|
|
*/
|
|
|
|
if (!pg_utf8_islegal(wch, bytes))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("requested character not valid for encoding: %d",
|
|
|
|
cvalue)));
|
2007-09-18 19:41:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
bool is_mb;
|
2007-09-18 19:41:17 +02:00
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
/*
|
|
|
|
* Error out on arguments that make no sense or that we can't validly
|
|
|
|
* represent in the encoding.
|
2007-09-18 19:41:17 +02:00
|
|
|
*/
|
|
|
|
if (cvalue == 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("null character not permitted")));
|
|
|
|
|
|
|
|
is_mb = pg_encoding_max_length(encoding) > 1;
|
|
|
|
|
2007-12-18 19:01:48 +01:00
|
|
|
if ((is_mb && (cvalue > 127)) || (!is_mb && (cvalue > 255)))
|
2007-09-18 19:41:17 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("requested character too large for encoding: %d",
|
|
|
|
cvalue)));
|
2007-11-15 22:14:46 +01:00
|
|
|
|
2007-09-18 19:41:17 +02:00
|
|
|
result = (text *) palloc(VARHDRSZ + 1);
|
|
|
|
SET_VARSIZE(result, VARHDRSZ + 1);
|
|
|
|
*VARDATA(result) = (char) cvalue;
|
|
|
|
}
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2000-09-25 14:58:47 +02:00
|
|
|
/********************************************************************
|
|
|
|
*
|
|
|
|
* repeat
|
|
|
|
*
|
|
|
|
* Syntax:
|
|
|
|
*
|
|
|
|
* text repeat(text string, int val)
|
|
|
|
*
|
|
|
|
* Purpose:
|
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Repeat string by val.
|
2000-09-25 14:58:47 +02:00
|
|
|
*
|
|
|
|
********************************************************************/
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
Datum
|
|
|
|
repeat(PG_FUNCTION_ARGS)
|
2000-04-07 15:40:45 +02:00
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
text *string = PG_GETARG_TEXT_PP(0);
|
2001-03-22 05:01:46 +01:00
|
|
|
int32 count = PG_GETARG_INT32(1);
|
|
|
|
text *result;
|
|
|
|
int slen,
|
|
|
|
tlen;
|
|
|
|
int i;
|
2007-09-22 00:52:52 +02:00
|
|
|
char *cp,
|
|
|
|
*sp;
|
2000-04-07 15:40:45 +02:00
|
|
|
|
|
|
|
if (count < 0)
|
|
|
|
count = 0;
|
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
slen = VARSIZE_ANY_EXHDR(string);
|
|
|
|
tlen = VARHDRSZ + (count * slen);
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2002-08-22 06:54:20 +02:00
|
|
|
/* Check for integer overflow */
|
2002-08-22 07:05:19 +02:00
|
|
|
if (slen != 0 && count != 0)
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
int check = count * slen;
|
|
|
|
int check2 = check + VARHDRSZ;
|
2002-08-22 07:05:19 +02:00
|
|
|
|
|
|
|
if ((check / slen) != count || check2 <= check)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("requested length too large")));
|
2002-08-22 07:05:19 +02:00
|
|
|
}
|
2002-08-22 06:54:20 +02:00
|
|
|
|
2000-04-07 15:40:45 +02:00
|
|
|
result = (text *) palloc(tlen);
|
|
|
|
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(result, tlen);
|
2000-04-07 15:40:45 +02:00
|
|
|
cp = VARDATA(result);
|
2007-09-22 00:52:52 +02:00
|
|
|
sp = VARDATA_ANY(string);
|
2000-04-07 15:40:45 +02:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
memcpy(cp, sp, slen);
|
2000-04-07 15:40:45 +02:00
|
|
|
cp += slen;
|
|
|
|
}
|
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|