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
|
|
|
*
|
2002-06-20 22:29:54 +02:00
|
|
|
* Portions Copyright (c) 1996-2002, 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
|
2003-05-13 01:08:52 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.35 2003/05/12 23:08:50 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2000-06-05 09:29:25 +02:00
|
|
|
|
2003-05-13 01:08:52 +02:00
|
|
|
#include "libpq/pqformat.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "utils/builtins.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* USER I/O ROUTINES *
|
1996-07-09 08:22:35 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* charin - converts "x" to 'x'
|
2001-05-28 23:58:32 +02:00
|
|
|
*
|
|
|
|
* Note that an empty input string will implicitly be converted to \0.
|
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);
|
|
|
|
|
|
|
|
PG_RETURN_CHAR(ch[0]);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* charout - converts 'x' to "x"
|
2001-05-28 23:58:32 +02:00
|
|
|
*
|
|
|
|
* 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.
|
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);
|
1997-09-08 04:41:22 +02:00
|
|
|
char *result = (char *) palloc(2);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
result[0] = ch;
|
1997-09-07 07:04:48 +02:00
|
|
|
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).
|
|
|
|
* Arithmetic is 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)
|
1997-09-07 07:04:48 +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-09-07 07:04:48 +02: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-09-07 07:04:48 +02:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
charle(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02: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-09-07 07:04:48 +02:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
chargt(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02: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-09-07 07:04:48 +02:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
charge(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02: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-09-07 07:04:48 +02:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
charpl(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
|
|
|
char arg2 = PG_GETARG_CHAR(1);
|
|
|
|
|
|
|
|
PG_RETURN_CHAR((int8) arg1 + (int8) arg2);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
charmi(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
|
|
|
char arg2 = PG_GETARG_CHAR(1);
|
|
|
|
|
|
|
|
PG_RETURN_CHAR((int8) arg1 - (int8) arg2);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
charmul(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
|
|
|
char arg2 = PG_GETARG_CHAR(1);
|
|
|
|
|
|
|
|
PG_RETURN_CHAR((int8) arg1 * (int8) arg2);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
chardiv(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
|
|
|
char arg2 = PG_GETARG_CHAR(1);
|
|
|
|
|
2003-03-11 22:01:33 +01:00
|
|
|
if (arg2 == 0)
|
|
|
|
elog(ERROR, "division by zero");
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_CHAR((int8) arg1 / (int8) arg2);
|
1997-09-07 07:04:48 +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)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
text *arg1 = PG_GETARG_TEXT_P(0);
|
2001-05-28 23:58:32 +02:00
|
|
|
char result;
|
|
|
|
|
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* 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.
|
2001-05-28 23:58:32 +02:00
|
|
|
*/
|
|
|
|
if (VARSIZE(arg1) > VARHDRSZ)
|
|
|
|
result = *(VARDATA(arg1));
|
|
|
|
else
|
|
|
|
result = '\0';
|
|
|
|
|
|
|
|
PG_RETURN_CHAR(result);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-09-08 22:59:27 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
char_text(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char arg1 = PG_GETARG_CHAR(0);
|
|
|
|
text *result = palloc(VARHDRSZ + 1);
|
|
|
|
|
2001-05-28 23:58:32 +02:00
|
|
|
/*
|
|
|
|
* 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;
|
2000-06-05 09:29:25 +02:00
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(result);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|