77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.44 1998/10/08 18:30:12 momjian Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.44.2.1 1998/12/14 00:13:56 thomas Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -313,16 +313,18 @@ textcat(text *t1, text *t2)
313313 * - starting position (is one-based)
314314 * - string length
315315 *
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.
319318 * If the length is less than zero, return the remaining string.
320319 *
321320 * Note that the arguments operate on octet length,
322321 * so not aware of multi-byte character sets.
323322 *
324323 * Added multi-byte support.
325324 * - 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
326328 */
327329text *
328330text_substr (text * string , int32 m , int32 n )
@@ -336,27 +338,33 @@ text_substr(text *string, int32 m, int32 n)
336338
337339#endif
338340
339- if (( string == (text * ) NULL ) || ( m <= 0 ) )
341+ if (string == (text * ) NULL )
340342 return string ;
341343
342344 len = VARSIZE (string ) - VARHDRSZ ;
343345#ifdef MULTIBYTE
344346 len = pg_mbstrlen_with_len (VARDATA (string ), len );
345347#endif
346348
347- /* m will now become a zero-based starting position */
349+ /* starting position after the end of the string? */
348350 if (m > len )
349351 {
350- m = 0 ;
352+ m = 1 ;
351353 n = 0 ;
352354 }
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 )
354358 {
355- m -- ;
356- if (((m + n ) > len ) || (n < 0 ))
357- n = (len - m );
359+ n += (m - 1 );
360+ m = 1 ;
358361 }
359362
363+ /* m will now become a zero-based starting position */
364+ m -- ;
365+ if (((m + n ) > len ) || (n < 0 ))
366+ n = (len - m );
367+
360368#ifdef MULTIBYTE
361369 p = VARDATA (string );
362370 for (i = 0 ; i < m ; i ++ )
0 commit comments