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-2016, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * src/backend/utils/adt/char.c
14 *-------------------------------------------------------------------------
20 #include "libpq/pqformat.h"
21 #include "utils/builtins.h"
23 /*****************************************************************************
25 *****************************************************************************/
28 * charin - converts "x" to 'x'
30 * Note that an empty input string will implicitly be converted to \0.
33 charin(PG_FUNCTION_ARGS)
35 char *ch = PG_GETARG_CSTRING(0);
37 PG_RETURN_CHAR(ch[0]);
41 * charout - converts 'x' to "x"
43 * Note that if the char value is \0, the resulting string will appear
44 * to be empty (null-terminated after zero characters). So this is the
45 * inverse of the charin() function for such data.
48 charout(PG_FUNCTION_ARGS)
50 char ch = PG_GETARG_CHAR(0);
51 char *result = (char *) palloc(2);
55 PG_RETURN_CSTRING(result);
59 * charrecv - converts external binary format to char
61 * The external representation is one byte, with no character set
62 * conversion. This is somewhat dubious, perhaps, but in many
63 * cases people use char for a 1-byte binary type.
66 charrecv(PG_FUNCTION_ARGS)
68 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
70 PG_RETURN_CHAR(pq_getmsgbyte(buf));
74 * charsend - converts char to binary format
77 charsend(PG_FUNCTION_ARGS)
79 char arg1 = PG_GETARG_CHAR(0);
82 pq_begintypsend(&buf);
83 pq_sendbyte(&buf, arg1);
84 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
87 /*****************************************************************************
89 *****************************************************************************/
92 * NOTE: comparisons are done as though char is unsigned (uint8).
93 * Conversions to and from integer are done as though char is signed (int8).
95 * You wanted consistency?
99 chareq(PG_FUNCTION_ARGS)
101 char arg1 = PG_GETARG_CHAR(0);
102 char arg2 = PG_GETARG_CHAR(1);
104 PG_RETURN_BOOL(arg1 == arg2);
108 charne(PG_FUNCTION_ARGS)
110 char arg1 = PG_GETARG_CHAR(0);
111 char arg2 = PG_GETARG_CHAR(1);
113 PG_RETURN_BOOL(arg1 != arg2);
117 charlt(PG_FUNCTION_ARGS)
119 char arg1 = PG_GETARG_CHAR(0);
120 char arg2 = PG_GETARG_CHAR(1);
122 PG_RETURN_BOOL((uint8) arg1 < (uint8) arg2);
126 charle(PG_FUNCTION_ARGS)
128 char arg1 = PG_GETARG_CHAR(0);
129 char arg2 = PG_GETARG_CHAR(1);
131 PG_RETURN_BOOL((uint8) arg1 <= (uint8) arg2);
135 chargt(PG_FUNCTION_ARGS)
137 char arg1 = PG_GETARG_CHAR(0);
138 char arg2 = PG_GETARG_CHAR(1);
140 PG_RETURN_BOOL((uint8) arg1 > (uint8) arg2);
144 charge(PG_FUNCTION_ARGS)
146 char arg1 = PG_GETARG_CHAR(0);
147 char arg2 = PG_GETARG_CHAR(1);
149 PG_RETURN_BOOL((uint8) arg1 >= (uint8) arg2);
154 chartoi4(PG_FUNCTION_ARGS)
156 char arg1 = PG_GETARG_CHAR(0);
158 PG_RETURN_INT32((int32) ((int8) arg1));
162 i4tochar(PG_FUNCTION_ARGS)
164 int32 arg1 = PG_GETARG_INT32(0);
166 if (arg1 < SCHAR_MIN || arg1 > SCHAR_MAX)
168 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
169 errmsg("\"char\" out of range")));
171 PG_RETURN_CHAR((int8) arg1);
176 text_char(PG_FUNCTION_ARGS)
178 text *arg1 = PG_GETARG_TEXT_P(0);
182 * An empty input string is converted to \0 (for consistency with charin).
183 * If the input is longer than one character, the excess data is silently
186 if (VARSIZE(arg1) > VARHDRSZ)
187 result = *(VARDATA(arg1));
191 PG_RETURN_CHAR(result);
195 char_text(PG_FUNCTION_ARGS)
197 char arg1 = PG_GETARG_CHAR(0);
198 text *result = palloc(VARHDRSZ + 1);
201 * Convert \0 to an empty string, for consistency with charout (and
202 * because the text stuff doesn't like embedded nulls all that well).
206 SET_VARSIZE(result, VARHDRSZ + 1);
207 *(VARDATA(result)) = arg1;
210 SET_VARSIZE(result, VARHDRSZ);
212 PG_RETURN_TEXT_P(result);