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-2006, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $PostgreSQL: pgsql/src/backend/utils/adt/char.c,v 1.44 2006/03/05 15:58:41 momjian Exp $
14 *-------------------------------------------------------------------------
20 #include "libpq/pqformat.h"
21 #include "utils/builtins.h"
24 #define SCHAR_MAX (0x7F)
27 #define SCHAR_MIN (-SCHAR_MAX-1)
31 /*****************************************************************************
33 *****************************************************************************/
36 * charin - converts "x" to 'x'
38 * Note that an empty input string will implicitly be converted to \0.
41 charin(PG_FUNCTION_ARGS)
43 char *ch = PG_GETARG_CSTRING(0);
45 PG_RETURN_CHAR(ch[0]);
49 * charout - converts 'x' to "x"
51 * Note that if the char value is \0, the resulting string will appear
52 * to be empty (null-terminated after zero characters). So this is the
53 * inverse of the charin() function for such data.
56 charout(PG_FUNCTION_ARGS)
58 char ch = PG_GETARG_CHAR(0);
59 char *result = (char *) palloc(2);
63 PG_RETURN_CSTRING(result);
67 * charrecv - converts external binary format to char
69 * The external representation is one byte, with no character set
70 * conversion. This is somewhat dubious, perhaps, but in many
71 * cases people use char for a 1-byte binary type.
74 charrecv(PG_FUNCTION_ARGS)
76 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
78 PG_RETURN_CHAR(pq_getmsgbyte(buf));
82 * charsend - converts char to binary format
85 charsend(PG_FUNCTION_ARGS)
87 char arg1 = PG_GETARG_CHAR(0);
90 pq_begintypsend(&buf);
91 pq_sendbyte(&buf, arg1);
92 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
95 /*****************************************************************************
97 *****************************************************************************/
100 * NOTE: comparisons are done as though char is unsigned (uint8).
101 * Conversions to and from integer are done as though char is signed (int8).
103 * You wanted consistency?
107 chareq(PG_FUNCTION_ARGS)
109 char arg1 = PG_GETARG_CHAR(0);
110 char arg2 = PG_GETARG_CHAR(1);
112 PG_RETURN_BOOL(arg1 == arg2);
116 charne(PG_FUNCTION_ARGS)
118 char arg1 = PG_GETARG_CHAR(0);
119 char arg2 = PG_GETARG_CHAR(1);
121 PG_RETURN_BOOL(arg1 != arg2);
125 charlt(PG_FUNCTION_ARGS)
127 char arg1 = PG_GETARG_CHAR(0);
128 char arg2 = PG_GETARG_CHAR(1);
130 PG_RETURN_BOOL((uint8) arg1 < (uint8) arg2);
134 charle(PG_FUNCTION_ARGS)
136 char arg1 = PG_GETARG_CHAR(0);
137 char arg2 = PG_GETARG_CHAR(1);
139 PG_RETURN_BOOL((uint8) arg1 <= (uint8) arg2);
143 chargt(PG_FUNCTION_ARGS)
145 char arg1 = PG_GETARG_CHAR(0);
146 char arg2 = PG_GETARG_CHAR(1);
148 PG_RETURN_BOOL((uint8) arg1 > (uint8) arg2);
152 charge(PG_FUNCTION_ARGS)
154 char arg1 = PG_GETARG_CHAR(0);
155 char arg2 = PG_GETARG_CHAR(1);
157 PG_RETURN_BOOL((uint8) arg1 >= (uint8) arg2);
162 chartoi4(PG_FUNCTION_ARGS)
164 char arg1 = PG_GETARG_CHAR(0);
166 PG_RETURN_INT32((int32) ((int8) arg1));
170 i4tochar(PG_FUNCTION_ARGS)
172 int32 arg1 = PG_GETARG_INT32(0);
174 if (arg1 < SCHAR_MIN || arg1 > SCHAR_MAX)
176 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
177 errmsg("\"char\" out of range")));
179 PG_RETURN_CHAR((int8) arg1);
184 text_char(PG_FUNCTION_ARGS)
186 text *arg1 = PG_GETARG_TEXT_P(0);
190 * An empty input string is converted to \0 (for consistency with charin).
191 * If the input is longer than one character, the excess data is silently
194 if (VARSIZE(arg1) > VARHDRSZ)
195 result = *(VARDATA(arg1));
199 PG_RETURN_CHAR(result);
203 char_text(PG_FUNCTION_ARGS)
205 char arg1 = PG_GETARG_CHAR(0);
206 text *result = palloc(VARHDRSZ + 1);
209 * Convert \0 to an empty string, for consistency with charout (and
210 * because the text stuff doesn't like embedded nulls all that well).
214 VARATT_SIZEP(result) = VARHDRSZ + 1;
215 *(VARDATA(result)) = arg1;
218 VARATT_SIZEP(result) = VARHDRSZ;
220 PG_RETURN_TEXT_P(result);