1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* char.c
|
2003-05-13 01:08:52 +02:00
|
|
|
* Functions for the built-in type "char" (not to be confused with
|
|
|
|
* bpchar, which is the SQL CHAR(n) type).
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2023-01-02 21:00:37 +01:00
|
|
|
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/utils/adt/char.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2000-06-05 09:29:25 +02:00
|
|
|
|
2004-10-05 00:49:59 +02:00
|
|
|
#include <limits.h>
|
|
|
|
|
2003-05-13 01:08:52 +02:00
|
|
|
#include "libpq/pqformat.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "utils/builtins.h"
|
2023-01-10 05:48:59 +01:00
|
|
|
#include "varatt.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2022-08-02 16:29:35 +02:00
|
|
|
#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
|
|
|
|
#define TOOCTAL(c) ((c) + '0')
|
|
|
|
#define FROMOCTAL(c) ((unsigned char) (c) - '0')
|
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* USER I/O ROUTINES *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* charin - converts "x" to 'x'
|
2001-05-28 23:58:32 +02:00
|
|
|
*
|
2022-08-02 16:29:35 +02:00
|
|
|
* This accepts the formats charout produces. If we have multibyte input
|
|
|
|
* that is not in the form '\ooo', then we take its first byte as the value
|
|
|
|
* and silently discard the rest; this is a backwards-compatibility provision.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
charin(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char *ch = PG_GETARG_CSTRING(0);
|
|
|
|
|
2022-08-02 16:29:35 +02:00
|
|
|
if (strlen(ch) == 4 && ch[0] == '\\' &&
|
|
|
|
ISOCTAL(ch[1]) && ISOCTAL(ch[2]) && ISOCTAL(ch[3]))
|
|
|
|
PG_RETURN_CHAR((FROMOCTAL(ch[1]) << 6) +
|
|
|
|
(FROMOCTAL(ch[2]) << 3) +
|
|
|
|
FROMOCTAL(ch[3]));
|
|
|
|
/* This will do the right thing for a zero-length input string */
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_CHAR(ch[0]);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* charout - converts 'x' to "x"
|
2001-05-28 23:58:32 +02:00
|
|
|
*
|
2022-08-02 16:29:35 +02:00
|
|
|
* The possible output formats are:
|
|
|
|
* 1. 0x00 is represented as an empty string.
|
|
|
|
* 2. 0x01..0x7F are represented as a single ASCII byte.
|
|
|
|
* 3. 0x80..0xFF are represented as \ooo (backslash and 3 octal digits).
|
|
|
|
* Case 3 is meant to match the traditional "escape" format of bytea.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
charout(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char ch = PG_GETARG_CHAR(0);
|
2022-08-02 16:29:35 +02:00
|
|
|
char *result = (char *) palloc(5);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2022-08-02 16:29:35 +02:00
|
|
|
if (IS_HIGHBIT_SET(ch))
|
|
|
|
{
|
|
|
|
result[0] = '\\';
|
|
|
|
result[1] = TOOCTAL(((unsigned char) ch) >> 6);
|
|
|
|
result[2] = TOOCTAL((((unsigned char) ch) >> 3) & 07);
|
|
|
|
result[3] = TOOCTAL(((unsigned char) ch) & 07);
|
|
|
|
result[4] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This produces acceptable results for 0x00 as well */
|
|
|
|
result[0] = ch;
|
|
|
|
result[1] = '\0';
|
|
|
|
}
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_CSTRING(result);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2003-05-13 01:08:52 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* cases people use char for a 1-byte binary type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
charrecv(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
|
|
|
|
|
|
|
|
PG_RETURN_CHAR(pq_getmsgbyte(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* charsend - converts char to binary format
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
charsend(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
|
|
|
StringInfoData buf;
|
|
|
|
|
|
|
|
pq_begintypsend(&buf);
|
|
|
|
pq_sendbyte(&buf, arg1);
|
|
|
|
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
|
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* PUBLIC ROUTINES *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2000-06-05 09:29:25 +02:00
|
|
|
* NOTE: comparisons are done as though char is unsigned (uint8).
|
2004-10-05 00:49:59 +02:00
|
|
|
* Conversions to and from integer are done as though char is signed (int8).
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-06-05 09:29:25 +02:00
|
|
|
* You wanted consistency?
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
chareq(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
|
|
|
char arg2 = PG_GETARG_CHAR(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_BOOL(arg1 == arg2);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
charne(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
|
|
|
char arg2 = PG_GETARG_CHAR(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_BOOL(arg1 != arg2);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
charlt(PG_FUNCTION_ARGS)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
|
|
|
char arg2 = PG_GETARG_CHAR(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL((uint8) arg1 < (uint8) arg2);
|
1997-03-15 00:21:12 +01:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
charle(PG_FUNCTION_ARGS)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
|
|
|
char arg2 = PG_GETARG_CHAR(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL((uint8) arg1 <= (uint8) arg2);
|
1997-03-15 00:21:12 +01:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
chargt(PG_FUNCTION_ARGS)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
|
|
|
char arg2 = PG_GETARG_CHAR(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL((uint8) arg1 > (uint8) arg2);
|
1997-03-15 00:21:12 +01:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
charge(PG_FUNCTION_ARGS)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
|
|
|
char arg2 = PG_GETARG_CHAR(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL((uint8) arg1 >= (uint8) arg2);
|
1997-03-15 00:21:12 +01:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
2004-10-05 00:49:59 +02:00
|
|
|
chartoi4(PG_FUNCTION_ARGS)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
|
|
|
|
2004-10-05 00:49:59 +02:00
|
|
|
PG_RETURN_INT32((int32) ((int8) arg1));
|
1997-03-15 00:21:12 +01:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
2004-10-05 00:49:59 +02:00
|
|
|
i4tochar(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-10-05 00:49:59 +02:00
|
|
|
int32 arg1 = PG_GETARG_INT32(0);
|
2000-06-05 09:29:25 +02:00
|
|
|
|
2004-10-05 00:49:59 +02:00
|
|
|
if (arg1 < SCHAR_MIN || arg1 > SCHAR_MAX)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
2004-10-05 00:49:59 +02:00
|
|
|
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
|
|
|
errmsg("\"char\" out of range")));
|
2003-03-11 22:01:33 +01:00
|
|
|
|
2004-10-05 00:49:59 +02:00
|
|
|
PG_RETURN_CHAR((int8) arg1);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2001-05-28 23:58:32 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
text_char(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2017-03-13 00:35:34 +01:00
|
|
|
text *arg1 = PG_GETARG_TEXT_PP(0);
|
2022-08-02 16:29:35 +02:00
|
|
|
char *ch = VARDATA_ANY(arg1);
|
2001-05-28 23:58:32 +02:00
|
|
|
char result;
|
|
|
|
|
|
|
|
/*
|
2022-08-02 16:29:35 +02:00
|
|
|
* Conversion rules are the same as in charin(), but here we need to
|
|
|
|
* handle the empty-string case honestly.
|
2001-05-28 23:58:32 +02:00
|
|
|
*/
|
2022-08-02 16:29:35 +02:00
|
|
|
if (VARSIZE_ANY_EXHDR(arg1) == 4 && ch[0] == '\\' &&
|
|
|
|
ISOCTAL(ch[1]) && ISOCTAL(ch[2]) && ISOCTAL(ch[3]))
|
|
|
|
result = (FROMOCTAL(ch[1]) << 6) +
|
|
|
|
(FROMOCTAL(ch[2]) << 3) +
|
|
|
|
FROMOCTAL(ch[3]);
|
|
|
|
else if (VARSIZE_ANY_EXHDR(arg1) > 0)
|
|
|
|
result = ch[0];
|
2001-05-28 23:58:32 +02:00
|
|
|
else
|
|
|
|
result = '\0';
|
|
|
|
|
|
|
|
PG_RETURN_CHAR(result);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
char_text(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
2022-08-02 16:29:35 +02:00
|
|
|
text *result = palloc(VARHDRSZ + 4);
|
2000-06-05 09:29:25 +02:00
|
|
|
|
2001-05-28 23:58:32 +02:00
|
|
|
/*
|
2022-08-02 16:29:35 +02:00
|
|
|
* Conversion rules are the same as in charout(), but here we need to be
|
|
|
|
* honest about converting 0x00 to an empty string.
|
2001-05-28 23:58:32 +02:00
|
|
|
*/
|
2022-08-02 16:29:35 +02:00
|
|
|
if (IS_HIGHBIT_SET(arg1))
|
|
|
|
{
|
|
|
|
SET_VARSIZE(result, VARHDRSZ + 4);
|
|
|
|
(VARDATA(result))[0] = '\\';
|
|
|
|
(VARDATA(result))[1] = TOOCTAL(((unsigned char) arg1) >> 6);
|
|
|
|
(VARDATA(result))[2] = TOOCTAL((((unsigned char) arg1) >> 3) & 07);
|
|
|
|
(VARDATA(result))[3] = TOOCTAL(((unsigned char) arg1) & 07);
|
|
|
|
}
|
|
|
|
else if (arg1 != '\0')
|
2001-05-28 23:58:32 +02:00
|
|
|
{
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(result, VARHDRSZ + 1);
|
2001-05-28 23:58:32 +02:00
|
|
|
*(VARDATA(result)) = arg1;
|
|
|
|
}
|
|
|
|
else
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(result, VARHDRSZ);
|
2000-06-05 09:29:25 +02:00
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(result);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|