7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.16 1997/06/11 05:18:02 vadim Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.17 1997/07/29 16:12:07 thomas Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -216,40 +216,81 @@ int textlen (text* t)
216
216
/*
217
217
* textcat -
218
218
* takes two text* and returns a text* that is the concatentation of
219
- * the two
220
- */
221
-
222
- /*
223
- * Rewrited by Sapa, sapa@hq.icb.chel.su. 8-Jul-96.
219
+ * the two.
220
+ *
221
+ * Rewritten by Sapa, sapa@hq.icb.chel.su. 8-Jul-96.
222
+ * Updated by Thomas, Thomas.Lockhart@jpl.nasa.gov 1997-07-10.
223
+ * Allocate space for output in all cases.
224
+ * XXX - thomas 1997-07-10
225
+ * As in previous code, allow concatenation when one string is NULL.
226
+ * Is this OK?
224
227
*/
225
228
226
229
text *
227
230
textcat (text * t1 , text * t2 )
228
231
{
229
- int len1 , len2 , newlen ;
232
+ int len1 , len2 , len ;
230
233
char * ptr ;
231
234
text * result ;
232
235
233
- /* Check for NULL strings... */
234
- if (t1 == NULL ) return t2 ;
235
- if (t2 == NULL ) return t1 ;
236
+ if (!PointerIsValid (t1 ) && !PointerIsValid (t2 ))
237
+ return (NULL );
236
238
237
- /* Check for ZERO-LENGTH strings... */
238
- /* I use <= instead of == , I know - it's paranoia, but... */
239
- if (( len1 = VARSIZE (t1 ) - VARHDRSZ ) <= 0 ) return t2 ;
240
- if (( len2 = VARSIZE ( t2 ) - VARHDRSZ ) < = 0 ) return t1 ;
239
+ len1 = ( PointerIsValid ( t1 )? ( VARSIZE ( t1 ) - VARHDRSZ ): 0 );
240
+ if ( len1 < 0 ) len1 = 0 ;
241
+ len2 = ( PointerIsValid ( t2 )? ( VARSIZE (t2 ) - VARHDRSZ ): 0 ) ;
242
+ if ( len2 < 0 ) len2 = 0 ;
241
243
242
- result = ( text * ) palloc ( newlen = len1 + len2 + VARHDRSZ );
244
+ result = PALLOC ( len = len1 + len2 + VARHDRSZ );
243
245
244
246
/* Fill data field of result string... */
245
- memcpy (ptr = VARDATA (result ), VARDATA (t1 ), len1 );
246
- memcpy (ptr + len1 , VARDATA (t2 ), len2 );
247
+ ptr = VARDATA (result );
248
+ if (PointerIsValid (t1 )) memcpy (ptr , VARDATA (t1 ), len1 );
249
+ if (PointerIsValid (t2 )) memcpy (ptr + len1 , VARDATA (t2 ), len2 );
247
250
248
251
/* Set size of result string... */
249
- VARSIZE (result ) = newlen ;
252
+ VARSIZE (result ) = len ;
250
253
251
- return result ;
252
- }
254
+ return (result );
255
+ } /* textcat() */
256
+
257
+ /*
258
+ * textpos -
259
+ * Return the position of the specified substring.
260
+ * Implements the SQL92 POSITION() function.
261
+ * Ref: A Guide To The SQL Standard, Date & Darwen, 1997
262
+ * - thomas 1997-07-27
263
+ */
264
+
265
+ int32
266
+ textpos (text * t1 , text * t2 )
267
+ {
268
+ int pos ;
269
+ int px , p ;
270
+ int len1 , len2 ;
271
+ char * p1 , * p2 ;
272
+
273
+ if (!PointerIsValid (t1 ) || !PointerIsValid (t2 ))
274
+ return (0 );
275
+
276
+ if (VARSIZE (t2 ) <= 0 )
277
+ return (1 );
278
+
279
+ len1 = (VARSIZE (t1 ) - VARHDRSZ );
280
+ len2 = (VARSIZE (t2 ) - VARHDRSZ );
281
+ p1 = VARDATA (t1 );
282
+ p2 = VARDATA (t2 );
283
+ pos = 0 ;
284
+ px = (len1 - len2 );
285
+ for (p = 0 ; p <= px ; p ++ ) {
286
+ if ((* p2 == * p1 ) && (strncmp (p1 , p2 , len2 ) == 0 )) {
287
+ pos = p + 1 ;
288
+ break ;
289
+ };
290
+ p1 ++ ;
291
+ };
292
+ return (pos );
293
+ } /* textpos() */
253
294
254
295
/*
255
296
* texteq - returns 1 iff arguments are equal
@@ -269,15 +310,15 @@ texteq(struct varlena *arg1, struct varlena *arg2)
269
310
a2p = arg2 -> vl_dat ;
270
311
/*
271
312
* Varlenas are stored as the total size (data + size variable)
272
- * followed by the data. The size variable is an int32 so the
273
- * length of the data is the total length less sizeof(int32)
313
+ * followed by the data.
314
+ * Use VARHDRSZ instead of explicit sizeof() - thomas 1997-07-10
274
315
*/
275
- len -= sizeof ( int32 ) ;
316
+ len -= VARHDRSZ ;
276
317
while (len -- != 0 )
277
318
if (* a1p ++ != * a2p ++ )
278
319
return ((bool ) 0 );
279
320
return ((bool ) 1 );
280
- }
321
+ } /* texteq() */
281
322
282
323
bool
283
324
textne (struct varlena * arg1 , struct varlena * arg2 )
0 commit comments