1
/*-----------------------------------------------------------------------
3
* The PostgreSQL routine for string to ascii conversion.
5
* Portions Copyright (c) 1999-2005, PostgreSQL Global Development Group
8
* $PostgreSQL: pgsql/src/backend/utils/adt/ascii.c,v 1.24 2005-01-01 05:43:07 momjian Exp $
10
*-----------------------------------------------------------------------
14
#include "utils/builtins.h"
15
#include "mb/pg_wchar.h"
16
#include "utils/ascii.h"
18
static void pg_to_ascii(unsigned char *src, unsigned char *src_end,
19
unsigned char *dest, int enc);
20
static text *encode_to_ascii(text *data, int enc);
28
pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *dest, int enc)
31
const unsigned char *ascii;
35
* relevant start for an encoding
43
* ISO-8859-1 <range: 160 -- 255>
45
ascii = " cL Y \"Ca -R 'u ., ?AAAAAAACEEEEIIII NOOOOOxOUUUUYTBaaaaaaaceeeeiiii nooooo/ouuuuyty";
48
else if (enc == PG_LATIN2)
51
* ISO-8859-2 <range: 160 -- 255>
53
ascii = " A L LS \"SSTZ-ZZ a,l'ls ,sstz\"zzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTBraaaalccceeeeiiddnnoooo/ruuuuyt.";
56
else if (enc == PG_LATIN9)
59
* ISO-8859-15 <range: 160 -- 255>
61
ascii = " cL YS sCa -R Zu .z EeY?AAAAAAACEEEEIIII NOOOOOxOUUUUYTBaaaaaaaceeeeiiii nooooo/ouuuuyty";
64
else if (enc == PG_WIN1250)
67
* Window CP1250 <range: 128 -- 255>
69
ascii = " ' \" %S<STZZ `'\"\".-- s>stzz L A \"CS -RZ ,l'u .,as L\"lzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTBraaaalccceeeeiiddnnoooo/ruuuuyt ";
75
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
76
errmsg("encoding conversion from %s to ASCII not supported",
77
pg_encoding_to_char(enc))));
78
return; /* keep compiler quiet */
84
for (x = src; x < src_end; x++)
89
*dest++ = ' '; /* bogus 128 to 'range' */
91
*dest++ = ascii[*x - range];
98
* The text datum is overwritten in-place, therefore this coding method
99
* cannot support conversions that change the string length!
103
encode_to_ascii(text *data, int enc)
105
pg_to_ascii((unsigned char *) VARDATA(data), /* src */
106
(unsigned char *) (data) + VARSIZE(data), /* src end */
107
(unsigned char *) VARDATA(data), /* dest */
114
* convert to ASCII - enc is set as 'name' arg.
118
to_ascii_encname(PG_FUNCTION_ARGS)
120
text *data = PG_GETARG_TEXT_P_COPY(0);
121
int enc = pg_char_to_encoding(NameStr(*PG_GETARG_NAME(1)));
123
PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
127
* convert to ASCII - enc is set as int4
131
to_ascii_enc(PG_FUNCTION_ARGS)
133
text *data = PG_GETARG_TEXT_P_COPY(0);
134
int enc = PG_GETARG_INT32(1);
136
PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
140
* convert to ASCII - current enc is DatabaseEncoding
144
to_ascii_default(PG_FUNCTION_ARGS)
146
text *data = PG_GETARG_TEXT_P_COPY(0);
147
int enc = GetDatabaseEncoding();
149
PG_RETURN_TEXT_P(encode_to_ascii(data, enc));