1 /*-------------------------------------------------------------------------
4 * Functions for the built-in type "char".
5 * Functions for the built-in type "cid" (what's that doing here?)
7 * Portions Copyright (c) 1996-2001, 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.31 2001/05/28 21:58:32 tgl Exp $
14 *-------------------------------------------------------------------------
18 #include "utils/builtins.h"
20 /*****************************************************************************
22 *****************************************************************************/
25 * charin - converts "x" to 'x'
27 * Note that an empty input string will implicitly be converted to \0.
30 charin(PG_FUNCTION_ARGS)
32 char *ch = PG_GETARG_CSTRING(0);
34 PG_RETURN_CHAR(ch[0]);
38 * charout - converts 'x' to "x"
40 * Note that if the char value is \0, the resulting string will appear
41 * to be empty (null-terminated after zero characters). So this is the
42 * inverse of the charin() function for such data.
45 charout(PG_FUNCTION_ARGS)
47 char ch = PG_GETARG_CHAR(0);
48 char *result = (char *) palloc(2);
52 PG_RETURN_CSTRING(result);
55 /*****************************************************************************
57 *****************************************************************************/
60 * NOTE: comparisons are done as though char is unsigned (uint8).
61 * Arithmetic is done as though char is signed (int8).
63 * You wanted consistency?
67 chareq(PG_FUNCTION_ARGS)
69 char arg1 = PG_GETARG_CHAR(0);
70 char arg2 = PG_GETARG_CHAR(1);
72 PG_RETURN_BOOL(arg1 == arg2);
76 charne(PG_FUNCTION_ARGS)
78 char arg1 = PG_GETARG_CHAR(0);
79 char arg2 = PG_GETARG_CHAR(1);
81 PG_RETURN_BOOL(arg1 != arg2);
85 charlt(PG_FUNCTION_ARGS)
87 char arg1 = PG_GETARG_CHAR(0);
88 char arg2 = PG_GETARG_CHAR(1);
90 PG_RETURN_BOOL((uint8) arg1 < (uint8) arg2);
94 charle(PG_FUNCTION_ARGS)
96 char arg1 = PG_GETARG_CHAR(0);
97 char arg2 = PG_GETARG_CHAR(1);
99 PG_RETURN_BOOL((uint8) arg1 <= (uint8) arg2);
103 chargt(PG_FUNCTION_ARGS)
105 char arg1 = PG_GETARG_CHAR(0);
106 char arg2 = PG_GETARG_CHAR(1);
108 PG_RETURN_BOOL((uint8) arg1 > (uint8) arg2);
112 charge(PG_FUNCTION_ARGS)
114 char arg1 = PG_GETARG_CHAR(0);
115 char arg2 = PG_GETARG_CHAR(1);
117 PG_RETURN_BOOL((uint8) arg1 >= (uint8) arg2);
121 charpl(PG_FUNCTION_ARGS)
123 char arg1 = PG_GETARG_CHAR(0);
124 char arg2 = PG_GETARG_CHAR(1);
126 PG_RETURN_CHAR((int8) arg1 + (int8) arg2);
130 charmi(PG_FUNCTION_ARGS)
132 char arg1 = PG_GETARG_CHAR(0);
133 char arg2 = PG_GETARG_CHAR(1);
135 PG_RETURN_CHAR((int8) arg1 - (int8) arg2);
139 charmul(PG_FUNCTION_ARGS)
141 char arg1 = PG_GETARG_CHAR(0);
142 char arg2 = PG_GETARG_CHAR(1);
144 PG_RETURN_CHAR((int8) arg1 * (int8) arg2);
148 chardiv(PG_FUNCTION_ARGS)
150 char arg1 = PG_GETARG_CHAR(0);
151 char arg2 = PG_GETARG_CHAR(1);
153 PG_RETURN_CHAR((int8) arg1 / (int8) arg2);
158 text_char(PG_FUNCTION_ARGS)
160 text *arg1 = PG_GETARG_TEXT_P(0);
164 * An empty input string is converted to \0 (for consistency with charin).
165 * If the input is longer than one character, the excess data is silently
168 if (VARSIZE(arg1) > VARHDRSZ)
169 result = *(VARDATA(arg1));
173 PG_RETURN_CHAR(result);
177 char_text(PG_FUNCTION_ARGS)
179 char arg1 = PG_GETARG_CHAR(0);
180 text *result = palloc(VARHDRSZ + 1);
183 * Convert \0 to an empty string, for consistency with charout (and
184 * because the text stuff doesn't like embedded nulls all that well).
188 VARATT_SIZEP(result) = VARHDRSZ + 1;
189 *(VARDATA(result)) = arg1;
193 VARATT_SIZEP(result) = VARHDRSZ;
196 PG_RETURN_TEXT_P(result);
200 /*****************************************************************************
201 * USER I/O ROUTINES *
202 *****************************************************************************/
205 * cidin - converts CommandId to internal representation.
208 cidin(PG_FUNCTION_ARGS)
210 char *s = PG_GETARG_CSTRING(0);
215 /* XXX assume that CommandId is 32 bits... */
216 PG_RETURN_INT32((int32) c);
220 * cidout - converts a cid to external representation.
223 cidout(PG_FUNCTION_ARGS)
225 /* XXX assume that CommandId is 32 bits... */
226 CommandId c = PG_GETARG_INT32(0);
227 char *result = (char *) palloc(16);
229 sprintf(result, "%u", (unsigned int) c);
230 PG_RETURN_CSTRING(result);
233 /*****************************************************************************
235 *****************************************************************************/
238 cideq(PG_FUNCTION_ARGS)
240 /* XXX assume that CommandId is 32 bits... */
241 CommandId arg1 = PG_GETARG_INT32(0);
242 CommandId arg2 = PG_GETARG_INT32(1);
244 PG_RETURN_BOOL(arg1 == arg2);