diff options
| author | Tom Lane | 2014-02-23 21:59:05 +0000 |
|---|---|---|
| committer | Tom Lane | 2014-02-23 21:59:05 +0000 |
| commit | 769065c1b2471f484bb48bb58a8bdcf1d12a419c (patch) | |
| tree | dc0344a494ceabe955b403b992f4092ec4140f8b /src/pl | |
| parent | 49c817eab78c6f0ce8c3bf46766b73d6cf3190b7 (diff) | |
Prefer pg_any_to_server/pg_server_to_any over pg_do_encoding_conversion.
A large majority of the callers of pg_do_encoding_conversion were
specifying the database encoding as either source or target of the
conversion, meaning that we can use the less general functions
pg_any_to_server/pg_server_to_any instead.
The main advantage of using the latter functions is that they can make use
of a cached conversion-function lookup in the common case that the other
encoding is the current client_encoding. It's notationally cleaner too in
most cases, not least because of the historical artifact that the latter
functions use "char *" rather than "unsigned char *" in their APIs.
Note that pg_any_to_server will apply an encoding verification step in
some cases where pg_do_encoding_conversion would have just done nothing.
This seems to me to be a good idea at most of these call sites, though
it partially negates the performance benefit.
Per discussion of bug #9210.
Diffstat (limited to 'src/pl')
| -rw-r--r-- | src/pl/plperl/plperl.c | 8 | ||||
| -rw-r--r-- | src/pl/plperl/plperl_helpers.h | 25 | ||||
| -rw-r--r-- | src/pl/plpython/plpy_util.c | 15 | ||||
| -rw-r--r-- | src/pl/tcl/pltcl.c | 8 |
4 files changed, 20 insertions, 36 deletions
diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c index ed6884e863..f8ccaa59e8 100644 --- a/src/pl/plperl/plperl.c +++ b/src/pl/plperl/plperl.c @@ -3811,9 +3811,7 @@ hv_store_string(HV *hv, const char *key, SV *val) char *hkey; SV **ret; - hkey = (char *) - pg_do_encoding_conversion((unsigned char *) key, strlen(key), - GetDatabaseEncoding(), PG_UTF8); + hkey = pg_server_to_any(key, strlen(key), PG_UTF8); /* * This seems nowhere documented, but under Perl 5.8.0 and up, hv_store() @@ -3841,9 +3839,7 @@ hv_fetch_string(HV *hv, const char *key) char *hkey; SV **ret; - hkey = (char *) - pg_do_encoding_conversion((unsigned char *) key, strlen(key), - GetDatabaseEncoding(), PG_UTF8); + hkey = pg_server_to_any(key, strlen(key), PG_UTF8); /* See notes in hv_store_string */ hlen = -(int) strlen(hkey); diff --git a/src/pl/plperl/plperl_helpers.h b/src/pl/plperl/plperl_helpers.h index 3e8aa7c4a2..c1c7c297cc 100644 --- a/src/pl/plperl/plperl_helpers.h +++ b/src/pl/plperl/plperl_helpers.h @@ -9,24 +9,11 @@ static inline char * utf_u2e(char *utf8_str, size_t len) { - int enc = GetDatabaseEncoding(); char *ret; - /* - * When we are in a PG_UTF8 or SQL_ASCII database - * pg_do_encoding_conversion() will not do any conversion (which is good) - * or verification (not so much), so we need to run the verification step - * separately. - */ - if (enc == PG_UTF8 || enc == PG_SQL_ASCII) - { - pg_verify_mbstr_len(enc, utf8_str, len, false); - ret = utf8_str; - } - else - ret = (char *) pg_do_encoding_conversion((unsigned char *) utf8_str, - len, PG_UTF8, enc); + ret = pg_any_to_server(utf8_str, len, PG_UTF8); + /* ensure we have a copy even if no conversion happened */ if (ret == utf8_str) ret = pstrdup(ret); @@ -41,12 +28,14 @@ utf_u2e(char *utf8_str, size_t len) static inline char * utf_e2u(const char *str) { - char *ret = - (char *) pg_do_encoding_conversion((unsigned char *) str, strlen(str), - GetDatabaseEncoding(), PG_UTF8); + char *ret; + ret = pg_server_to_any(str, strlen(str), PG_UTF8); + + /* ensure we have a copy even if no conversion happened */ if (ret == str) ret = pstrdup(ret); + return ret; } diff --git a/src/pl/plpython/plpy_util.c b/src/pl/plpython/plpy_util.c index 95cbba5cdc..88670e66d0 100644 --- a/src/pl/plpython/plpy_util.c +++ b/src/pl/plpython/plpy_util.c @@ -90,11 +90,9 @@ PLyUnicode_Bytes(PyObject *unicode) { PG_TRY(); { - encoded = (char *) pg_do_encoding_conversion( - (unsigned char *) utf8string, - strlen(utf8string), - PG_UTF8, - GetDatabaseEncoding()); + encoded = pg_any_to_server(utf8string, + strlen(utf8string), + PG_UTF8); } PG_CATCH(); { @@ -109,7 +107,7 @@ PLyUnicode_Bytes(PyObject *unicode) /* finally, build a bytes object in the server encoding */ rv = PyBytes_FromStringAndSize(encoded, strlen(encoded)); - /* if pg_do_encoding_conversion allocated memory, free it now */ + /* if pg_any_to_server allocated memory, free it now */ if (utf8string != encoded) pfree(encoded); @@ -149,10 +147,7 @@ PLyUnicode_FromString(const char *s) char *utf8string; PyObject *o; - utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s, - strlen(s), - GetDatabaseEncoding(), - PG_UTF8); + utf8string = pg_server_to_any(s, strlen(s), PG_UTF8); o = PyUnicode_FromString(utf8string); diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c index 0538038982..b3bf65ec88 100644 --- a/src/pl/tcl/pltcl.c +++ b/src/pl/tcl/pltcl.c @@ -63,13 +63,17 @@ static unsigned char * utf_u2e(unsigned char *src) { - return pg_do_encoding_conversion(src, strlen(src), PG_UTF8, GetDatabaseEncoding()); + return (unsigned char *) pg_any_to_server((char *) src, + strlen(src), + PG_UTF8); } static unsigned char * utf_e2u(unsigned char *src) { - return pg_do_encoding_conversion(src, strlen(src), GetDatabaseEncoding(), PG_UTF8); + return (unsigned char *) pg_server_to_any((char *) src, + strlen(src), + PG_UTF8); } #define PLTCL_UTF |
