mirror of
https://github.com/pgvector/pgvector.git
synced 2026-06-06 05:51:21 +08:00
Improved vector_out code
This commit is contained in:
20
src/vector.c
20
src/vector.c
@@ -275,6 +275,9 @@ vector_in(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_POINTER(result);
|
||||
}
|
||||
|
||||
#define AppendChar(ptr, c) (*(ptr)++ = (c))
|
||||
#define AppendFloat(ptr, f) ((ptr) += float_to_shortest_decimal_bufn((f), (ptr)))
|
||||
|
||||
/*
|
||||
* Convert internal representation to textual representation
|
||||
*/
|
||||
@@ -286,7 +289,6 @@ vector_out(PG_FUNCTION_ARGS)
|
||||
int dim = vector->dim;
|
||||
char *buf;
|
||||
char *ptr;
|
||||
int n;
|
||||
|
||||
/*
|
||||
* Need:
|
||||
@@ -301,21 +303,17 @@ vector_out(PG_FUNCTION_ARGS)
|
||||
buf = (char *) palloc(FLOAT_SHORTEST_DECIMAL_LEN * dim + 2);
|
||||
ptr = buf;
|
||||
|
||||
*ptr = '[';
|
||||
ptr++;
|
||||
AppendChar(ptr, '[');
|
||||
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
*ptr = ',';
|
||||
ptr++;
|
||||
}
|
||||
AppendChar(ptr, ',');
|
||||
|
||||
n = float_to_shortest_decimal_bufn(vector->x[i], ptr);
|
||||
ptr += n;
|
||||
AppendFloat(ptr, vector->x[i]);
|
||||
}
|
||||
*ptr = ']';
|
||||
ptr++;
|
||||
|
||||
AppendChar(ptr, ']');
|
||||
*ptr = '\0';
|
||||
|
||||
PG_FREE_IF_COPY(vector, 0);
|
||||
|
||||
Reference in New Issue
Block a user