7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.45 1998/12/08 06:19:15 thomas Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.46 1998/12/13 23:35:48 thomas Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -313,16 +313,18 @@ textcat(text *t1, text *t2)
313
313
* - starting position (is one-based)
314
314
* - string length
315
315
*
316
- * If the starting position is zero or less, then return the entire string.
317
- * XXX Note that this may not be the right behavior:
318
- * if we are calculating the starting position we might want it to start at one.
316
+ * If the starting position is zero or less, then return from the start of the string
317
+ * adjusting the length to be consistant with the "negative start" per SQL92.
319
318
* If the length is less than zero, return the remaining string.
320
319
*
321
320
* Note that the arguments operate on octet length,
322
321
* so not aware of multi-byte character sets.
323
322
*
324
323
* Added multi-byte support.
325
324
* - Tatsuo Ishii 1998-4-21
325
+ * Changed behavior if starting position is less than one to conform to SQL92 behavior.
326
+ * Formerly returned the entire string; now returns a portion.
327
+ * - Thomas Lockhart 1998-12-10
326
328
*/
327
329
text *
328
330
text_substr (text * string , int32 m , int32 n )
@@ -336,27 +338,33 @@ text_substr(text *string, int32 m, int32 n)
336
338
337
339
#endif
338
340
339
- if (( string == (text * ) NULL ) || ( m <= 0 ) )
341
+ if (string == (text * ) NULL )
340
342
return string ;
341
343
342
344
len = VARSIZE (string ) - VARHDRSZ ;
343
345
#ifdef MULTIBYTE
344
346
len = pg_mbstrlen_with_len (VARDATA (string ), len );
345
347
#endif
346
348
347
- /* m will now become a zero-based starting position */
349
+ /* starting position after the end of the string? */
348
350
if (m > len )
349
351
{
350
- m = 0 ;
352
+ m = 1 ;
351
353
n = 0 ;
352
354
}
353
- else
355
+ /* starting position before the start of the string?
356
+ * then offset into the string per SQL92 spec... */
357
+ else if (m < 1 )
354
358
{
355
- m -- ;
356
- if (((m + n ) > len ) || (n < 0 ))
357
- n = (len - m );
359
+ n += (m - 1 );
360
+ m = 1 ;
358
361
}
359
362
363
+ /* m will now become a zero-based starting position */
364
+ m -- ;
365
+ if (((m + n ) > len ) || (n < 0 ))
366
+ n = (len - m );
367
+
360
368
#ifdef MULTIBYTE
361
369
p = VARDATA (string );
362
370
for (i = 0 ; i < m ; i ++ )
0 commit comments