* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/varbit.c,v 1.37 2003/09/25 06:58:05 petere Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/varbit.c,v 1.37.2.1 2007/08/21 02:40:33 tgl Exp $
*
*-------------------------------------------------------------------------
*/
result = (char *) palloc(len + 1);
sp = VARBITS(s);
r = result;
- for (i = 0; i < len - BITS_PER_BYTE; i += BITS_PER_BYTE, sp++)
+ for (i = 0; i <= len - BITS_PER_BYTE; i += BITS_PER_BYTE, sp++)
{
+ /* print full bytes */
x = *sp;
for (k = 0; k < BITS_PER_BYTE; k++)
{
x <<= 1;
}
}
- x = *sp;
- for (k = i; k < len; k++)
+ if (i < len)
{
- *r++ = (x & BITHIGH) ? '1' : '0';
- x <<= 1;
+ /* print the last partial byte */
+ x = *sp;
+ for (k = i; k < len; k++)
+ {
+ *r++ = (x & BITHIGH) ? '1' : '0';
+ x <<= 1;
+ }
}
*r = '\0';