This is a set of single row character functions, defined for the datatype

text, which are supposed to behave exactly as their Oracle counterparts.

From: Edmund Mergl <E.Mergl@bawue.de>
This commit is contained in:
Marc G. Fournier 1997-03-04 05:32:26 +00:00
parent 497e3c9b5e
commit 83978e1ea7
4 changed files with 436 additions and 6 deletions

View File

@ -4,7 +4,7 @@
# Makefile for utils/adt
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.3 1996/11/06 10:30:35 scrappy Exp $
# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.4 1997/03/04 05:32:04 scrappy Exp $
#
#-------------------------------------------------------------------------
@ -20,7 +20,8 @@ CFLAGS+=$(INCLUDE_OPT)
OBJS = acl.o arrayfuncs.o arrayutils.o bool.o char.o chunk.o date.o \
datum.o dt.o filename.o float.o geo-ops.o geo-selfuncs.o int.o \
misc.o nabstime.o name.o not_in.o numutils.o oid.o \
oidname.o oidint2.o oidint4.o regexp.o regproc.o selfuncs.o \
oidname.o oidint2.o oidint4.o oracle_compat.o \
regexp.o regproc.o selfuncs.o \
tid.o varchar.o varlena.o sets.o datetimes.o like.o
all: SUBSYS.o

View File

@ -0,0 +1,420 @@
/*
* Edmund Mergl <E.Mergl@bawue.de>
*
* $Id: oracle_compat.c,v 1.1 1997/03/04 05:32:06 scrappy Exp $
*
*/
#include <ctype.h>
#include "postgres.h"
/********************************************************************
*
* lower
*
* Syntax:
*
* text *lower(text *string)
*
* Purpose:
*
* Returns string, with all letters forced to lowercase.
*
********************************************************************/
text *
lower(text *string)
{
char *ptr;
int m;
m = VARSIZE(string) - VARHDRSZ;
if (m <= 0) {
return string;
}
ptr = VARDATA(string);
while (m--) {
*ptr = tolower(*ptr);
++ptr;
}
return string;
}
/********************************************************************
*
* upper
*
* Syntax:
*
* text *upper(text *string)
*
* Purpose:
*
* Returns string, with all letters forced to uppercase.
*
********************************************************************/
text *
upper(text *string)
{
char *ptr;
int m;
m = VARSIZE(string) - VARHDRSZ;
if (m <= 0) {
return string;
}
ptr = VARDATA(string);
while (m--) {
*ptr = toupper(*ptr);
++ptr;
}
return string;
}
/********************************************************************
*
* initcap
*
* Syntax:
*
* text *initcap(text *string)
*
* Purpose:
*
* Returns string, with first letter of each word in uppercase,
* all other letters in lowercase. A word is delimited by white
* space.
*
********************************************************************/
text *
initcap(text *string)
{
char *ptr;
int m;
m = VARSIZE(string) - VARHDRSZ;
if (m <= 0) {
return string;
}
ptr = VARDATA(string);
*ptr = toupper(*ptr);
++ptr;
--m;
while (m--) {
if (*(ptr - 1) == ' ' || *(ptr - 1) == ' ') {
*ptr = toupper(*ptr);
} else {
*ptr = tolower(*ptr);
}
++ptr;
}
return string;
}
/********************************************************************
*
* lpad
*
* Syntax:
*
* text *lpad(text *string1, int4 len, text *string2)
*
* Purpose:
*
* Returns string1, left-padded to length len with the sequence of
* characters in string2.
*
********************************************************************/
text *
lpad(text *string1, int4 len, text *string2)
{
text *ret;
char *ptr, *ptr1, *ptr2;
int m, n;
m = len - VARSIZE(string1) + VARHDRSZ;
if (m <= 0 || (VARSIZE(string2) - VARHDRSZ) <= 0) {
return string1;
}
ret = (text *)malloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
ptr = VARDATA(ret);
ptr2 = VARDATA(string2);
while (m--) {
*ptr++ = *ptr2;
ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2;
}
n = VARSIZE(string1) - VARHDRSZ;
ptr1 = VARDATA(string1);
while (n--) {
*ptr++ = *ptr1++;
}
return ret;
}
/********************************************************************
*
* rpad
*
* Syntax:
*
* text *rpad(text *string1, int4 len, text *string2)
*
* Purpose:
*
* Returns string1, right-padded to length len with the sequence of
* characters in string2.
*
********************************************************************/
text *
rpad(text *string1, int4 len, text *string2)
{
text *ret;
char *ptr, *ptr1, *ptr2;
int m, n;
m = len - VARSIZE(string1) + VARHDRSZ;
if (m <= 0 || (VARSIZE(string2) - VARHDRSZ) <= 0) {
return string1;
}
ret = (text *)malloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
n = VARSIZE(string1) - VARHDRSZ;
ptr = VARDATA(ret);
ptr1 = VARDATA(string1);
while (n--) {
*ptr++ = *ptr1++;
}
ptr2 = VARDATA(string2);
while (m--) {
*ptr++ = *ptr2;
ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2;
}
return ret;
}
/********************************************************************
*
* ltrim
*
* Syntax:
*
* text *ltrim(text *string, text *set)
*
* Purpose:
*
* Returns string with initial characters removed up to the first
* character not in set.
*
********************************************************************/
text *
ltrim(text *string, text *set)
{
char *ptr, *ptr2, *end2;
int m;
m = VARSIZE(string) - VARHDRSZ;
if (m <= 0 || 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;
}
if (*ptr != *ptr2) {
break;
}
++ptr;
ptr2 = VARDATA(set);
}
m = VARDATA(string) + VARSIZE(string) - VARHDRSZ - ptr + 1;
ptr2 = VARDATA(string);
while (m--) {
*ptr2++ = *ptr++;
}
VARSIZE(string) -= ptr - ptr2;
return string;
}
/********************************************************************
*
* rtrim
*
* Syntax:
*
* text *rtrim(text *string, text *set)
*
* Purpose:
*
* Returns string with final characters removed after the last
* character not in set.
*
********************************************************************/
text *
rtrim(text *string, text *set)
{
char *ptr, *ptr2, *end2;
int m;
m = VARSIZE(string) - VARHDRSZ;
if (m <= 0 || 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;
}
if (*ptr != *ptr2) {
break;
}
--ptr;
ptr2 = VARDATA(set);
}
VARSIZE(string) -= VARDATA(string) + VARSIZE(string) - VARHDRSZ - 1 - ptr;
return string;
}
/********************************************************************
*
* substr
*
* Syntax:
*
* text *substr(text *string, int4 m, int4 n)
*
* Purpose:
*
* Returns a portion of string, beginning at character m, n
* characters long. The first position of string is 1.
*
********************************************************************/
text *
substr(text *string, int4 m, int4 n)
{
char *ptr, *ptr1;
int len;
len = VARSIZE(string) - VARHDRSZ - m;
if (m <= 0 || n <= 0 || len <= 0) {
return string;
}
ptr = VARDATA(string);
ptr1 = ptr + m - 1;
len = len + 1 < n ? len + 1 : n;
while (len--) {
*ptr++ = *ptr1++;
}
VARSIZE(string) -= ptr1 - ptr;
return string;
}
/********************************************************************
*
* translate
*
* Syntax:
*
* text *translate(text *string, char from, char to)
*
* Purpose:
*
* Returns string after replacing all occurences of from with
* the corresponding character in to. TRANSLATE will not remove
* characters.
*
********************************************************************/
text *
translate(text *string, char from, char to)
{
char *ptr;
int m;
m = VARSIZE(string) - VARHDRSZ;
ptr = VARDATA(string);
while (m--) {
if (*ptr == from) {
*ptr = to;
}
++ptr;
}
return string;
}
/* EOF */

View File

@ -6,7 +6,7 @@
*
* Copyright (c) 1994, Regents of the University of California
*
* $Id: pg_proc.h,v 1.7 1996/11/14 21:39:14 scrappy Exp $
* $Id: pg_proc.h,v 1.8 1997/03/04 05:32:11 scrappy Exp $
*
* NOTES
* The script catalog/genbki.sh reads this file and generates .bki
@ -758,6 +758,17 @@ DATA(insert OID = 1239 ( texticregexne PGUID 11 f t f 2 f 16 "25 25" 100
DATA(insert OID = 1240 ( nameicregexeq PGUID 11 f t f 2 f 16 "19 25" 100 0 0 100 foo bar ));
DATA(insert OID = 1241 ( nameicregexne PGUID 11 f t f 2 f 16 "19 25" 100 0 0 100 foo bar ));
/* Oracle Compatibility Related Functions - By Edmund Mergl <E.Mergl@bawue.de> */
DATA(insert OID = 1260 ( lower PGUID 11 f t f 1 f 25 "25" 100 0 0 100 foo bar ));
DATA(insert OID = 1261 ( upper PGUID 11 f t f 1 f 25 "25" 100 0 0 100 foo bar ));
DATA(insert OID = 1262 ( initcap PGUID 11 f t f 1 f 25 "25" 100 0 0 100 foo bar ));
DATA(insert OID = 1263 ( lpad PGUID 11 f t f 3 f 25 "25 23 25" 100 0 0 100 foo bar ));
DATA(insert OID = 1264 ( rpad PGUID 11 f t f 3 f 25 "25 23 25" 100 0 0 100 foo bar ));
DATA(insert OID = 1265 ( ltrim PGUID 11 f t f 2 f 25 "25 25" 100 0 0 100 foo bar ));
DATA(insert OID = 1266 ( rtrim PGUID 11 f t f 2 f 25 "25 25" 100 0 0 100 foo bar ));
DATA(insert OID = 1267 ( substr PGUID 11 f t f 3 f 25 "25 23 23" 100 0 0 100 foo bar ));
DATA(insert OID = 1268 ( translate PGUID 11 f t f 3 f 25 "25 18 18" 100 0 0 100 foo bar ));
/*
* prototypes for functions pg_proc.c
*/

View File

@ -15,7 +15,7 @@
*
* Copyright (c) 1994, Regents of the University of California
*
* $Id: memutils.h,v 1.4 1996/11/04 04:00:48 momjian Exp $
* $Id: memutils.h,v 1.5 1997/03/04 05:32:26 scrappy Exp $
*
* NOTES
* some of the information in this file will be moved to
@ -244,8 +244,6 @@ extern void AllocSetDump(AllocSet set);
*/
typedef int LibCCopyLength;
typedef CLibCopyLength;
/*
* MemoryCopy --
* Copies fixed length block of memory to another.