mirror of
https://github.com/postgres/postgres.git
synced 2025-07-03 20:02:46 +03:00
Have pg_itoa, pg_ltoa and pg_lltoa return the length of the string
Core by no means makes excessive use of these functions, but quite a large number of those usages do require the caller to call strlen() on the returned string. This is quite wasteful since these functions do already have a good idea of the length of the string, so we might as well just have them return that. Reviewed-by: Andrew Gierth Discussion: https://postgr.es/m/CAApHDvrm2A5x2uHYxsqriO2cUaGcFvND%2BksC9e7Tjep0t2RK_A%40mail.gmail.com
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user