Commit 37f17d39 authored by Thomas G. Lockhart's avatar Thomas G. Lockhart

Define text_substr().

Change declarations from "struct varlena *" to "text *".
Remove register variables since compiler should do better on its own.
parent a343b2e7
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.25 1997/12/16 15:59:11 thomas Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.26 1998/01/01 05:50:50 thomas Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -37,13 +37,13 @@ ...@@ -37,13 +37,13 @@
* The input is scaned twice. * The input is scaned twice.
* The error checking of input is minimal. * The error checking of input is minimal.
*/ */
struct varlena * text *
byteain(char *inputText) byteain(char *inputText)
{ {
register char *tp; char *tp;
register char *rp; char *rp;
register int byte; int byte;
struct varlena *result; text *result;
if (inputText == NULL) if (inputText == NULL)
elog(WARN, "Bad input string for type bytea"); elog(WARN, "Bad input string for type bytea");
...@@ -60,7 +60,7 @@ byteain(char *inputText) ...@@ -60,7 +60,7 @@ byteain(char *inputText)
} }
tp = inputText; tp = inputText;
byte += VARHDRSZ; byte += VARHDRSZ;
result = (struct varlena *) palloc(byte); result = (text *) palloc(byte);
result->vl_len = byte; /* varlena? */ result->vl_len = byte; /* varlena? */
rp = result->vl_dat; rp = result->vl_dat;
while (*tp != '\0') while (*tp != '\0')
...@@ -77,28 +77,6 @@ byteain(char *inputText) ...@@ -77,28 +77,6 @@ byteain(char *inputText)
return (result); return (result);
} }
/*
* Shoves a bunch of memory pointed at by bytes into varlena.
* BUGS: Extremely unportable as things shoved can be string
* representations of structs, etc.
*/
#ifdef NOT_USED
struct varlena *
shove_bytes(unsigned char *stuff, int len)
{
struct varlena *result;
result = (struct varlena *) palloc(len + VARHDRSZ);
result->vl_len = len;
memmove(result->vl_dat,
stuff + VARHDRSZ,
len - VARHDRSZ);
return (result);
}
#endif
/* /*
* byteaout - converts to printable representation of byte array * byteaout - converts to printable representation of byte array
* *
...@@ -107,15 +85,16 @@ shove_bytes(unsigned char *stuff, int len) ...@@ -107,15 +85,16 @@ shove_bytes(unsigned char *stuff, int len)
* *
* NULL vlena should be an error--returning string with NULL for now. * NULL vlena should be an error--returning string with NULL for now.
*/ */
char * char *
byteaout(struct varlena * vlena) byteaout(text *vlena)
{ {
register char *vp; char *result;
register char *rp;
register int val; /* holds unprintable chars */ char *vp;
char *rp;
int val; /* holds unprintable chars */
int i; int i;
int len; int len;
static char *result;
if (vlena == NULL) if (vlena == NULL)
{ {
...@@ -164,17 +143,19 @@ byteaout(struct varlena * vlena) ...@@ -164,17 +143,19 @@ byteaout(struct varlena * vlena)
/* /*
* textin - converts "..." to internal representation * textin - converts "..." to internal representation
*/ */
struct varlena * text *
textin(char *inputText) textin(char *inputText)
{ {
struct varlena *result; text *result;
int len; int len;
if (inputText == NULL) if (inputText == NULL)
return (NULL); return (NULL);
len = strlen(inputText) + VARHDRSZ; len = strlen(inputText) + VARHDRSZ;
result = (struct varlena *) palloc(len); result = (text *) palloc(len);
VARSIZE(result) = len; VARSIZE(result) = len;
memmove(VARDATA(result), inputText, len - VARHDRSZ); memmove(VARDATA(result), inputText, len - VARHDRSZ);
return (result); return (result);
} }
...@@ -182,8 +163,8 @@ textin(char *inputText) ...@@ -182,8 +163,8 @@ textin(char *inputText)
/* /*
* textout - converts internal representation to "..." * textout - converts internal representation to "..."
*/ */
char * char *
textout(struct varlena * vlena) textout(text *vlena)
{ {
int len; int len;
char *result; char *result;
...@@ -207,10 +188,10 @@ textout(struct varlena * vlena) ...@@ -207,10 +188,10 @@ textout(struct varlena * vlena)
/* /*
* textlen - * textlen -
* returns the actual length of a text* (which may be less than * returns the actual length of a text*
* the VARSIZE of the text*) * (which is less than the VARSIZE of the text*)
*/ */
int int32
textlen(text *t) textlen(text *t)
{ {
if (!PointerIsValid(t)) if (!PointerIsValid(t))
...@@ -231,8 +212,7 @@ textlen(text *t) ...@@ -231,8 +212,7 @@ textlen(text *t)
* As in previous code, allow concatenation when one string is NULL. * As in previous code, allow concatenation when one string is NULL.
* Is this OK? * Is this OK?
*/ */
text *
text *
textcat(text *t1, text *t2) textcat(text *t1, text *t2)
{ {
int len1, int len1,
...@@ -269,7 +249,57 @@ textcat(text *t1, text *t2) ...@@ -269,7 +249,57 @@ textcat(text *t1, text *t2)
VARSIZE(result) = len; VARSIZE(result) = len;
return (result); return (result);
} /* textcat() */ } /* textcat() */
/*
* text_substr()
* Return a substring starting at the specified position.
* - thomas 1997-12-31
*
* Input:
* - string
* - starting position (is one-based)
* - string length
*
* If the starting position is zero or less, then return the entire string.
* XXX Note that this may not be the right behavior:
* if we are calculating the starting position we might want it to start at one.
* If the length is less than zero, return the remaining string.
*
* Note that the arguments operate on octet length,
* so not aware of multi-byte character sets.
*/
text *
text_substr(text *string, int32 m, int32 n)
{
text *ret;
int len;
if ((string == (text *) NULL) || (m <= 0))
return string;
len = VARSIZE(string) - VARHDRSZ;
/* m will now become a zero-based starting position */
if (m >= len)
{
m = 0;
n = 0;
}
else
{
m--;
if (((m+n) > len) || (n < 0))
n = (len-m);
}
ret = (text *) PALLOC(VARHDRSZ + n);
VARSIZE(ret) = VARHDRSZ + n;
memcpy(VARDATA(ret), VARDATA(string)+m, n);
return ret;
} /* text_substr() */
/* /*
* textpos - * textpos -
...@@ -278,7 +308,6 @@ textcat(text *t1, text *t2) ...@@ -278,7 +308,6 @@ textcat(text *t1, text *t2)
* Ref: A Guide To The SQL Standard, Date & Darwen, 1997 * Ref: A Guide To The SQL Standard, Date & Darwen, 1997
* - thomas 1997-07-27 * - thomas 1997-07-27
*/ */
int32 int32
textpos(text *t1, text *t2) textpos(text *t1, text *t2)
{ {
...@@ -312,17 +341,17 @@ textpos(text *t1, text *t2) ...@@ -312,17 +341,17 @@ textpos(text *t1, text *t2)
p1++; p1++;
}; };
return (pos); return (pos);
} /* textpos() */ } /* textpos() */
/* /*
* texteq - returns 1 iff arguments are equal * texteq - returns 1 iff arguments are equal
* textne - returns 1 iff arguments are not equal * textne - returns 1 iff arguments are not equal
*/ */
bool bool
texteq(struct varlena * arg1, struct varlena * arg2) texteq(text *arg1, text *arg2)
{ {
register int len; int len;
register char *a1p, char *a1p,
*a2p; *a2p;
if (arg1 == NULL || arg2 == NULL) if (arg1 == NULL || arg2 == NULL)
...@@ -342,10 +371,10 @@ texteq(struct varlena * arg1, struct varlena * arg2) ...@@ -342,10 +371,10 @@ texteq(struct varlena * arg1, struct varlena * arg2)
if (*a1p++ != *a2p++) if (*a1p++ != *a2p++)
return ((bool) 0); return ((bool) 0);
return ((bool) 1); return ((bool) 1);
} /* texteq() */ } /* texteq() */
bool bool
textne(struct varlena * arg1, struct varlena * arg2) textne(text *arg1, text *arg2)
{ {
return ((bool) !texteq(arg1, arg2)); return ((bool) !texteq(arg1, arg2));
} }
...@@ -358,7 +387,7 @@ textne(struct varlena * arg1, struct varlena * arg2) ...@@ -358,7 +387,7 @@ textne(struct varlena * arg1, struct varlena * arg2)
* but it appears that most routines (incl. this one) assume not! - tgl 97/04/07 * but it appears that most routines (incl. this one) assume not! - tgl 97/04/07
*/ */
bool bool
text_lt(struct varlena * arg1, struct varlena * arg2) text_lt(text *arg1, text *arg2)
{ {
bool result; bool result;
...@@ -404,7 +433,7 @@ text_lt(struct varlena * arg1, struct varlena * arg2) ...@@ -404,7 +433,7 @@ text_lt(struct varlena * arg1, struct varlena * arg2)
#endif #endif
return (result); return (result);
} /* text_lt() */ } /* text_lt() */
/* text_le() /* text_le()
* Comparison function for text strings. * Comparison function for text strings.
...@@ -414,7 +443,7 @@ text_lt(struct varlena * arg1, struct varlena * arg2) ...@@ -414,7 +443,7 @@ text_lt(struct varlena * arg1, struct varlena * arg2)
* but it appears that most routines (incl. this one) assume not! - tgl 97/04/07 * but it appears that most routines (incl. this one) assume not! - tgl 97/04/07
*/ */
bool bool
text_le(struct varlena * arg1, struct varlena * arg2) text_le(text *arg1, text *arg2)
{ {
bool result; bool result;
...@@ -460,16 +489,16 @@ text_le(struct varlena * arg1, struct varlena * arg2) ...@@ -460,16 +489,16 @@ text_le(struct varlena * arg1, struct varlena * arg2)
#endif #endif
return (result); return (result);
} /* text_le() */ } /* text_le() */
bool bool
text_gt(struct varlena * arg1, struct varlena * arg2) text_gt(text *arg1, text *arg2)
{ {
return ((bool) !text_le(arg1, arg2)); return ((bool) !text_le(arg1, arg2));
} }
bool bool
text_ge(struct varlena * arg1, struct varlena * arg2) text_ge(text *arg1, text *arg2)
{ {
return ((bool) !text_lt(arg1, arg2)); return ((bool) !text_lt(arg1, arg2));
} }
...@@ -481,9 +510,9 @@ text_ge(struct varlena * arg1, struct varlena * arg2) ...@@ -481,9 +510,9 @@ text_ge(struct varlena * arg1, struct varlena * arg2)
*------------------------------------------------------------- *-------------------------------------------------------------
*/ */
int32 int32
byteaGetSize(struct varlena * v) byteaGetSize(text *v)
{ {
register int len; int len;
len = v->vl_len - sizeof(v->vl_len); len = v->vl_len - sizeof(v->vl_len);
...@@ -499,7 +528,7 @@ byteaGetSize(struct varlena * v) ...@@ -499,7 +528,7 @@ byteaGetSize(struct varlena * v)
*------------------------------------------------------------- *-------------------------------------------------------------
*/ */
int32 int32
byteaGetByte(struct varlena * v, int32 n) byteaGetByte(text *v, int32 n)
{ {
int len; int len;
int byte; int byte;
...@@ -527,7 +556,7 @@ byteaGetByte(struct varlena * v, int32 n) ...@@ -527,7 +556,7 @@ byteaGetByte(struct varlena * v, int32 n)
*------------------------------------------------------------- *-------------------------------------------------------------
*/ */
int32 int32
byteaGetBit(struct varlena * v, int32 n) byteaGetBit(text *v, int32 n)
{ {
int byteNo, int byteNo,
bitNo; bitNo;
...@@ -556,11 +585,11 @@ byteaGetBit(struct varlena * v, int32 n) ...@@ -556,11 +585,11 @@ byteaGetBit(struct varlena * v, int32 n)
* *
*------------------------------------------------------------- *-------------------------------------------------------------
*/ */
struct varlena * text *
byteaSetByte(struct varlena * v, int32 n, int32 newByte) byteaSetByte(text *v, int32 n, int32 newByte)
{ {
int len; int len;
struct varlena *res; text *res;
len = byteaGetSize(v); len = byteaGetSize(v);
...@@ -574,7 +603,7 @@ byteaSetByte(struct varlena * v, int32 n, int32 newByte) ...@@ -574,7 +603,7 @@ byteaSetByte(struct varlena * v, int32 n, int32 newByte)
/* /*
* Make a copy of the original varlena. * Make a copy of the original varlena.
*/ */
res = (struct varlena *) palloc(VARSIZE(v)); res = (text *) palloc(VARSIZE(v));
if (res == NULL) if (res == NULL)
{ {
elog(WARN, "byteaSetByte: Out of memory (%d bytes requested)", elog(WARN, "byteaSetByte: Out of memory (%d bytes requested)",
...@@ -598,10 +627,10 @@ byteaSetByte(struct varlena * v, int32 n, int32 newByte) ...@@ -598,10 +627,10 @@ byteaSetByte(struct varlena * v, int32 n, int32 newByte)
* *
*------------------------------------------------------------- *-------------------------------------------------------------
*/ */
struct varlena * text *
byteaSetBit(struct varlena * v, int32 n, int32 newBit) byteaSetBit(text *v, int32 n, int32 newBit)
{ {
struct varlena *res; text *res;
int oldByte, int oldByte,
newByte; newByte;
int byteNo, int byteNo,
......
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