7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.12 1997/04/02 18:13:24 scrappy Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.13 1997/04/09 08:29:35 scrappy Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -285,44 +285,75 @@ textne(struct varlena *arg1, struct varlena *arg2)
285
285
return ((bool ) !texteq (arg1 , arg2 ));
286
286
}
287
287
288
+ /* text_lt()
289
+ * Comparison function for text strings.
290
+ * Includes locale support, but must copy strings to temporary memory
291
+ * to allow null-termination for inputs to strcoll().
292
+ * XXX HACK code for textlen() indicates that there can be embedded nulls
293
+ * but it appears that most routines (incl. this one) assume not! - tgl 97/04/07
294
+ */
288
295
bool
289
296
text_lt (struct varlena * arg1 , struct varlena * arg2 )
290
297
{
298
+ bool result ;
299
+
300
+ int cval ;
291
301
int len ;
292
302
#ifdef UNSIGNED_CHAR_TEXT
293
303
unsigned
294
304
#endif
295
305
char * a1p , * a2p ;
296
306
297
307
if (arg1 == NULL || arg2 == NULL )
298
- return ((bool ) 0 );
308
+ return ((bool ) FALSE );
299
309
300
- a1p = (unsigned char * )VARDATA (arg1 );
301
- a2p = (unsigned char * )VARDATA (arg2 );
310
+ len = (((VARSIZE (arg1 ) <= VARSIZE (arg2 ))? VARSIZE (arg1 ): VARSIZE (arg2 ))- VARHDRSZ );
302
311
303
- if ((len = arg1 -> vl_len ) > arg2 -> vl_len )
304
- len = arg2 -> vl_len ;
305
- len -= sizeof (int32 );
306
-
307
- while (len != 0 && * a1p == * a2p )
308
- {
309
- a1p ++ ;
310
- a2p ++ ;
311
- len -- ;
312
- }
313
- if (len )
314
312
#ifdef USE_LOCALE
315
- return (bool ) (strcoll (a2p ,a1p ));
313
+ if (!PointerIsValid (a1p = PALLOC (len + 1 ))
314
+ || !PointerIsValid (a2p = PALLOC (len + 1 ))) {
315
+ elog (WARN ,"Unable to allocate memory for text comparison" ,NULL );
316
+ return (FALSE);
317
+ };
318
+
319
+ memcpy (a1p , VARDATA (arg1 ), len );
320
+ * (a1p + len ) = '\0' ;
321
+ memcpy (a2p , VARDATA (arg2 ), len );
322
+ * (a2p + len ) = '\0' ;
323
+
324
+ cval = strcoll (a1p ,a2p );
325
+ result = ((cval < 0 ) || ((cval == 0 ) && (VARSIZE (arg1 ) < VARSIZE (arg2 ))));
326
+
327
+ PFREE (a1p );
328
+ PFREE (a2p );
329
+
330
+ return (result );
316
331
#else
317
- return (bool ) (* a1p < * a2p );
332
+ a1p = (unsigned char * )VARDATA (arg1 );
333
+ a2p = (unsigned char * )VARDATA (arg2 );
334
+
335
+ while (len != 0 && * a1p == * a2p ) {
336
+ a1p ++ ;
337
+ a2p ++ ;
338
+ len -- ;
339
+ };
340
+ return ((bool ) (len ? (* a1p < * a2p ): (VARSIZE (arg1 ) < VARSIZE (arg2 ))));
318
341
#endif
319
- else
320
- return (bool ) (arg1 -> vl_len < arg2 -> vl_len );
321
- }
322
-
342
+ } /* text_lt() */
343
+
344
+ /* text_le()
345
+ * Comparison function for text strings.
346
+ * Includes locale support, but must copy strings to temporary memory
347
+ * to allow null-termination for inputs to strcoll().
348
+ * XXX HACK code for textlen() indicates that there can be embedded nulls
349
+ * but it appears that most routines (incl. this one) assume not! - tgl 97/04/07
350
+ */
323
351
bool
324
352
text_le (struct varlena * arg1 , struct varlena * arg2 )
325
353
{
354
+ bool result ;
355
+
356
+ int cval ;
326
357
int len ;
327
358
#ifdef UNSIGNED_CHAR_TEXT
328
359
unsigned
@@ -332,28 +363,40 @@ text_le(struct varlena *arg1, struct varlena *arg2)
332
363
if (arg1 == NULL || arg2 == NULL )
333
364
return ((bool ) 0 );
334
365
335
- a1p = (unsigned char * )VARDATA (arg1 );
336
- a2p = (unsigned char * )VARDATA (arg2 );
366
+ len = (((VARSIZE (arg1 ) <= VARSIZE (arg2 ))? VARSIZE (arg1 ): VARSIZE (arg2 ))- VARHDRSZ );
337
367
338
- if ((len = arg1 -> vl_len ) > arg2 -> vl_len )
339
- len = arg2 -> vl_len ;
340
- len -= sizeof (int32 ); /* varlena! */
341
-
342
- while (len != 0 && * a1p == * a2p )
343
- {
344
- a1p ++ ;
345
- a2p ++ ;
346
- len -- ;
347
- }
348
- if (len )
349
368
#ifdef USE_LOCALE
350
- return (bool ) (strcoll (a2p ,a1p ));
369
+ if (!PointerIsValid (a1p = PALLOC (len + 1 ))
370
+ || !PointerIsValid (a2p = PALLOC (len + 1 ))) {
371
+ elog (WARN ,"Unable to allocate memory for text comparison" ,NULL );
372
+ return (FALSE);
373
+ };
374
+
375
+ memcpy (a1p , VARDATA (arg1 ), len );
376
+ * (a1p + len ) = '\0' ;
377
+ memcpy (a2p , VARDATA (arg2 ), len );
378
+ * (a2p + len ) = '\0' ;
379
+
380
+ cval = strcoll (a1p ,a2p );
381
+ result = ((cval < 0 ) || ((cval == 0 ) && (VARSIZE (arg1 ) <= VARSIZE (arg2 ))));
382
+
383
+ PFREE (a1p );
384
+ PFREE (a2p );
385
+
386
+ return (result );
351
387
#else
352
- return (bool ) (* a1p < * a2p );
388
+ a1p = (unsigned char * )VARDATA (arg1 );
389
+ a2p = (unsigned char * )VARDATA (arg2 );
390
+
391
+ while (len != 0 && * a1p == * a2p ) {
392
+ a1p ++ ;
393
+ a2p ++ ;
394
+ len -- ;
395
+ };
396
+
397
+ return ((bool ) (len ? (* a1p <= * a2p ): (VARSIZE (arg1 ) <= VARSIZE (arg2 ))));
353
398
#endif
354
- else
355
- return ((bool ) VARSIZE (arg1 ) <= VARSIZE (arg2 ));
356
- }
399
+ } /* text_le() */
357
400
358
401
bool
359
402
text_gt (struct varlena * arg1 , struct varlena * arg2 )
0 commit comments