* Functions for the built-in type "char" (not to be confused with
* bpchar, which is the SQL CHAR(n) type).
*
- * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.37 2003/08/04 00:43:25 momjian Exp $
+ * src/backend/utils/adt/char.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
+#include <limits.h>
+
#include "libpq/pqformat.h"
#include "utils/builtins.h"
* charrecv - converts external binary format to char
*
* The external representation is one byte, with no character set
- * conversion. This is somewhat dubious, perhaps, but in many
+ * conversion. This is somewhat dubious, perhaps, but in many
* cases people use char for a 1-byte binary type.
*/
Datum
/*
* NOTE: comparisons are done as though char is unsigned (uint8).
- * Arithmetic is done as though char is signed (int8).
+ * Conversions to and from integer are done as though char is signed (int8).
*
* You wanted consistency?
*/
PG_RETURN_BOOL((uint8) arg1 >= (uint8) arg2);
}
-Datum
-charpl(PG_FUNCTION_ARGS)
-{
- char arg1 = PG_GETARG_CHAR(0);
- char arg2 = PG_GETARG_CHAR(1);
-
- PG_RETURN_CHAR((int8) arg1 + (int8) arg2);
-}
Datum
-charmi(PG_FUNCTION_ARGS)
+chartoi4(PG_FUNCTION_ARGS)
{
char arg1 = PG_GETARG_CHAR(0);
- char arg2 = PG_GETARG_CHAR(1);
- PG_RETURN_CHAR((int8) arg1 - (int8) arg2);
+ PG_RETURN_INT32((int32) ((int8) arg1));
}
Datum
-charmul(PG_FUNCTION_ARGS)
+i4tochar(PG_FUNCTION_ARGS)
{
- char arg1 = PG_GETARG_CHAR(0);
- char arg2 = PG_GETARG_CHAR(1);
-
- PG_RETURN_CHAR((int8) arg1 * (int8) arg2);
-}
-
-Datum
-chardiv(PG_FUNCTION_ARGS)
-{
- char arg1 = PG_GETARG_CHAR(0);
- char arg2 = PG_GETARG_CHAR(1);
+ int32 arg1 = PG_GETARG_INT32(0);
- if (arg2 == 0)
+ if (arg1 < SCHAR_MIN || arg1 > SCHAR_MAX)
ereport(ERROR,
- (errcode(ERRCODE_DIVISION_BY_ZERO),
- errmsg("division by zero")));
+ (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+ errmsg("\"char\" out of range")));
- PG_RETURN_CHAR((int8) arg1 / (int8) arg2);
+ PG_RETURN_CHAR((int8) arg1);
}
Datum
text_char(PG_FUNCTION_ARGS)
{
- text *arg1 = PG_GETARG_TEXT_P(0);
+ text *arg1 = PG_GETARG_TEXT_PP(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.
+ * 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));
+ if (VARSIZE_ANY_EXHDR(arg1) > 0)
+ result = *(VARDATA_ANY(arg1));
else
result = '\0';
*/
if (arg1 != '\0')
{
- VARATT_SIZEP(result) = VARHDRSZ + 1;
+ SET_VARSIZE(result, VARHDRSZ + 1);
*(VARDATA(result)) = arg1;
}
else
- VARATT_SIZEP(result) = VARHDRSZ;
+ SET_VARSIZE(result, VARHDRSZ);
PG_RETURN_TEXT_P(result);
}