diff options
author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2016-12-26 20:01:19 +0100 |
---|---|---|
committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2016-12-27 00:29:01 +0100 |
commit | dfe547856ee946163dfdc695723f7ab67865228b (patch) | |
tree | 9143d7beadf2b32efe542e9fe013959bf0861887 /psycopg/utils.c | |
parent | a255e4e1c6bbe32b0865da410fecd7be067902a7 (diff) | |
download | psycopg2-dfe547856ee946163dfdc695723f7ab67865228b.tar.gz |
Use -1 instead of 0 to say "calculate the length" in many funcs
0 is a valid length, isn't it?
Diffstat (limited to 'psycopg/utils.c')
-rw-r--r-- | psycopg/utils.c | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/psycopg/utils.c b/psycopg/utils.c index bc6f7be..85ca9d6 100644 --- a/psycopg/utils.c +++ b/psycopg/utils.c @@ -40,6 +40,8 @@ * and set an exception. The returned string includes quotes and leading E if * needed. * + * `len` is optional: if < 0 it will be calculated. + * * If tolen is set, it will contain the length of the escaped string, * including quotes. */ @@ -50,7 +52,7 @@ psycopg_escape_string(connectionObject *conn, const char *from, Py_ssize_t len, Py_ssize_t ql; int eq = (conn && (conn->equote)) ? 1 : 0; - if (len == 0) { + if (len < 0) { len = strlen(from); } else if (strchr(from, '\0') != from + len) { PyErr_Format(PyExc_ValueError, "A string literal cannot contain NUL (0x00) characters."); @@ -92,13 +94,13 @@ psycopg_escape_string(connectionObject *conn, const char *from, Py_ssize_t len, /* Escape a string for inclusion in a query as identifier. * - * 'len' is optional: if 0 the length is calculated. + * 'len' is optional: if < 0 it will be calculated. * * Return a string allocated by Postgres: free it using PQfreemem * In case of error set a Python exception. */ char * -psycopg_escape_identifier(connectionObject *conn, const char *str, size_t len) +psycopg_escape_identifier(connectionObject *conn, const char *str, Py_ssize_t len) { char *rv = NULL; @@ -107,7 +109,7 @@ psycopg_escape_identifier(connectionObject *conn, const char *str, size_t len) goto exit; } - if (!len) { len = strlen(str); } + if (len < 0) { len = strlen(str); } rv = PQescapeIdentifier(conn->pgconn, str, len); if (!rv) { @@ -127,7 +129,7 @@ exit: /* Duplicate a string. * * Allocate a new buffer on the Python heap containing the new string. - * 'len' is optional: if 0 the length is calculated. + * 'len' is optional: if < 0 the length is calculated. * * Store the return in 'to' and return 0 in case of success, else return -1 * and raise an exception. @@ -141,7 +143,7 @@ psycopg_strdup(char **to, const char *from, Py_ssize_t len) *to = NULL; return 0; } - if (!len) { len = strlen(from); } + if (len < 0) { len = strlen(from); } if (!(*to = PyMem_Malloc(len + 1))) { PyErr_NoMemory(); return -1; |