1 /*-----------------------------------------------------------------------
3 * The PostgreSQL routine for string to ascii conversion.
5 * Portions Copyright (c) 1999-2006, PostgreSQL Global Development Group
8 * $PostgreSQL: pgsql/src/backend/utils/adt/ascii.c,v 1.29 2006/07/14 14:52:23 momjian Exp $
10 *-----------------------------------------------------------------------
14 #include "mb/pg_wchar.h"
15 #include "utils/ascii.h"
17 static void pg_to_ascii(unsigned char *src, unsigned char *src_end,
18 unsigned char *dest, int enc);
19 static text *encode_to_ascii(text *data, int enc);
27 pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *dest, int enc)
30 const unsigned char *ascii;
34 * relevant start for an encoding
42 * ISO-8859-1 <range: 160 -- 255>
44 ascii = (const unsigned char *) " cL Y \"Ca -R 'u ., ?AAAAAAACEEEEIIII NOOOOOxOUUUUYTBaaaaaaaceeeeiiii nooooo/ouuuuyty";
47 else if (enc == PG_LATIN2)
50 * ISO-8859-2 <range: 160 -- 255>
52 ascii = (const unsigned char *) " A L LS \"SSTZ-ZZ a,l'ls ,sstz\"zzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTBraaaalccceeeeiiddnnoooo/ruuuuyt.";
55 else if (enc == PG_LATIN9)
58 * ISO-8859-15 <range: 160 -- 255>
60 ascii = (const unsigned char *) " cL YS sCa -R Zu .z EeY?AAAAAAACEEEEIIII NOOOOOxOUUUUYTBaaaaaaaceeeeiiii nooooo/ouuuuyty";
63 else if (enc == PG_WIN1250)
66 * Window CP1250 <range: 128 -- 255>
68 ascii = (const unsigned char *) " ' \" %S<STZZ `'\"\".-- s>stzz L A \"CS -RZ ,l'u .,as L\"lzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTBraaaalccceeeeiiddnnoooo/ruuuuyt ";
74 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
75 errmsg("encoding conversion from %s to ASCII not supported",
76 pg_encoding_to_char(enc))));
77 return; /* keep compiler quiet */
83 for (x = src; x < src_end; x++)
88 *dest++ = ' '; /* bogus 128 to 'range' */
90 *dest++ = ascii[*x - range];
97 * The text datum is overwritten in-place, therefore this coding method
98 * cannot support conversions that change the string length!
102 encode_to_ascii(text *data, int enc)
104 pg_to_ascii((unsigned char *) VARDATA(data), /* src */
105 (unsigned char *) (data) + VARSIZE(data), /* src end */
106 (unsigned char *) VARDATA(data), /* dest */
113 * convert to ASCII - enc is set as 'name' arg.
117 to_ascii_encname(PG_FUNCTION_ARGS)
119 text *data = PG_GETARG_TEXT_P_COPY(0);
120 int enc = pg_char_to_encoding(NameStr(*PG_GETARG_NAME(1)));
122 PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
126 * convert to ASCII - enc is set as int4
130 to_ascii_enc(PG_FUNCTION_ARGS)
132 text *data = PG_GETARG_TEXT_P_COPY(0);
133 int enc = PG_GETARG_INT32(1);
135 PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
139 * convert to ASCII - current enc is DatabaseEncoding
143 to_ascii_default(PG_FUNCTION_ARGS)
145 text *data = PG_GETARG_TEXT_P_COPY(0);
146 int enc = GetDatabaseEncoding();
148 PG_RETURN_TEXT_P(encode_to_ascii(data, enc));