diff --git a/src/vector.c b/src/vector.c index 5e1e0b6..8c0d2d2 100644 --- a/src/vector.c +++ b/src/vector.c @@ -86,6 +86,24 @@ CheckElement(float value) errmsg("infinite value not allowed in vector"))); } +/* + * PostgreSQL has with the array_isspace function for the character checking which is reimplemented + * as vector_isspace, as it's static, source code link: + * https://github.com/postgres/postgres/blob/378d73ef204d0dcbeab834d52478e8cb90578ab7/src/backend/utils/adt/arrayfuncs.c#L438 + */ +static inline bool +vector_isspace(char ch) +{ + if (ch == ' ' || + ch == '\t' || + ch == '\n' || + ch == '\r' || + ch == '\v' || + ch == '\f') + return true; + return false; +} + /* * Check state array */ @@ -150,6 +168,9 @@ vector_in(PG_FUNCTION_ARGS) char *stringEnd; Vector *result; + while (vector_isspace(*str)) + str++; + if (*str != '[') ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), @@ -172,6 +193,9 @@ vector_in(PG_FUNCTION_ARGS) CheckElement(x[dim]); dim++; + while (vector_isspace(*stringEnd)) + stringEnd++; + if (stringEnd == pt) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), @@ -191,7 +215,12 @@ vector_in(PG_FUNCTION_ARGS) errmsg("malformed vector literal"), errdetail("Unexpected end of input."))); - if (stringEnd[1] != '\0') + stringEnd++; + /* only whitespace is allowed after the closing brace */ + while (vector_isspace(*stringEnd)) + stringEnd++; + + if (*stringEnd != '\0') ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("malformed vector literal"), diff --git a/test/expected/input.out b/test/expected/input.out index 99fc344..b6ccca4 100644 --- a/test/expected/input.out +++ b/test/expected/input.out @@ -10,6 +10,12 @@ SELECT '[-1,2,3]'::vector; [-1,2,3] (1 row) +SELECT ' [ 1, 2 , 3 ] '::vector(3); + vector +--------- + [1,2,3] +(1 row) + SELECT '[1.23456]'::vector; vector ----------- diff --git a/test/sql/input.sql b/test/sql/input.sql index b3d2e53..7d74bc9 100644 --- a/test/sql/input.sql +++ b/test/sql/input.sql @@ -1,5 +1,6 @@ SELECT '[1,2,3]'::vector; SELECT '[-1,2,3]'::vector; +SELECT ' [ 1, 2 , 3 ] '::vector(3); SELECT '[1.23456]'::vector; SELECT '[hello,1]'::vector; SELECT '[NaN,1]'::vector;