aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/quote.c
diff options
context:
space:
mode:
authorRobert Haas <rhaas@postgresql.org>2010-11-20 10:04:48 -0500
committerRobert Haas <rhaas@postgresql.org>2010-11-20 10:04:48 -0500
commit4343c0e546b216ab38a3397a4f0f7476d557b352 (patch)
tree5a5676ac12ebe9f480851e2bb963ac1418c86921 /src/backend/utils/adt/quote.c
parente8bf683fbee935181e207d7171630194ed01f6a1 (diff)
downloadpostgresql-4343c0e546b216ab38a3397a4f0f7476d557b352.tar.gz
postgresql-4343c0e546b216ab38a3397a4f0f7476d557b352.zip
Expose quote_literal_cstr() from core.
This eliminates the need for inefficient implementions of this functionality in both contrib/dblink and contrib/tablefunc, so remove them. The upcoming patch implementing an in-core format() function will also require this functionality. In passing, add some regression tests.
Diffstat (limited to 'src/backend/utils/adt/quote.c')
-rw-r--r--src/backend/utils/adt/quote.c75
1 files changed, 53 insertions, 22 deletions
diff --git a/src/backend/utils/adt/quote.c b/src/backend/utils/adt/quote.c
index 70e98cad84b..af07443f7df 100644
--- a/src/backend/utils/adt/quote.c
+++ b/src/backend/utils/adt/quote.c
@@ -33,8 +33,8 @@ quote_ident(PG_FUNCTION_ARGS)
}
/*
- * quote_literal -
- * returns a properly quoted literal
+ * quote_literal_internal -
+ * helper function for quote_literal and quote_literal_cstr
*
* NOTE: think not to make this function's behavior change with
* standard_conforming_strings. We don't know where the result
@@ -42,6 +42,37 @@ quote_ident(PG_FUNCTION_ARGS)
* will work with either setting. Take a look at what dblink
* uses this for before thinking you know better.
*/
+static size_t
+quote_literal_internal(char *dst, char *src, size_t len)
+{
+ char *s;
+ char *savedst = dst;
+
+ for (s = src; s < src + len; s++)
+ {
+ if (*s == '\\')
+ {
+ *dst++ = ESCAPE_STRING_SYNTAX;
+ break;
+ }
+ }
+
+ *dst++ = '\'';
+ while (len-- > 0)
+ {
+ if (SQL_STR_DOUBLE(*src, true))
+ *dst++ = *src;
+ *dst++ = *src++;
+ }
+ *dst++ = '\'';
+
+ return dst - savedst;
+}
+
+/*
+ * quote_literal -
+ * returns a properly quoted literal
+ */
Datum
quote_literal(PG_FUNCTION_ARGS)
{
@@ -58,30 +89,30 @@ quote_literal(PG_FUNCTION_ARGS)
cp1 = VARDATA(t);
cp2 = VARDATA(result);
- for (; len-- > 0; cp1++)
- {
- if (*cp1 == '\\')
- {
- *cp2++ = ESCAPE_STRING_SYNTAX;
- break;
- }
- }
+ SET_VARSIZE(result, VARHDRSZ + quote_literal_internal(cp2, cp1, len));
- len = VARSIZE(t) - VARHDRSZ;
- cp1 = VARDATA(t);
+ PG_RETURN_TEXT_P(result);
+}
- *cp2++ = '\'';
- while (len-- > 0)
- {
- if (SQL_STR_DOUBLE(*cp1, true))
- *cp2++ = *cp1;
- *cp2++ = *cp1++;
- }
- *cp2++ = '\'';
+/*
+ * quote_literal_cstr -
+ * returns a properly quoted literal
+ */
+char *
+quote_literal_cstr(char *rawstr)
+{
+ char *result;
+ int len;
+ int newlen;
- SET_VARSIZE(result, cp2 - ((char *) result));
+ len = strlen(rawstr);
+ /* We make a worst-case result area; wasting a little space is OK */
+ result = palloc(len * 2 + 3);
- PG_RETURN_TEXT_P(result);
+ newlen = quote_literal_internal(result, rawstr, len);
+ result[newlen] = '\0';
+
+ return result;
}
/*