Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit37f17d3

Browse files
author
Thomas G. Lockhart
committed
Define text_substr().
Change declarations from "struct varlena *" to "text *".Remove register variables since compiler should do better on its own.
1 parenta343b2e commit37f17d3

File tree

1 file changed

+99
-70
lines changed

1 file changed

+99
-70
lines changed

‎src/backend/utils/adt/varlena.c

Lines changed: 99 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.25 1997/12/16 15:59:11 thomas Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.26 1998/01/01 05:50:50 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -37,13 +37,13 @@
3737
*The input is scaned twice.
3838
*The error checking of input is minimal.
3939
*/
40-
structvarlena*
40+
text*
4141
byteain(char*inputText)
4242
{
43-
registerchar*tp;
44-
registerchar*rp;
45-
registerintbyte;
46-
structvarlena*result;
43+
char*tp;
44+
char*rp;
45+
intbyte;
46+
text*result;
4747

4848
if (inputText==NULL)
4949
elog(WARN,"Bad input string for type bytea");
@@ -60,7 +60,7 @@ byteain(char *inputText)
6060
}
6161
tp=inputText;
6262
byte+=VARHDRSZ;
63-
result= (structvarlena*)palloc(byte);
63+
result= (text*)palloc(byte);
6464
result->vl_len=byte;/* varlena? */
6565
rp=result->vl_dat;
6666
while (*tp!='\0')
@@ -77,28 +77,6 @@ byteain(char *inputText)
7777
return (result);
7878
}
7979

80-
/*
81-
* Shoves a bunch of memory pointed at by bytes into varlena.
82-
* BUGS: Extremely unportable as things shoved can be string
83-
* representations of structs, etc.
84-
*/
85-
#ifdefNOT_USED
86-
structvarlena*
87-
shove_bytes(unsignedchar*stuff,intlen)
88-
{
89-
structvarlena*result;
90-
91-
result= (structvarlena*)palloc(len+VARHDRSZ);
92-
result->vl_len=len;
93-
memmove(result->vl_dat,
94-
stuff+VARHDRSZ,
95-
len-VARHDRSZ);
96-
return (result);
97-
}
98-
99-
#endif
100-
101-
10280
/*
10381
*byteaout- converts to printable representation of byte array
10482
*
@@ -107,15 +85,16 @@ shove_bytes(unsigned char *stuff, int len)
10785
*
10886
*NULL vlena should be an error--returning string with NULL for now.
10987
*/
110-
char*
111-
byteaout(structvarlena*vlena)
88+
char*
89+
byteaout(text*vlena)
11290
{
113-
registerchar*vp;
114-
registerchar*rp;
115-
registerintval;/* holds unprintable chars */
91+
char*result;
92+
93+
char*vp;
94+
char*rp;
95+
intval;/* holds unprintable chars */
11696
inti;
11797
intlen;
118-
staticchar*result;
11998

12099
if (vlena==NULL)
121100
{
@@ -164,26 +143,28 @@ byteaout(struct varlena * vlena)
164143
/*
165144
*textin- converts "..." to internal representation
166145
*/
167-
structvarlena*
146+
text*
168147
textin(char*inputText)
169148
{
170-
structvarlena*result;
149+
text*result;
171150
intlen;
172151

173152
if (inputText==NULL)
174153
return (NULL);
154+
175155
len=strlen(inputText)+VARHDRSZ;
176-
result= (structvarlena*)palloc(len);
156+
result= (text*)palloc(len);
177157
VARSIZE(result)=len;
158+
178159
memmove(VARDATA(result),inputText,len-VARHDRSZ);
179160
return (result);
180161
}
181162

182163
/*
183164
*textout- converts internal representation to "..."
184165
*/
185-
char*
186-
textout(structvarlena*vlena)
166+
char*
167+
textout(text*vlena)
187168
{
188169
intlen;
189170
char*result;
@@ -207,10 +188,10 @@ textout(struct varlena * vlena)
207188

208189
/*
209190
* textlen -
210-
* returns the actual length of a text* (which may be less than
211-
* the VARSIZE of the text*)
191+
* returns the actual length of a text*
192+
* (which is less than the VARSIZE of the text*)
212193
*/
213-
int
194+
int32
214195
textlen(text*t)
215196
{
216197
if (!PointerIsValid(t))
@@ -231,8 +212,7 @@ textlen(text *t)
231212
* As in previous code, allow concatenation when one string is NULL.
232213
* Is this OK?
233214
*/
234-
235-
text*
215+
text*
236216
textcat(text*t1,text*t2)
237217
{
238218
intlen1,
@@ -269,7 +249,57 @@ textcat(text *t1, text *t2)
269249
VARSIZE(result)=len;
270250

271251
return (result);
272-
}/* textcat() */
252+
}/* textcat() */
253+
254+
/*
255+
* text_substr()
256+
* Return a substring starting at the specified position.
257+
* - thomas 1997-12-31
258+
*
259+
* Input:
260+
* - string
261+
* - starting position (is one-based)
262+
* - string length
263+
*
264+
* If the starting position is zero or less, then return the entire string.
265+
* XXX Note that this may not be the right behavior:
266+
* if we are calculating the starting position we might want it to start at one.
267+
* If the length is less than zero, return the remaining string.
268+
*
269+
* Note that the arguments operate on octet length,
270+
* so not aware of multi-byte character sets.
271+
*/
272+
text*
273+
text_substr(text*string,int32m,int32n)
274+
{
275+
text*ret;
276+
intlen;
277+
278+
if ((string== (text*)NULL)|| (m <=0))
279+
returnstring;
280+
281+
len=VARSIZE(string)-VARHDRSZ;
282+
283+
/* m will now become a zero-based starting position */
284+
if (m >=len)
285+
{
286+
m=0;
287+
n=0;
288+
}
289+
else
290+
{
291+
m--;
292+
if (((m+n)>len)|| (n<0))
293+
n= (len-m);
294+
}
295+
296+
ret= (text*)PALLOC(VARHDRSZ+n);
297+
VARSIZE(ret)=VARHDRSZ+n;
298+
299+
memcpy(VARDATA(ret),VARDATA(string)+m,n);
300+
301+
returnret;
302+
}/* text_substr() */
273303

274304
/*
275305
* textpos -
@@ -278,7 +308,6 @@ textcat(text *t1, text *t2)
278308
* Ref: A Guide To The SQL Standard, Date & Darwen, 1997
279309
* - thomas 1997-07-27
280310
*/
281-
282311
int32
283312
textpos(text*t1,text*t2)
284313
{
@@ -312,17 +341,17 @@ textpos(text *t1, text *t2)
312341
p1++;
313342
};
314343
return (pos);
315-
}/* textpos() */
344+
}/* textpos() */
316345

317346
/*
318347
*texteq- returns 1 iff arguments are equal
319348
*textne- returns 1 iff arguments are not equal
320349
*/
321350
bool
322-
texteq(structvarlena*arg1,structvarlena*arg2)
351+
texteq(text*arg1,text*arg2)
323352
{
324-
registerintlen;
325-
registerchar*a1p,
353+
intlen;
354+
char*a1p,
326355
*a2p;
327356

328357
if (arg1==NULL||arg2==NULL)
@@ -342,10 +371,10 @@ texteq(struct varlena * arg1, struct varlena * arg2)
342371
if (*a1p++!=*a2p++)
343372
return ((bool)0);
344373
return ((bool)1);
345-
}/* texteq() */
374+
}/* texteq() */
346375

347376
bool
348-
textne(structvarlena*arg1,structvarlena*arg2)
377+
textne(text*arg1,text*arg2)
349378
{
350379
return ((bool) !texteq(arg1,arg2));
351380
}
@@ -358,7 +387,7 @@ textne(struct varlena * arg1, struct varlena * arg2)
358387
*but it appears that most routines (incl. this one) assume not! - tgl 97/04/07
359388
*/
360389
bool
361-
text_lt(structvarlena*arg1,structvarlena*arg2)
390+
text_lt(text*arg1,text*arg2)
362391
{
363392
boolresult;
364393

@@ -404,7 +433,7 @@ text_lt(struct varlena * arg1, struct varlena * arg2)
404433
#endif
405434

406435
return (result);
407-
}/* text_lt() */
436+
}/* text_lt() */
408437

409438
/* text_le()
410439
* Comparison function for text strings.
@@ -414,7 +443,7 @@ text_lt(struct varlena * arg1, struct varlena * arg2)
414443
*but it appears that most routines (incl. this one) assume not! - tgl 97/04/07
415444
*/
416445
bool
417-
text_le(structvarlena*arg1,structvarlena*arg2)
446+
text_le(text*arg1,text*arg2)
418447
{
419448
boolresult;
420449

@@ -460,16 +489,16 @@ text_le(struct varlena * arg1, struct varlena * arg2)
460489
#endif
461490

462491
return (result);
463-
}/* text_le() */
492+
}/* text_le() */
464493

465494
bool
466-
text_gt(structvarlena*arg1,structvarlena*arg2)
495+
text_gt(text*arg1,text*arg2)
467496
{
468497
return ((bool) !text_le(arg1,arg2));
469498
}
470499

471500
bool
472-
text_ge(structvarlena*arg1,structvarlena*arg2)
501+
text_ge(text*arg1,text*arg2)
473502
{
474503
return ((bool) !text_lt(arg1,arg2));
475504
}
@@ -481,9 +510,9 @@ text_ge(struct varlena * arg1, struct varlena * arg2)
481510
*-------------------------------------------------------------
482511
*/
483512
int32
484-
byteaGetSize(structvarlena*v)
513+
byteaGetSize(text*v)
485514
{
486-
registerintlen;
515+
intlen;
487516

488517
len=v->vl_len-sizeof(v->vl_len);
489518

@@ -499,7 +528,7 @@ byteaGetSize(struct varlena * v)
499528
*-------------------------------------------------------------
500529
*/
501530
int32
502-
byteaGetByte(structvarlena*v,int32n)
531+
byteaGetByte(text*v,int32n)
503532
{
504533
intlen;
505534
intbyte;
@@ -527,7 +556,7 @@ byteaGetByte(struct varlena * v, int32 n)
527556
*-------------------------------------------------------------
528557
*/
529558
int32
530-
byteaGetBit(structvarlena*v,int32n)
559+
byteaGetBit(text*v,int32n)
531560
{
532561
intbyteNo,
533562
bitNo;
@@ -556,11 +585,11 @@ byteaGetBit(struct varlena * v, int32 n)
556585
*
557586
*-------------------------------------------------------------
558587
*/
559-
structvarlena*
560-
byteaSetByte(structvarlena*v,int32n,int32newByte)
588+
text*
589+
byteaSetByte(text*v,int32n,int32newByte)
561590
{
562591
intlen;
563-
structvarlena*res;
592+
text*res;
564593

565594
len=byteaGetSize(v);
566595

@@ -574,7 +603,7 @@ byteaSetByte(struct varlena * v, int32 n, int32 newByte)
574603
/*
575604
* Make a copy of the original varlena.
576605
*/
577-
res= (structvarlena*)palloc(VARSIZE(v));
606+
res= (text*)palloc(VARSIZE(v));
578607
if (res==NULL)
579608
{
580609
elog(WARN,"byteaSetByte: Out of memory (%d bytes requested)",
@@ -598,10 +627,10 @@ byteaSetByte(struct varlena * v, int32 n, int32 newByte)
598627
*
599628
*-------------------------------------------------------------
600629
*/
601-
structvarlena*
602-
byteaSetBit(structvarlena*v,int32n,int32newBit)
630+
text*
631+
byteaSetBit(text*v,int32n,int32newBit)
603632
{
604-
structvarlena*res;
633+
text*res;
605634
intoldByte,
606635
newByte;
607636
intbyteNo,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp