]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/char.c
Make text <=> char conversion functions convert zero character to and
[postgresql] / src / backend / utils / adt / char.c
1 /*-------------------------------------------------------------------------
2  *
3  * char.c
4  *        Functions for the built-in type "char".
5  *        Functions for the built-in type "cid" (what's that doing here?)
6  *
7  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *
11  * IDENTIFICATION
12  *        $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.31 2001/05/28 21:58:32 tgl Exp $
13  *
14  *-------------------------------------------------------------------------
15  */
16 #include "postgres.h"
17
18 #include "utils/builtins.h"
19
20 /*****************************************************************************
21  *       USER I/O ROUTINES                                                                                                               *
22  *****************************************************************************/
23
24 /*
25  *              charin                  - converts "x" to 'x'
26  *
27  * Note that an empty input string will implicitly be converted to \0.
28  */
29 Datum
30 charin(PG_FUNCTION_ARGS)
31 {
32         char       *ch = PG_GETARG_CSTRING(0);
33
34         PG_RETURN_CHAR(ch[0]);
35 }
36
37 /*
38  *              charout                 - converts 'x' to "x"
39  *
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.
43  */
44 Datum
45 charout(PG_FUNCTION_ARGS)
46 {
47         char            ch = PG_GETARG_CHAR(0);
48         char       *result = (char *) palloc(2);
49
50         result[0] = ch;
51         result[1] = '\0';
52         PG_RETURN_CSTRING(result);
53 }
54
55 /*****************************************************************************
56  *       PUBLIC ROUTINES                                                                                                                 *
57  *****************************************************************************/
58
59 /*
60  * NOTE: comparisons are done as though char is unsigned (uint8).
61  * Arithmetic is done as though char is signed (int8).
62  *
63  * You wanted consistency?
64  */
65
66 Datum
67 chareq(PG_FUNCTION_ARGS)
68 {
69         char            arg1 = PG_GETARG_CHAR(0);
70         char            arg2 = PG_GETARG_CHAR(1);
71
72         PG_RETURN_BOOL(arg1 == arg2);
73 }
74
75 Datum
76 charne(PG_FUNCTION_ARGS)
77 {
78         char            arg1 = PG_GETARG_CHAR(0);
79         char            arg2 = PG_GETARG_CHAR(1);
80
81         PG_RETURN_BOOL(arg1 != arg2);
82 }
83
84 Datum
85 charlt(PG_FUNCTION_ARGS)
86 {
87         char            arg1 = PG_GETARG_CHAR(0);
88         char            arg2 = PG_GETARG_CHAR(1);
89
90         PG_RETURN_BOOL((uint8) arg1 < (uint8) arg2);
91 }
92
93 Datum
94 charle(PG_FUNCTION_ARGS)
95 {
96         char            arg1 = PG_GETARG_CHAR(0);
97         char            arg2 = PG_GETARG_CHAR(1);
98
99         PG_RETURN_BOOL((uint8) arg1 <= (uint8) arg2);
100 }
101
102 Datum
103 chargt(PG_FUNCTION_ARGS)
104 {
105         char            arg1 = PG_GETARG_CHAR(0);
106         char            arg2 = PG_GETARG_CHAR(1);
107
108         PG_RETURN_BOOL((uint8) arg1 > (uint8) arg2);
109 }
110
111 Datum
112 charge(PG_FUNCTION_ARGS)
113 {
114         char            arg1 = PG_GETARG_CHAR(0);
115         char            arg2 = PG_GETARG_CHAR(1);
116
117         PG_RETURN_BOOL((uint8) arg1 >= (uint8) arg2);
118 }
119
120 Datum
121 charpl(PG_FUNCTION_ARGS)
122 {
123         char            arg1 = PG_GETARG_CHAR(0);
124         char            arg2 = PG_GETARG_CHAR(1);
125
126         PG_RETURN_CHAR((int8) arg1 + (int8) arg2);
127 }
128
129 Datum
130 charmi(PG_FUNCTION_ARGS)
131 {
132         char            arg1 = PG_GETARG_CHAR(0);
133         char            arg2 = PG_GETARG_CHAR(1);
134
135         PG_RETURN_CHAR((int8) arg1 - (int8) arg2);
136 }
137
138 Datum
139 charmul(PG_FUNCTION_ARGS)
140 {
141         char            arg1 = PG_GETARG_CHAR(0);
142         char            arg2 = PG_GETARG_CHAR(1);
143
144         PG_RETURN_CHAR((int8) arg1 * (int8) arg2);
145 }
146
147 Datum
148 chardiv(PG_FUNCTION_ARGS)
149 {
150         char            arg1 = PG_GETARG_CHAR(0);
151         char            arg2 = PG_GETARG_CHAR(1);
152
153         PG_RETURN_CHAR((int8) arg1 / (int8) arg2);
154 }
155
156
157 Datum
158 text_char(PG_FUNCTION_ARGS)
159 {
160         text       *arg1 = PG_GETARG_TEXT_P(0);
161         char            result;
162
163         /*
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
166          * discarded.
167          */
168         if (VARSIZE(arg1) > VARHDRSZ)
169                 result = *(VARDATA(arg1));
170         else
171                 result = '\0';
172
173         PG_RETURN_CHAR(result);
174 }
175
176 Datum
177 char_text(PG_FUNCTION_ARGS)
178 {
179         char            arg1 = PG_GETARG_CHAR(0);
180         text       *result = palloc(VARHDRSZ + 1);
181
182         /*
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).
185          */
186         if (arg1 != '\0')
187         {
188                 VARATT_SIZEP(result) = VARHDRSZ + 1;
189                 *(VARDATA(result)) = arg1;
190         }
191         else
192         {
193                 VARATT_SIZEP(result) = VARHDRSZ;
194         }
195
196         PG_RETURN_TEXT_P(result);
197 }
198
199
200 /*****************************************************************************
201  *       USER I/O ROUTINES                                                                                                               *
202  *****************************************************************************/
203
204 /*
205  *              cidin   - converts CommandId to internal representation.
206  */
207 Datum
208 cidin(PG_FUNCTION_ARGS)
209 {
210         char       *s = PG_GETARG_CSTRING(0);
211         CommandId       c;
212
213         c = atoi(s);
214
215         /* XXX assume that CommandId is 32 bits... */
216         PG_RETURN_INT32((int32) c);
217 }
218
219 /*
220  *              cidout  - converts a cid to external representation.
221  */
222 Datum
223 cidout(PG_FUNCTION_ARGS)
224 {
225         /* XXX assume that CommandId is 32 bits... */
226         CommandId       c = PG_GETARG_INT32(0);
227         char       *result = (char *) palloc(16);
228
229         sprintf(result, "%u", (unsigned int) c);
230         PG_RETURN_CSTRING(result);
231 }
232
233 /*****************************************************************************
234  *       PUBLIC ROUTINES                                                                                                                 *
235  *****************************************************************************/
236
237 Datum
238 cideq(PG_FUNCTION_ARGS)
239 {
240         /* XXX assume that CommandId is 32 bits... */
241         CommandId       arg1 = PG_GETARG_INT32(0);
242         CommandId       arg2 = PG_GETARG_INT32(1);
243
244         PG_RETURN_BOOL(arg1 == arg2);
245 }