aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2014-02-23 16:59:05 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2014-02-23 16:59:05 -0500
commit769065c1b2471f484bb48bb58a8bdcf1d12a419c (patch)
treedc0344a494ceabe955b403b992f4092ec4140f8b /src/backend
parent49c817eab78c6f0ce8c3bf46766b73d6cf3190b7 (diff)
downloadpostgresql-769065c1b2471f484bb48bb58a8bdcf1d12a419c.tar.gz
postgresql-769065c1b2471f484bb48bb58a8bdcf1d12a419c.zip
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/backend')
-rw-r--r--src/backend/commands/extension.c8
-rw-r--r--src/backend/snowball/dict_snowball.c10
-rw-r--r--src/backend/tsearch/ts_locale.c5
-rw-r--r--src/backend/utils/adt/pg_locale.c23
-rw-r--r--src/backend/utils/adt/xml.c27
-rw-r--r--src/backend/utils/mb/mbutils.c4
6 files changed, 26 insertions, 51 deletions
diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index ce5aed301b1..06bd90b9aa9 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -635,7 +635,6 @@ read_extension_script_file(const ExtensionControlFile *control,
const char *filename)
{
int src_encoding;
- int dest_encoding = GetDatabaseEncoding();
bytea *content;
char *src_str;
char *dest_str;
@@ -645,7 +644,7 @@ read_extension_script_file(const ExtensionControlFile *control,
/* use database encoding if not given */
if (control->encoding < 0)
- src_encoding = dest_encoding;
+ src_encoding = GetDatabaseEncoding();
else
src_encoding = control->encoding;
@@ -655,10 +654,7 @@ read_extension_script_file(const ExtensionControlFile *control,
pg_verify_mbstr_len(src_encoding, src_str, len, false);
/* convert the encoding to the database encoding */
- dest_str = (char *) pg_do_encoding_conversion((unsigned char *) src_str,
- len,
- src_encoding,
- dest_encoding);
+ dest_str = pg_any_to_server(src_str, len, src_encoding);
/* if no conversion happened, we have to arrange for null termination */
if (dest_str == src_str)
diff --git a/src/backend/snowball/dict_snowball.c b/src/backend/snowball/dict_snowball.c
index a585c7206b2..37d2966359f 100644
--- a/src/backend/snowball/dict_snowball.c
+++ b/src/backend/snowball/dict_snowball.c
@@ -255,10 +255,7 @@ dsnowball_lexize(PG_FUNCTION_ARGS)
{
char *recoded;
- recoded = (char *) pg_do_encoding_conversion((unsigned char *) txt,
- strlen(txt),
- GetDatabaseEncoding(),
- PG_UTF8);
+ recoded = pg_server_to_any(txt, strlen(txt), PG_UTF8);
if (recoded != txt)
{
pfree(txt);
@@ -284,10 +281,7 @@ dsnowball_lexize(PG_FUNCTION_ARGS)
{
char *recoded;
- recoded = (char *) pg_do_encoding_conversion((unsigned char *) txt,
- strlen(txt),
- PG_UTF8,
- GetDatabaseEncoding());
+ recoded = pg_any_to_server(txt, strlen(txt), PG_UTF8);
if (recoded != txt)
{
pfree(txt);
diff --git a/src/backend/tsearch/ts_locale.c b/src/backend/tsearch/ts_locale.c
index d73687af9e0..f9490c835dd 100644
--- a/src/backend/tsearch/ts_locale.c
+++ b/src/backend/tsearch/ts_locale.c
@@ -209,10 +209,7 @@ t_readline(FILE *fp)
(void) pg_verify_mbstr(PG_UTF8, buf, len, false);
/* And convert */
- recoded = (char *) pg_do_encoding_conversion((unsigned char *) buf,
- len,
- PG_UTF8,
- GetDatabaseEncoding());
+ recoded = pg_any_to_server(buf, len, PG_UTF8);
if (recoded == buf)
{
/*
diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c
index 0bf350adeab..f34446329f5 100644
--- a/src/backend/utils/adt/pg_locale.c
+++ b/src/backend/utils/adt/pg_locale.c
@@ -418,9 +418,7 @@ db_encoding_strdup(int encoding, const char *str)
char *mstr;
/* convert the string to the database encoding */
- pstr = (char *) pg_do_encoding_conversion(
- (unsigned char *) str, strlen(str),
- encoding, GetDatabaseEncoding());
+ pstr = pg_any_to_server(str, strlen(str), encoding);
mstr = strdup(pstr);
if (pstr != str)
pfree(pstr);
@@ -581,35 +579,32 @@ strftime_win32(char *dst, size_t dstlen, const wchar_t *format, const struct tm
{
size_t len;
wchar_t wbuf[MAX_L10N_DATA];
- int encoding;
-
- encoding = GetDatabaseEncoding();
len = wcsftime(wbuf, MAX_L10N_DATA, format, tm);
if (len == 0)
-
+ {
/*
* strftime call failed - return 0 with the contents of dst
* unspecified
*/
return 0;
+ }
len = WideCharToMultiByte(CP_UTF8, 0, wbuf, len, dst, dstlen, NULL, NULL);
if (len == 0)
- elog(ERROR,
- "could not convert string to UTF-8: error code %lu", GetLastError());
+ elog(ERROR, "could not convert string to UTF-8: error code %lu",
+ GetLastError());
dst[len] = '\0';
- if (encoding != PG_UTF8)
+ if (GetDatabaseEncoding() != PG_UTF8)
{
- char *convstr =
- (char *) pg_do_encoding_conversion((unsigned char *) dst,
- len, PG_UTF8, encoding);
+ char *convstr = pg_any_to_server(dst, len, PG_UTF8);
- if (dst != convstr)
+ if (convstr != dst)
{
strlcpy(dst, convstr, dstlen);
len = strlen(dst);
+ pfree(convstr);
}
}
diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index d36751855b6..765469c623e 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -345,10 +345,7 @@ xml_recv(PG_FUNCTION_ARGS)
xmlFreeDoc(doc);
/* Now that we know what we're dealing with, convert to server encoding */
- newstr = (char *) pg_do_encoding_conversion((unsigned char *) str,
- nbytes,
- encoding,
- GetDatabaseEncoding());
+ newstr = pg_any_to_server(str, nbytes, encoding);
if (newstr != str)
{
@@ -1793,10 +1790,8 @@ sqlchar_to_unicode(char *s)
char *utf8string;
pg_wchar ret[2]; /* need space for trailing zero */
- utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s,
- pg_mblen(s),
- GetDatabaseEncoding(),
- PG_UTF8);
+ /* note we're not assuming s is null-terminated */
+ utf8string = pg_server_to_any(s, pg_mblen(s), PG_UTF8);
pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret,
pg_encoding_mblen(PG_UTF8, utf8string));
@@ -1892,19 +1887,15 @@ map_sql_identifier_to_xml_name(char *ident, bool fully_escaped,
static char *
unicode_to_sqlchar(pg_wchar c)
{
- unsigned char utf8string[5]; /* need room for trailing zero */
+ char utf8string[8]; /* need room for trailing zero */
char *result;
memset(utf8string, 0, sizeof(utf8string));
- unicode_to_utf8(c, utf8string);
-
- result = (char *) pg_do_encoding_conversion(utf8string,
- pg_encoding_mblen(PG_UTF8,
- (char *) utf8string),
- PG_UTF8,
- GetDatabaseEncoding());
- /* if pg_do_encoding_conversion didn't strdup, we must */
- if (result == (char *) utf8string)
+ unicode_to_utf8(c, (unsigned char *) utf8string);
+
+ result = pg_any_to_server(utf8string, strlen(utf8string), PG_UTF8);
+ /* if pg_any_to_server didn't strdup, we must */
+ if (result == utf8string)
result = pstrdup(result);
return result;
}
diff --git a/src/backend/utils/mb/mbutils.c b/src/backend/utils/mb/mbutils.c
index 7f43cae69e2..15cf0d806b3 100644
--- a/src/backend/utils/mb/mbutils.c
+++ b/src/backend/utils/mb/mbutils.c
@@ -1077,7 +1077,9 @@ pgwin32_message_to_UTF16(const char *str, int len, int *utf16len)
char *utf8;
utf8 = (char *) pg_do_encoding_conversion((unsigned char *) str,
- len, GetMessageEncoding(), PG_UTF8);
+ len,
+ GetMessageEncoding(),
+ PG_UTF8);
if (utf8 != str)
len = strlen(utf8);