diff options
Diffstat (limited to 'src/backend/utils')
-rw-r--r-- | src/backend/utils/adt/int.c | 4 | ||||
-rw-r--r-- | src/backend/utils/adt/int8.c | 11 | ||||
-rw-r--r-- | src/backend/utils/adt/numutils.c | 32 |
3 files changed, 29 insertions, 18 deletions
diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index 63c59c56b3f..418c13e1b4c 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -191,9 +191,7 @@ int2vectorout(PG_FUNCTION_ARGS) { if (num != 0) *rp++ = ' '; - pg_itoa(int2Array->values[num], rp); - while (*++rp != '\0') - ; + rp += pg_itoa(int2Array->values[num], rp); } *rp = '\0'; PG_RETURN_CSTRING(result); diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index abba8f1df04..005f68d8539 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -149,9 +149,16 @@ int8out(PG_FUNCTION_ARGS) int64 val = PG_GETARG_INT64(0); char buf[MAXINT8LEN + 1]; char *result; + int len; - pg_lltoa(val, buf); - result = pstrdup(buf); + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and memcpy() + * to avoid the strlen() call that would otherwise be done in pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); PG_RETURN_CSTRING(result); } diff --git a/src/backend/utils/adt/numutils.c b/src/backend/utils/adt/numutils.c index a9d3fbf758f..412ae361d2c 100644 --- a/src/backend/utils/adt/numutils.c +++ b/src/backend/utils/adt/numutils.c @@ -327,16 +327,17 @@ invalid_syntax: /* * pg_itoa: converts a signed 16-bit integer to its string representation + * and returns strlen(a). * * Caller must ensure that 'a' points to enough memory to hold the result * (at least 7 bytes, counting a leading sign and trailing NUL). * * It doesn't seem worth implementing this separately. */ -void +int pg_itoa(int16 i, char *a) { - pg_ltoa((int32) i, a); + return pg_ltoa((int32) i, a); } /* @@ -404,25 +405,27 @@ pg_ultoa_n(uint32 value, char *a) } /* - * NUL-terminate the output of pg_ultoa_n. + * pg_ltoa: converts a signed 32-bit integer to its string representation and + * returns strlen(a). * * It is the caller's responsibility to ensure that a is at least 12 bytes long, * which is enough room to hold a minus sign, a maximally long int32, and the * above terminating NUL. */ -void +int pg_ltoa(int32 value, char *a) { uint32 uvalue = (uint32) value; - int len; + int len = 0; if (value < 0) { uvalue = (uint32) 0 - uvalue; - *a++ = '-'; + a[len++] = '-'; } - len = pg_ultoa_n(uvalue, a); + len += pg_ultoa_n(uvalue, a + len); a[len] = '\0'; + return len; } /* @@ -510,24 +513,27 @@ pg_ulltoa_n(uint64 value, char *a) } /* - * pg_lltoa: convert a signed 64-bit integer to its string representation + * pg_lltoa: converts a signed 64-bit integer to its string representation and + * returns strlen(a). * * Caller must ensure that 'a' points to enough memory to hold the result * (at least MAXINT8LEN + 1 bytes, counting a leading sign and trailing NUL). */ -void +int pg_lltoa(int64 value, char *a) { - int len; uint64 uvalue = value; + int len = 0; if (value < 0) { - *a++ = '-'; uvalue = (uint64) 0 - uvalue; + a[len++] = '-'; } - len = pg_ulltoa_n(uvalue, a); - a[len] = 0; + + len += pg_ulltoa_n(uvalue, a + len); + a[len] = '\0'; + return len; } |