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

Go to the source code of this file.

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)
 

Function Documentation

◆ char_text()

Datum char_text ( PG_FUNCTION_ARGS  )

Definition at line 195 of file char.c.

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

196 {
197  char arg1 = PG_GETARG_CHAR(0);
198  text *result = palloc(VARHDRSZ + 1);
199 
200  /*
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).
203  */
204  if (arg1 != '\0')
205  {
206  SET_VARSIZE(result, VARHDRSZ + 1);
207  *(VARDATA(result)) = arg1;
208  }
209  else
210  SET_VARSIZE(result, VARHDRSZ);
211 
212  PG_RETURN_TEXT_P(result);
213 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:493
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
void * palloc(Size size)
Definition: mcxt.c:848
Definition: c.h:487
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:238

◆ chareq()

Datum chareq ( PG_FUNCTION_ARGS  )

Definition at line 99 of file char.c.

References PG_GETARG_CHAR, and PG_RETURN_BOOL.

100 {
101  char arg1 = PG_GETARG_CHAR(0);
102  char arg2 = PG_GETARG_CHAR(1);
103 
104  PG_RETURN_BOOL(arg1 == arg2);
105 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:238

◆ charge()

Datum charge ( PG_FUNCTION_ARGS  )

Definition at line 144 of file char.c.

References PG_GETARG_CHAR, and PG_RETURN_BOOL.

145 {
146  char arg1 = PG_GETARG_CHAR(0);
147  char arg2 = PG_GETARG_CHAR(1);
148 
149  PG_RETURN_BOOL((uint8) arg1 >= (uint8) arg2);
150 }
unsigned char uint8
Definition: c.h:294
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:238

◆ chargt()

Datum chargt ( PG_FUNCTION_ARGS  )

Definition at line 135 of file char.c.

References PG_GETARG_CHAR, and PG_RETURN_BOOL.

136 {
137  char arg1 = PG_GETARG_CHAR(0);
138  char arg2 = PG_GETARG_CHAR(1);
139 
140  PG_RETURN_BOOL((uint8) arg1 > (uint8) arg2);
141 }
unsigned char uint8
Definition: c.h:294
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:238

◆ charin()

Datum charin ( PG_FUNCTION_ARGS  )

Definition at line 33 of file char.c.

References PG_GETARG_CSTRING, and PG_RETURN_CHAR.

34 {
35  char *ch = PG_GETARG_CSTRING(0);
36 
37  PG_RETURN_CHAR(ch[0]);
38 }
#define PG_RETURN_CHAR(x)
Definition: fmgr.h:318
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242

◆ charle()

Datum charle ( PG_FUNCTION_ARGS  )

Definition at line 126 of file char.c.

References PG_GETARG_CHAR, and PG_RETURN_BOOL.

127 {
128  char arg1 = PG_GETARG_CHAR(0);
129  char arg2 = PG_GETARG_CHAR(1);
130 
131  PG_RETURN_BOOL((uint8) arg1 <= (uint8) arg2);
132 }
unsigned char uint8
Definition: c.h:294
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:238

◆ charlt()

Datum charlt ( PG_FUNCTION_ARGS  )

Definition at line 117 of file char.c.

References PG_GETARG_CHAR, and PG_RETURN_BOOL.

118 {
119  char arg1 = PG_GETARG_CHAR(0);
120  char arg2 = PG_GETARG_CHAR(1);
121 
122  PG_RETURN_BOOL((uint8) arg1 < (uint8) arg2);
123 }
unsigned char uint8
Definition: c.h:294
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:238

◆ charne()

Datum charne ( PG_FUNCTION_ARGS  )

Definition at line 108 of file char.c.

References PG_GETARG_CHAR, and PG_RETURN_BOOL.

109 {
110  char arg1 = PG_GETARG_CHAR(0);
111  char arg2 = PG_GETARG_CHAR(1);
112 
113  PG_RETURN_BOOL(arg1 != arg2);
114 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:238

◆ charout()

Datum charout ( PG_FUNCTION_ARGS  )

Definition at line 48 of file char.c.

References palloc(), PG_GETARG_CHAR, and PG_RETURN_CSTRING.

49 {
50  char ch = PG_GETARG_CHAR(0);
51  char *result = (char *) palloc(2);
52 
53  result[0] = ch;
54  result[1] = '\0';
55  PG_RETURN_CSTRING(result);
56 }
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
void * palloc(Size size)
Definition: mcxt.c:848
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:238

◆ charrecv()

Datum charrecv ( PG_FUNCTION_ARGS  )

Definition at line 66 of file char.c.

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

67 {
69 
71 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define PG_RETURN_CHAR(x)
Definition: fmgr.h:318
static char * buf
Definition: pg_test_fsync.c:67
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401

◆ charsend()

Datum charsend ( PG_FUNCTION_ARGS  )

Definition at line 77 of file char.c.

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

78 {
79  char arg1 = PG_GETARG_CHAR(0);
81 
82  pq_begintypsend(&buf);
83  pq_sendbyte(&buf, arg1);
85 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
static void pq_sendbyte(StringInfo buf, int8 byt)
Definition: pqformat.h:164
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:238

◆ chartoi4()

Datum chartoi4 ( PG_FUNCTION_ARGS  )

Definition at line 154 of file char.c.

References PG_GETARG_CHAR, and PG_RETURN_INT32.

155 {
156  char arg1 = PG_GETARG_CHAR(0);
157 
158  PG_RETURN_INT32((int32) ((int8) arg1));
159 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
signed int int32
Definition: c.h:284
signed char int8
Definition: c.h:282
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:238

◆ i4tochar()

Datum i4tochar ( PG_FUNCTION_ARGS  )

Definition at line 162 of file char.c.

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

163 {
164  int32 arg1 = PG_GETARG_INT32(0);
165 
166  if (arg1 < SCHAR_MIN || arg1 > SCHAR_MAX)
167  ereport(ERROR,
168  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
169  errmsg("\"char\" out of range")));
170 
171  PG_RETURN_CHAR((int8) arg1);
172 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define PG_RETURN_CHAR(x)
Definition: fmgr.h:318
#define ereport(elevel, rest)
Definition: elog.h:122
signed char int8
Definition: c.h:282
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ text_char()

Datum text_char ( PG_FUNCTION_ARGS  )

Definition at line 176 of file char.c.

References PG_GETARG_TEXT_PP, PG_RETURN_CHAR, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

177 {
178  text *arg1 = PG_GETARG_TEXT_PP(0);
179  char result;
180 
181  /*
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
184  * discarded.
185  */
186  if (VARSIZE_ANY_EXHDR(arg1) > 0)
187  result = *(VARDATA_ANY(arg1));
188  else
189  result = '\0';
190 
191  PG_RETURN_CHAR(result);
192 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_CHAR(x)
Definition: fmgr.h:318
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:487