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-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.34 2003/03/11 21:01:33 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);
154 elog(ERROR, "division by zero");
156 PG_RETURN_CHAR((int8) arg1 / (int8) arg2);
161 text_char(PG_FUNCTION_ARGS)
163 text *arg1 = PG_GETARG_TEXT_P(0);
167 * An empty input string is converted to \0 (for consistency with
168 * charin). If the input is longer than one character, the excess data
169 * is silently discarded.
171 if (VARSIZE(arg1) > VARHDRSZ)
172 result = *(VARDATA(arg1));
176 PG_RETURN_CHAR(result);
180 char_text(PG_FUNCTION_ARGS)
182 char arg1 = PG_GETARG_CHAR(0);
183 text *result = palloc(VARHDRSZ + 1);
186 * Convert \0 to an empty string, for consistency with charout (and
187 * because the text stuff doesn't like embedded nulls all that well).
191 VARATT_SIZEP(result) = VARHDRSZ + 1;
192 *(VARDATA(result)) = arg1;
195 VARATT_SIZEP(result) = VARHDRSZ;
197 PG_RETURN_TEXT_P(result);
201 /*****************************************************************************
202 * USER I/O ROUTINES *
203 *****************************************************************************/
206 * cidin - converts CommandId to internal representation.
209 cidin(PG_FUNCTION_ARGS)
211 char *s = PG_GETARG_CSTRING(0);
216 /* XXX assume that CommandId is 32 bits... */
217 PG_RETURN_INT32((int32) c);
221 * cidout - converts a cid to external representation.
224 cidout(PG_FUNCTION_ARGS)
226 /* XXX assume that CommandId is 32 bits... */
227 CommandId c = PG_GETARG_INT32(0);
228 char *result = (char *) palloc(16);
230 sprintf(result, "%u", (unsigned int) c);
231 PG_RETURN_CSTRING(result);
234 /*****************************************************************************
236 *****************************************************************************/
239 cideq(PG_FUNCTION_ARGS)
241 /* XXX assume that CommandId is 32 bits... */
242 CommandId arg1 = PG_GETARG_INT32(0);
243 CommandId arg2 = PG_GETARG_INT32(1);
245 PG_RETURN_BOOL(arg1 == arg2);