PostgreSQL Source Code  git master
char.c File Reference
#include "postgres.h"
#include <limits.h>
#include "libpq/pqformat.h"
#include "utils/fmgrprotos.h"
#include "varatt.h"
Include dependency graph for char.c:

Go to the source code of this file.

Macros

#define ISOCTAL(c)   (((c) >= '0') && ((c) <= '7'))
 
#define TOOCTAL(c)   ((c) + '0')
 
#define FROMOCTAL(c)   ((unsigned char) (c) - '0')
 

Functions

Datum charin (PG_FUNCTION_ARGS)
 
Datum charout (PG_FUNCTION_ARGS)
 
Datum charrecv (PG_FUNCTION_ARGS)
 
Datum charsend (PG_FUNCTION_ARGS)
 
Datum chareq (PG_FUNCTION_ARGS)
 
Datum charne (PG_FUNCTION_ARGS)
 
Datum charlt (PG_FUNCTION_ARGS)
 
Datum charle (PG_FUNCTION_ARGS)
 
Datum chargt (PG_FUNCTION_ARGS)
 
Datum charge (PG_FUNCTION_ARGS)
 
Datum chartoi4 (PG_FUNCTION_ARGS)
 
Datum i4tochar (PG_FUNCTION_ARGS)
 
Datum text_char (PG_FUNCTION_ARGS)
 
Datum char_text (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ FROMOCTAL

#define FROMOCTAL (   c)    ((unsigned char) (c) - '0')

Definition at line 26 of file char.c.

◆ ISOCTAL

#define ISOCTAL (   c)    (((c) >= '0') && ((c) <= '7'))

Definition at line 24 of file char.c.

◆ TOOCTAL

#define TOOCTAL (   c)    ((c) + '0')

Definition at line 25 of file char.c.

Function Documentation

◆ char_text()

Datum char_text ( PG_FUNCTION_ARGS  )

Definition at line 228 of file char.c.

229 {
230  char arg1 = PG_GETARG_CHAR(0);
231  text *result = palloc(VARHDRSZ + 4);
232 
233  /*
234  * Conversion rules are the same as in charout(), but here we need to be
235  * honest about converting 0x00 to an empty string.
236  */
237  if (IS_HIGHBIT_SET(arg1))
238  {
239  SET_VARSIZE(result, VARHDRSZ + 4);
240  (VARDATA(result))[0] = '\\';
241  (VARDATA(result))[1] = TOOCTAL(((unsigned char) arg1) >> 6);
242  (VARDATA(result))[2] = TOOCTAL((((unsigned char) arg1) >> 3) & 07);
243  (VARDATA(result))[3] = TOOCTAL(((unsigned char) arg1) & 07);
244  }
245  else if (arg1 != '\0')
246  {
247  SET_VARSIZE(result, VARHDRSZ + 1);
248  *(VARDATA(result)) = arg1;
249  }
250  else
251  SET_VARSIZE(result, VARHDRSZ);
252 
253  PG_RETURN_TEXT_P(result);
254 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1109
#define VARHDRSZ
Definition: c.h:646
#define TOOCTAL(c)
Definition: char.c:25
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:273
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
void * palloc(Size size)
Definition: mcxt.c:1317
Definition: c.h:641
#define VARDATA(PTR)
Definition: varatt.h:278
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References IS_HIGHBIT_SET, palloc(), PG_GETARG_CHAR, PG_RETURN_TEXT_P, SET_VARSIZE, TOOCTAL, VARDATA, and VARHDRSZ.

◆ chareq()

Datum chareq ( PG_FUNCTION_ARGS  )

Definition at line 127 of file char.c.

128 {
129  char arg1 = PG_GETARG_CHAR(0);
130  char arg2 = PG_GETARG_CHAR(1);
131 
132  PG_RETURN_BOOL(arg1 == arg2);
133 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References PG_GETARG_CHAR, and PG_RETURN_BOOL.

◆ charge()

Datum charge ( PG_FUNCTION_ARGS  )

Definition at line 172 of file char.c.

173 {
174  char arg1 = PG_GETARG_CHAR(0);
175  char arg2 = PG_GETARG_CHAR(1);
176 
177  PG_RETURN_BOOL((uint8) arg1 >= (uint8) arg2);
178 }
uint8_t uint8
Definition: c.h:483

References PG_GETARG_CHAR, and PG_RETURN_BOOL.

◆ chargt()

Datum chargt ( PG_FUNCTION_ARGS  )

Definition at line 163 of file char.c.

164 {
165  char arg1 = PG_GETARG_CHAR(0);
166  char arg2 = PG_GETARG_CHAR(1);
167 
168  PG_RETURN_BOOL((uint8) arg1 > (uint8) arg2);
169 }

References PG_GETARG_CHAR, and PG_RETURN_BOOL.

◆ charin()

Datum charin ( PG_FUNCTION_ARGS  )

Definition at line 41 of file char.c.

42 {
43  char *ch = PG_GETARG_CSTRING(0);
44 
45  if (strlen(ch) == 4 && ch[0] == '\\' &&
46  ISOCTAL(ch[1]) && ISOCTAL(ch[2]) && ISOCTAL(ch[3]))
47  PG_RETURN_CHAR((FROMOCTAL(ch[1]) << 6) +
48  (FROMOCTAL(ch[2]) << 3) +
49  FROMOCTAL(ch[3]));
50  /* This will do the right thing for a zero-length input string */
51  PG_RETURN_CHAR(ch[0]);
52 }
#define ISOCTAL(c)
Definition: char.c:24
#define FROMOCTAL(c)
Definition: char.c:26
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define PG_RETURN_CHAR(x)
Definition: fmgr.h:358

References FROMOCTAL, ISOCTAL, PG_GETARG_CSTRING, and PG_RETURN_CHAR.

◆ charle()

Datum charle ( PG_FUNCTION_ARGS  )

Definition at line 154 of file char.c.

155 {
156  char arg1 = PG_GETARG_CHAR(0);
157  char arg2 = PG_GETARG_CHAR(1);
158 
159  PG_RETURN_BOOL((uint8) arg1 <= (uint8) arg2);
160 }

References PG_GETARG_CHAR, and PG_RETURN_BOOL.

◆ charlt()

Datum charlt ( PG_FUNCTION_ARGS  )

Definition at line 145 of file char.c.

146 {
147  char arg1 = PG_GETARG_CHAR(0);
148  char arg2 = PG_GETARG_CHAR(1);
149 
150  PG_RETURN_BOOL((uint8) arg1 < (uint8) arg2);
151 }

References PG_GETARG_CHAR, and PG_RETURN_BOOL.

◆ charne()

Datum charne ( PG_FUNCTION_ARGS  )

Definition at line 136 of file char.c.

137 {
138  char arg1 = PG_GETARG_CHAR(0);
139  char arg2 = PG_GETARG_CHAR(1);
140 
141  PG_RETURN_BOOL(arg1 != arg2);
142 }

References PG_GETARG_CHAR, and PG_RETURN_BOOL.

◆ charout()

Datum charout ( PG_FUNCTION_ARGS  )

Definition at line 64 of file char.c.

65 {
66  char ch = PG_GETARG_CHAR(0);
67  char *result = (char *) palloc(5);
68 
69  if (IS_HIGHBIT_SET(ch))
70  {
71  result[0] = '\\';
72  result[1] = TOOCTAL(((unsigned char) ch) >> 6);
73  result[2] = TOOCTAL((((unsigned char) ch) >> 3) & 07);
74  result[3] = TOOCTAL(((unsigned char) ch) & 07);
75  result[4] = '\0';
76  }
77  else
78  {
79  /* This produces acceptable results for 0x00 as well */
80  result[0] = ch;
81  result[1] = '\0';
82  }
83  PG_RETURN_CSTRING(result);
84 }
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362

References IS_HIGHBIT_SET, palloc(), PG_GETARG_CHAR, PG_RETURN_CSTRING, and TOOCTAL.

◆ charrecv()

Datum charrecv ( PG_FUNCTION_ARGS  )

Definition at line 94 of file char.c.

95 {
97 
99 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static char * buf
Definition: pg_test_fsync.c:72
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:399
StringInfoData * StringInfo
Definition: stringinfo.h:54

References buf, PG_GETARG_POINTER, PG_RETURN_CHAR, and pq_getmsgbyte().

◆ charsend()

Datum charsend ( PG_FUNCTION_ARGS  )

Definition at line 105 of file char.c.

106 {
107  char arg1 = PG_GETARG_CHAR(0);
109 
111  pq_sendbyte(&buf, arg1);
113 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:160

References buf, PG_GETARG_CHAR, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendbyte().

◆ chartoi4()

Datum chartoi4 ( PG_FUNCTION_ARGS  )

Definition at line 182 of file char.c.

183 {
184  char arg1 = PG_GETARG_CHAR(0);
185 
186  PG_RETURN_INT32((int32) ((int8) arg1));
187 }
int8_t int8
Definition: c.h:479
int32_t int32
Definition: c.h:481
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354

References PG_GETARG_CHAR, and PG_RETURN_INT32.

◆ i4tochar()

Datum i4tochar ( PG_FUNCTION_ARGS  )

Definition at line 190 of file char.c.

191 {
192  int32 arg1 = PG_GETARG_INT32(0);
193 
194  if (arg1 < SCHAR_MIN || arg1 > SCHAR_MAX)
195  ereport(ERROR,
196  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
197  errmsg("\"char\" out of range")));
198 
199  PG_RETURN_CHAR((int8) arg1);
200 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, and PG_RETURN_CHAR.

◆ text_char()

Datum text_char ( PG_FUNCTION_ARGS  )

Definition at line 204 of file char.c.

205 {
206  text *arg1 = PG_GETARG_TEXT_PP(0);
207  char *ch = VARDATA_ANY(arg1);
208  char result;
209 
210  /*
211  * Conversion rules are the same as in charin(), but here we need to
212  * handle the empty-string case honestly.
213  */
214  if (VARSIZE_ANY_EXHDR(arg1) == 4 && ch[0] == '\\' &&
215  ISOCTAL(ch[1]) && ISOCTAL(ch[2]) && ISOCTAL(ch[3]))
216  result = (FROMOCTAL(ch[1]) << 6) +
217  (FROMOCTAL(ch[2]) << 3) +
218  FROMOCTAL(ch[3]);
219  else if (VARSIZE_ANY_EXHDR(arg1) > 0)
220  result = ch[0];
221  else
222  result = '\0';
223 
224  PG_RETURN_CHAR(result);
225 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

References FROMOCTAL, ISOCTAL, PG_GETARG_TEXT_PP, PG_RETURN_CHAR, VARDATA_ANY, and VARSIZE_ANY_EXHDR.