1 /*-------------------------------------------------------------------------
4 * Functions for the built-in type "char" (not to be confused with
5 * bpchar, which is the SQL CHAR(n) type).
7 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.35 2003/05/12 23:08:50 tgl Exp $
14 *-------------------------------------------------------------------------
18 #include "libpq/pqformat.h"
19 #include "utils/builtins.h"
21 /*****************************************************************************
23 *****************************************************************************/
26 * charin - converts "x" to 'x'
28 * Note that an empty input string will implicitly be converted to \0.
31 charin(PG_FUNCTION_ARGS)
33 char *ch = PG_GETARG_CSTRING(0);
35 PG_RETURN_CHAR(ch[0]);
39 * charout - converts 'x' to "x"
41 * Note that if the char value is \0, the resulting string will appear
42 * to be empty (null-terminated after zero characters). So this is the
43 * inverse of the charin() function for such data.
46 charout(PG_FUNCTION_ARGS)
48 char ch = PG_GETARG_CHAR(0);
49 char *result = (char *) palloc(2);
53 PG_RETURN_CSTRING(result);
57 * charrecv - converts external binary format to char
59 * The external representation is one byte, with no character set
60 * conversion. This is somewhat dubious, perhaps, but in many
61 * cases people use char for a 1-byte binary type.
64 charrecv(PG_FUNCTION_ARGS)
66 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
68 PG_RETURN_CHAR(pq_getmsgbyte(buf));
72 * charsend - converts char to binary format
75 charsend(PG_FUNCTION_ARGS)
77 char arg1 = PG_GETARG_CHAR(0);
80 pq_begintypsend(&buf);
81 pq_sendbyte(&buf, arg1);
82 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
85 /*****************************************************************************
87 *****************************************************************************/
90 * NOTE: comparisons are done as though char is unsigned (uint8).
91 * Arithmetic is done as though char is signed (int8).
93 * You wanted consistency?
97 chareq(PG_FUNCTION_ARGS)
99 char arg1 = PG_GETARG_CHAR(0);
100 char arg2 = PG_GETARG_CHAR(1);
102 PG_RETURN_BOOL(arg1 == arg2);
106 charne(PG_FUNCTION_ARGS)
108 char arg1 = PG_GETARG_CHAR(0);
109 char arg2 = PG_GETARG_CHAR(1);
111 PG_RETURN_BOOL(arg1 != arg2);
115 charlt(PG_FUNCTION_ARGS)
117 char arg1 = PG_GETARG_CHAR(0);
118 char arg2 = PG_GETARG_CHAR(1);
120 PG_RETURN_BOOL((uint8) arg1 < (uint8) arg2);
124 charle(PG_FUNCTION_ARGS)
126 char arg1 = PG_GETARG_CHAR(0);
127 char arg2 = PG_GETARG_CHAR(1);
129 PG_RETURN_BOOL((uint8) arg1 <= (uint8) arg2);
133 chargt(PG_FUNCTION_ARGS)
135 char arg1 = PG_GETARG_CHAR(0);
136 char arg2 = PG_GETARG_CHAR(1);
138 PG_RETURN_BOOL((uint8) arg1 > (uint8) arg2);
142 charge(PG_FUNCTION_ARGS)
144 char arg1 = PG_GETARG_CHAR(0);
145 char arg2 = PG_GETARG_CHAR(1);
147 PG_RETURN_BOOL((uint8) arg1 >= (uint8) arg2);
151 charpl(PG_FUNCTION_ARGS)
153 char arg1 = PG_GETARG_CHAR(0);
154 char arg2 = PG_GETARG_CHAR(1);
156 PG_RETURN_CHAR((int8) arg1 + (int8) arg2);
160 charmi(PG_FUNCTION_ARGS)
162 char arg1 = PG_GETARG_CHAR(0);
163 char arg2 = PG_GETARG_CHAR(1);
165 PG_RETURN_CHAR((int8) arg1 - (int8) arg2);
169 charmul(PG_FUNCTION_ARGS)
171 char arg1 = PG_GETARG_CHAR(0);
172 char arg2 = PG_GETARG_CHAR(1);
174 PG_RETURN_CHAR((int8) arg1 * (int8) arg2);
178 chardiv(PG_FUNCTION_ARGS)
180 char arg1 = PG_GETARG_CHAR(0);
181 char arg2 = PG_GETARG_CHAR(1);
184 elog(ERROR, "division by zero");
186 PG_RETURN_CHAR((int8) arg1 / (int8) arg2);
191 text_char(PG_FUNCTION_ARGS)
193 text *arg1 = PG_GETARG_TEXT_P(0);
197 * An empty input string is converted to \0 (for consistency with
198 * charin). If the input is longer than one character, the excess data
199 * is silently discarded.
201 if (VARSIZE(arg1) > VARHDRSZ)
202 result = *(VARDATA(arg1));
206 PG_RETURN_CHAR(result);
210 char_text(PG_FUNCTION_ARGS)
212 char arg1 = PG_GETARG_CHAR(0);
213 text *result = palloc(VARHDRSZ + 1);
216 * Convert \0 to an empty string, for consistency with charout (and
217 * because the text stuff doesn't like embedded nulls all that well).
221 VARATT_SIZEP(result) = VARHDRSZ + 1;
222 *(VARDATA(result)) = arg1;
225 VARATT_SIZEP(result) = VARHDRSZ;
227 PG_RETURN_TEXT_P(result);