Commit e8466b03 authored by Marc G. Fournier's avatar Marc G. Fournier

Update oracle_compat.c

parent 064466ad
/* /*
* Edmund Mergl <E.Mergl@bawue.de> * Edmund Mergl <E.Mergl@bawue.de>
* *
* $Id: oracle_compat.c,v 1.2 1997/03/09 20:40:58 momjian Exp $ * $Id: oracle_compat.c,v 1.3 1997/03/15 06:00:19 scrappy Exp $
* *
*/ */
...@@ -9,15 +9,14 @@ ...@@ -9,15 +9,14 @@
#include <ctype.h> #include <ctype.h>
#include "postgres.h" #include "postgres.h"
#include "utils/builtins.h" /* where function declarations go */
/******************************************************************** /********************************************************************
* *
* lower * LOWER
* *
* Syntax: * Syntax:
* *
* text *lower(text *string) * text *LOWER(text *string)
* *
* Purpose: * Purpose:
* *
...@@ -26,9 +25,10 @@ ...@@ -26,9 +25,10 @@
********************************************************************/ ********************************************************************/
text * text *
lower(text *string) LOWER(text *string)
{ {
char *ptr; text *ret;
char *ptr, *ptr_ret;
int m; int m;
m = VARSIZE(string) - VARHDRSZ; m = VARSIZE(string) - VARHDRSZ;
...@@ -37,24 +37,27 @@ lower(text *string) ...@@ -37,24 +37,27 @@ lower(text *string)
return string; return string;
} }
ret = (text *)malloc(VARSIZE(string));
VARSIZE(ret) = VARSIZE(string);
ptr = VARDATA(string); ptr = VARDATA(string);
ptr_ret = VARDATA(ret);
while (m--) { while (m--) {
*ptr = tolower(*ptr); *ptr_ret++ = tolower(*ptr++);
++ptr;
} }
return string; return ret;
} }
/******************************************************************** /********************************************************************
* *
* upper * UPPER
* *
* Syntax: * Syntax:
* *
* text *upper(text *string) * text *UPPER(text *string)
* *
* Purpose: * Purpose:
* *
...@@ -63,9 +66,10 @@ lower(text *string) ...@@ -63,9 +66,10 @@ lower(text *string)
********************************************************************/ ********************************************************************/
text * text *
upper(text *string) UPPER(text *string)
{ {
char *ptr; text *ret;
char *ptr, *ptr_ret;
int m; int m;
m = VARSIZE(string) - VARHDRSZ; m = VARSIZE(string) - VARHDRSZ;
...@@ -74,24 +78,27 @@ upper(text *string) ...@@ -74,24 +78,27 @@ upper(text *string)
return string; return string;
} }
ret = (text *)malloc(VARSIZE(string));
VARSIZE(ret) = VARSIZE(string);
ptr = VARDATA(string); ptr = VARDATA(string);
ptr_ret = VARDATA(ret);
while (m--) { while (m--) {
*ptr = toupper(*ptr); *ptr_ret++ = toupper(*ptr++);
++ptr;
} }
return string; return ret;
} }
/******************************************************************** /********************************************************************
* *
* initcap * INITCAP
* *
* Syntax: * Syntax:
* *
* text *initcap(text *string) * text *INITCAP(text *string)
* *
* Purpose: * Purpose:
* *
...@@ -102,9 +109,10 @@ upper(text *string) ...@@ -102,9 +109,10 @@ upper(text *string)
********************************************************************/ ********************************************************************/
text * text *
initcap(text *string) INITCAP(text *string)
{ {
char *ptr; text *ret;
char *ptr, *ptr_ret;
int m; int m;
m = VARSIZE(string) - VARHDRSZ; m = VARSIZE(string) - VARHDRSZ;
...@@ -113,32 +121,34 @@ initcap(text *string) ...@@ -113,32 +121,34 @@ initcap(text *string)
return string; return string;
} }
ret = (text *)malloc(VARSIZE(string));
VARSIZE(ret) = VARSIZE(string);
ptr = VARDATA(string); ptr = VARDATA(string);
ptr_ret = VARDATA(ret);
*ptr = toupper(*ptr); *ptr_ret++ = toupper(*ptr++);
++ptr;
--m; --m;
while (m--) { while (m--) {
if (*(ptr - 1) == ' ' || *(ptr - 1) == ' ') { if (*(ptr_ret - 1) == ' ' || *(ptr_ret - 1) == ' ') {
*ptr = toupper(*ptr); *ptr_ret++ = toupper(*ptr++);
} else { } else {
*ptr = tolower(*ptr); *ptr_ret++ = tolower(*ptr++);
} }
++ptr;
} }
return string; return ret;
} }
/******************************************************************** /********************************************************************
* *
* lpad * LPAD
* *
* Syntax: * Syntax:
* *
* text *lpad(text *string1, int4 len, text *string2) * text *LPAD(text *string1, int4 len, text *string2)
* *
* Purpose: * Purpose:
* *
...@@ -148,10 +158,10 @@ initcap(text *string) ...@@ -148,10 +158,10 @@ initcap(text *string)
********************************************************************/ ********************************************************************/
text * text *
lpad(text *string1, int4 len, text *string2) LPAD(text *string1, int4 len, text *string2)
{ {
text *ret; text *ret;
char *ptr, *ptr1, *ptr2; char *ptr1, *ptr2, *ptr_ret;
int m, n; int m, n;
m = len - VARSIZE(string1) + VARHDRSZ; m = len - VARSIZE(string1) + VARHDRSZ;
...@@ -163,11 +173,11 @@ lpad(text *string1, int4 len, text *string2) ...@@ -163,11 +173,11 @@ lpad(text *string1, int4 len, text *string2)
ret = (text *)malloc(VARHDRSZ + len); ret = (text *)malloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len; VARSIZE(ret) = VARHDRSZ + len;
ptr = VARDATA(ret);
ptr2 = VARDATA(string2); ptr2 = VARDATA(string2);
ptr_ret = VARDATA(ret);
while (m--) { while (m--) {
*ptr++ = *ptr2; *ptr_ret++ = *ptr2;
ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2; ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2;
} }
...@@ -175,7 +185,7 @@ lpad(text *string1, int4 len, text *string2) ...@@ -175,7 +185,7 @@ lpad(text *string1, int4 len, text *string2)
ptr1 = VARDATA(string1); ptr1 = VARDATA(string1);
while (n--) { while (n--) {
*ptr++ = *ptr1++; *ptr_ret++ = *ptr1++;
} }
return ret; return ret;
...@@ -184,11 +194,11 @@ lpad(text *string1, int4 len, text *string2) ...@@ -184,11 +194,11 @@ lpad(text *string1, int4 len, text *string2)
/******************************************************************** /********************************************************************
* *
* rpad * RPAD
* *
* Syntax: * Syntax:
* *
* text *rpad(text *string1, int4 len, text *string2) * text *RPAD(text *string1, int4 len, text *string2)
* *
* Purpose: * Purpose:
* *
...@@ -198,10 +208,10 @@ lpad(text *string1, int4 len, text *string2) ...@@ -198,10 +208,10 @@ lpad(text *string1, int4 len, text *string2)
********************************************************************/ ********************************************************************/
text * text *
rpad(text *string1, int4 len, text *string2) RPAD(text *string1, int4 len, text *string2)
{ {
text *ret; text *ret;
char *ptr, *ptr1, *ptr2; char *ptr1, *ptr2, *ptr_ret;
int m, n; int m, n;
m = len - VARSIZE(string1) + VARHDRSZ; m = len - VARSIZE(string1) + VARHDRSZ;
...@@ -214,17 +224,17 @@ rpad(text *string1, int4 len, text *string2) ...@@ -214,17 +224,17 @@ rpad(text *string1, int4 len, text *string2)
VARSIZE(ret) = VARHDRSZ + len; VARSIZE(ret) = VARHDRSZ + len;
n = VARSIZE(string1) - VARHDRSZ; n = VARSIZE(string1) - VARHDRSZ;
ptr = VARDATA(ret);
ptr1 = VARDATA(string1); ptr1 = VARDATA(string1);
ptr_ret = VARDATA(ret);
while (n--) { while (n--) {
*ptr++ = *ptr1++; *ptr_ret++ = *ptr1++;
} }
ptr2 = VARDATA(string2); ptr2 = VARDATA(string2);
while (m--) { while (m--) {
*ptr++ = *ptr2; *ptr_ret++ = *ptr2;
ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2; ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2;
} }
...@@ -234,11 +244,11 @@ rpad(text *string1, int4 len, text *string2) ...@@ -234,11 +244,11 @@ rpad(text *string1, int4 len, text *string2)
/******************************************************************** /********************************************************************
* *
* ltrim * LTRIM
* *
* Syntax: * Syntax:
* *
* text *ltrim(text *string, text *set) * text *LTRIM(text *string, text *set)
* *
* Purpose: * Purpose:
* *
...@@ -248,9 +258,10 @@ rpad(text *string1, int4 len, text *string2) ...@@ -248,9 +258,10 @@ rpad(text *string1, int4 len, text *string2)
********************************************************************/ ********************************************************************/
text * text *
ltrim(text *string, text *set) LTRIM(text *string, text *set)
{ {
char *ptr, *ptr2, *end2; text *ret;
char *ptr, *ptr2, *end2, *ptr_ret;
int m; int m;
m = VARSIZE(string) - VARHDRSZ; m = VARSIZE(string) - VARHDRSZ;
...@@ -273,30 +284,32 @@ ltrim(text *string, text *set) ...@@ -273,30 +284,32 @@ ltrim(text *string, text *set)
if (*ptr != *ptr2) { if (*ptr != *ptr2) {
break; break;
} }
++ptr; ptr++;
ptr2 = VARDATA(set); ptr2 = VARDATA(set);
} }
m = VARDATA(string) + VARSIZE(string) - VARHDRSZ - ptr + 1; ++m;
ptr2 = VARDATA(string);
ret = (text *)malloc(VARHDRSZ + m);
VARSIZE(ret) = VARHDRSZ + m;
ptr_ret = VARDATA(ret);
while (m--) { while (m--) {
*ptr2++ = *ptr++; *ptr_ret++ = *ptr++;
} }
VARSIZE(string) -= ptr - ptr2; return ret;
return string;
} }
/******************************************************************** /********************************************************************
* *
* rtrim * RTRIM
* *
* Syntax: * Syntax:
* *
* text *rtrim(text *string, text *set) * text *RTRIM(text *string, text *set)
* *
* Purpose: * Purpose:
* *
...@@ -306,9 +319,10 @@ ltrim(text *string, text *set) ...@@ -306,9 +319,10 @@ ltrim(text *string, text *set)
********************************************************************/ ********************************************************************/
text * text *
rtrim(text *string, text *set) RTRIM(text *string, text *set)
{ {
char *ptr, *ptr2, *end2; text *ret;
char *ptr, *ptr2, *end2, *ptr_ret;
int m; int m;
m = VARSIZE(string) - VARHDRSZ; m = VARSIZE(string) - VARHDRSZ;
...@@ -335,19 +349,28 @@ rtrim(text *string, text *set) ...@@ -335,19 +349,28 @@ rtrim(text *string, text *set)
ptr2 = VARDATA(set); ptr2 = VARDATA(set);
} }
VARSIZE(string) -= VARDATA(string) + VARSIZE(string) - VARHDRSZ - 1 - ptr; ++m;
return string; ret = (text *)malloc(VARHDRSZ + m);
VARSIZE(ret) = VARHDRSZ + m;
ptr_ret = VARDATA(ret) + m - 1;
while (m--) {
*ptr_ret-- = *ptr--;
}
return ret;
} }
/******************************************************************** /********************************************************************
* *
* substr * SUBSTR
* *
* Syntax: * Syntax:
* *
* text *substr(text *string, int4 m, int4 n) * text *SUBSTR(text *string, int4 m, int4 n)
* *
* Purpose: * Purpose:
* *
...@@ -357,9 +380,10 @@ rtrim(text *string, text *set) ...@@ -357,9 +380,10 @@ rtrim(text *string, text *set)
********************************************************************/ ********************************************************************/
text * text *
substr(text *string, int4 m, int4 n) SUBSTR(text *string, int4 m, int4 n)
{ {
char *ptr, *ptr1; text *ret;
char *ptr, *ptr_ret;
int len; int len;
len = VARSIZE(string) - VARHDRSZ - m; len = VARSIZE(string) - VARHDRSZ - m;
...@@ -368,27 +392,29 @@ substr(text *string, int4 m, int4 n) ...@@ -368,27 +392,29 @@ substr(text *string, int4 m, int4 n)
return string; return string;
} }
ptr = VARDATA(string);
ptr1 = ptr + m - 1;
len = len + 1 < n ? len + 1 : n; len = len + 1 < n ? len + 1 : n;
ret = (text *)malloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
ptr = VARDATA(string) + m - 1;
ptr_ret = VARDATA(ret);
while (len--) { while (len--) {
*ptr++ = *ptr1++; *ptr_ret++ = *ptr++;
} }
VARSIZE(string) -= ptr1 - ptr; return ret;
return string;
} }
/******************************************************************** /********************************************************************
* *
* translate * TRANSLATE
* *
* Syntax: * Syntax:
* *
* text *translate(text *string, char from, char to) * text *TRANSLATE(text *string, char from, char to)
* *
* Purpose: * Purpose:
* *
...@@ -399,22 +425,277 @@ substr(text *string, int4 m, int4 n) ...@@ -399,22 +425,277 @@ substr(text *string, int4 m, int4 n)
********************************************************************/ ********************************************************************/
text * text *
translate(text *string, char from, char to) TRANSLATE(text *string, char from, char to)
{ {
char *ptr; text *ret;
char *ptr, *ptr_ret;
int m; int m;
m = VARSIZE(string) - VARHDRSZ; m = VARSIZE(string) - VARHDRSZ;
if (m <= 0) {
return string;
}
ret = (text *)malloc(VARSIZE(string));
VARSIZE(ret) = VARSIZE(string);
ptr = VARDATA(string); ptr = VARDATA(string);
ptr_ret = VARDATA(ret);
while (m--) {
*ptr_ret++ = *ptr == from ? to : *ptr;
ptr++;
}
return ret;
}
/********************************************************************
*
* Functions in lower case are the equivalents with the last
* parameter beeing set to a reasonable default.
*
********************************************************************/
/********************************************************************
*
* lpad
*
* Syntax:
*
* text *lpad(text *string1, int4 len)
*
* Purpose:
*
* Returns string1, left-padded to length len with blanks.
*
********************************************************************/
text *
lpad(text *string1, int4 len)
{
text *ret;
char *ptr1, *ptr_ret;
int m, n;
m = len - VARSIZE(string1) + VARHDRSZ;
if (m <= 0) {
return string1;
}
ret = (text *)malloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
ptr_ret = VARDATA(ret);
while (m--) { while (m--) {
if (*ptr == from) { *ptr_ret++ = ' ';
*ptr = to; }
n = VARSIZE(string1) - VARHDRSZ;
ptr1 = VARDATA(string1);
while (n--) {
*ptr_ret++ = *ptr1++;
}
return ret;
}
/********************************************************************
*
* rpad
*
* Syntax:
*
* text *rpad(text *string1, int4 len)
*
* Purpose:
*
* Returns string1, right-padded to length len with blanks.
*
********************************************************************/
text *
rpad(text *string1, int4 len)
{
text *ret;
char *ptr1, *ptr_ret;
int m, n;
m = len - VARSIZE(string1) + VARHDRSZ;
if (m <= 0) {
return string1;
}
ret = (text *)malloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
n = VARSIZE(string1) - VARHDRSZ;
ptr1 = VARDATA(string1);
ptr_ret = VARDATA(ret);
while (n--) {
*ptr_ret++ = *ptr1++;
}
while (m--) {
*ptr_ret++ = ' ';
}
return ret;
}
/********************************************************************
*
* ltrim
*
* Syntax:
*
* text *ltrim(text *string)
*
* Purpose:
*
* Returns string with initial blanks removed.
*
********************************************************************/
text *
ltrim(text *string)
{
text *ret;
char *ptr, *ptr_ret;
int m;
m = VARSIZE(string) - VARHDRSZ;
if (m <= 0) {
return string;
}
ptr = VARDATA(string);
while (m--) {
if (*ptr != ' ') {
break;
} }
++ptr; ++ptr;
} }
++m;
ret = (text *)malloc(VARHDRSZ + m);
VARSIZE(ret) = VARHDRSZ + m;
ptr_ret = VARDATA(ret);
while (m--) {
*ptr_ret++ = *ptr++;
}
return ret;
}
/********************************************************************
*
* rtrim
*
* Syntax:
*
* text *rtrim(text *string)
*
* Purpose:
*
* Returns string with final blanks removed.
*
********************************************************************/
text *
rtrim(text *string)
{
text *ret;
char *ptr, *ptr_ret;
int m;
m = VARSIZE(string) - VARHDRSZ;
if (m <= 0) {
return string;
}
ptr = VARDATA(string) + VARSIZE(string) - VARHDRSZ - 1;
while (m--) {
if (*ptr != ' ') {
break;
}
--ptr;
}
++m;
ret = (text *)malloc(VARHDRSZ + m);
VARSIZE(ret) = VARHDRSZ + m;
ptr_ret = VARDATA(ret) + m - 1;
while (m--) {
*ptr_ret-- = *ptr--;
}
return ret;
}
/********************************************************************
*
* substr
*
* Syntax:
*
* text *substr(text *string, int4 m)
*
* Purpose:
*
* Returns a portion of string, beginning at character m, up to
* the end of string. The first position of string is 1.
*
********************************************************************/
text *
substr(text *string, int4 m)
{
text *ret;
char *ptr, *ptr_ret;
int len;
len = VARSIZE(string) - VARHDRSZ - m;
if (m <= 0 || len <= 0) {
return string; return string;
}
len = len + 1;
ret = (text *)malloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
ptr = VARDATA(string) + m - 1;
ptr_ret = VARDATA(ret);
while (len--) {
*ptr_ret++ = *ptr++;
}
return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment