aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2001-05-28 21:58:32 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2001-05-28 21:58:32 +0000
commit8a3b80deb03b3d4be7fb1ffbe6b10f49bc0253db (patch)
tree63cc761f8b111b25a4fe7d9257a04f26569e39c9 /src
parenta056f14be0a22834b5fa7b90c0c5ea1ec42f2a29 (diff)
downloadpostgresql-8a3b80deb03b3d4be7fb1ffbe6b10f49bc0253db.tar.gz
postgresql-8a3b80deb03b3d4be7fb1ffbe6b10f49bc0253db.zip
Make text <=> char conversion functions convert zero character to and
from an empty text string. This makes them consistent with the de facto behavior of type char's I/O conversion functions, and avoids generating text values with embedded nulls, which confuse many text operators.
Diffstat (limited to 'src')
-rw-r--r--src/backend/utils/adt/char.c38
1 files changed, 33 insertions, 5 deletions
diff --git a/src/backend/utils/adt/char.c b/src/backend/utils/adt/char.c
index 3237566fc5f..dc6bd5e9765 100644
--- a/src/backend/utils/adt/char.c
+++ b/src/backend/utils/adt/char.c
@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.30 2001/01/24 19:43:13 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.31 2001/05/28 21:58:32 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -23,6 +23,8 @@
/*
* charin - converts "x" to 'x'
+ *
+ * Note that an empty input string will implicitly be converted to \0.
*/
Datum
charin(PG_FUNCTION_ARGS)
@@ -34,6 +36,10 @@ charin(PG_FUNCTION_ARGS)
/*
* charout - converts 'x' to "x"
+ *
+ * Note that if the char value is \0, the resulting string will appear
+ * to be empty (null-terminated after zero characters). So this is the
+ * inverse of the charin() function for such data.
*/
Datum
charout(PG_FUNCTION_ARGS)
@@ -147,13 +153,24 @@ chardiv(PG_FUNCTION_ARGS)
PG_RETURN_CHAR((int8) arg1 / (int8) arg2);
}
+
Datum
text_char(PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_P(0);
+ char result;
+
+ /*
+ * An empty input string is converted to \0 (for consistency with charin).
+ * If the input is longer than one character, the excess data is silently
+ * discarded.
+ */
+ if (VARSIZE(arg1) > VARHDRSZ)
+ result = *(VARDATA(arg1));
+ else
+ result = '\0';
- /* XXX what if arg1 has length zero? */
- PG_RETURN_CHAR(*(VARDATA(arg1)));
+ PG_RETURN_CHAR(result);
}
Datum
@@ -162,8 +179,19 @@ char_text(PG_FUNCTION_ARGS)
char arg1 = PG_GETARG_CHAR(0);
text *result = palloc(VARHDRSZ + 1);
- VARATT_SIZEP(result) = VARHDRSZ + 1;
- *(VARDATA(result)) = arg1;
+ /*
+ * Convert \0 to an empty string, for consistency with charout (and
+ * because the text stuff doesn't like embedded nulls all that well).
+ */
+ if (arg1 != '\0')
+ {
+ VARATT_SIZEP(result) = VARHDRSZ + 1;
+ *(VARDATA(result)) = arg1;
+ }
+ else
+ {
+ VARATT_SIZEP(result) = VARHDRSZ;
+ }
PG_RETURN_TEXT_P(result);
}