1- /* -----------------------------------------------------------------------
1+ /*-----------------------------------------------------------------------
22 * ascii.c
3+ * The PostgreSQL routine for string to ascii conversion.
34 *
4- * $Header: /cvsroot/pgsql/src/backend/utils/adt/ascii.c,v 1.14 2003/04/02 21:07:59 tgl Exp $
5- *
6- * Portions Copyright (c) 1999-2000, PostgreSQL Global Development Group
7- *
5+ * Portions Copyright (c) 1999-2002, PostgreSQL Global Development Group
86 *
9- * TO_ASCII()
7+ * IDENTIFICATION
8+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ascii.c,v 1.15 2003/07/14 16:41:38 tgl Exp $
109 *
11- * The PostgreSQL routine for string to ascii conversion.
12- *
13- * -----------------------------------------------------------------------
10+ *-----------------------------------------------------------------------
1411 */
15-
1612#include "postgres.h"
13+
1714#include "utils/builtins.h"
1815#include "mb/pg_wchar.h"
1916#include "utils/ascii.h"
2017
18+ static void pg_to_ascii (unsignedchar * src ,unsignedchar * src_end ,
19+ unsignedchar * dest ,int enc );
2120static text * encode_to_ascii (text * data ,int enc );
2221
22+
2323/* ----------
2424 * to_ascii
2525 * ----------
2626 */
27- char *
28- pg_to_ascii (unsignedchar * src ,unsignedchar * src_end ,unsignedchar * desc ,int enc )
27+ static void
28+ pg_to_ascii (unsignedchar * src ,unsignedchar * src_end ,unsignedchar * dest ,int enc )
2929{
3030unsignedchar * x ;
3131unsignedchar * ascii ;
@@ -37,7 +37,6 @@ pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *desc, int
3737#define RANGE_128 128
3838#define RANGE_160 160
3939
40-
4140if (enc == PG_LATIN1 )
4241{
4342/*
@@ -64,9 +63,9 @@ pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *desc, int
6463}
6564else
6665{
67- elog (ERROR ,"pg_to_ascii(): unsupported encoding from %s" ,
66+ elog (ERROR ,"unsupported encodingconversion from %s to ASCII " ,
6867pg_encoding_to_char (enc ));
69- return NULL ; /* keep compiler quiet */
68+ return ; /* keep compiler quiet */
7069}
7170
7271/*
@@ -75,27 +74,27 @@ pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *desc, int
7574for (x = src ;x < src_end ;x ++ )
7675{
7776if (* x < 128 )
78- * desc ++ = * x ;
77+ * dest ++ = * x ;
7978else if (* x < range )
80- * desc ++ = ' ' ;/* bogus 128 to 'range' */
79+ * dest ++ = ' ' ;/* bogus 128 to 'range' */
8180else
82- * desc ++ = ascii [* x - range ];
81+ * dest ++ = ascii [* x - range ];
8382}
84-
85- return desc ;
8683}
8784
8885/* ----------
8986 * encode text
87+ *
88+ * The text datum is overwritten in-place, therefore this coding method
89+ * cannot support conversions that change the string length!
9090 * ----------
9191 */
9292static text *
9393encode_to_ascii (text * data ,int enc )
9494{
95- pg_to_ascii (
96- (unsignedchar * )VARDATA (data ),/* src */
97- VARDATA (data )+ VARSIZE (data ),/* src end */
98- (unsignedchar * )VARDATA (data ),/* desc */
95+ pg_to_ascii ((unsignedchar * )VARDATA (data ),/* src */
96+ (unsignedchar * ) (data )+ VARSIZE (data ),/* src end */
97+ (unsignedchar * )VARDATA (data ),/* dest */
9998enc );/* encoding */
10099
101100return data ;
@@ -108,14 +107,10 @@ encode_to_ascii(text *data, int enc)
108107Datum
109108to_ascii_encname (PG_FUNCTION_ARGS )
110109{
111- PG_RETURN_TEXT_P
112- (
113- encode_to_ascii
114- (
115- PG_GETARG_TEXT_P_COPY (0 ),
116- pg_char_to_encoding (NameStr (* PG_GETARG_NAME (1 )))
117- )
118- );
110+ text * data = PG_GETARG_TEXT_P_COPY (0 );
111+ int enc = pg_char_to_encoding (NameStr (* PG_GETARG_NAME (1 )));
112+
113+ PG_RETURN_TEXT_P (encode_to_ascii (data ,enc ));
119114}
120115
121116/* ----------
@@ -125,14 +120,10 @@ to_ascii_encname(PG_FUNCTION_ARGS)
125120Datum
126121to_ascii_enc (PG_FUNCTION_ARGS )
127122{
128- PG_RETURN_TEXT_P
129- (
130- encode_to_ascii
131- (
132- PG_GETARG_TEXT_P_COPY (0 ),
133- PG_GETARG_INT32 (1 )
134- )
135- );
123+ text * data = PG_GETARG_TEXT_P_COPY (0 );
124+ int enc = PG_GETARG_INT32 (1 );
125+
126+ PG_RETURN_TEXT_P (encode_to_ascii (data ,enc ));
136127}
137128
138129/* ----------
@@ -142,12 +133,8 @@ to_ascii_enc(PG_FUNCTION_ARGS)
142133Datum
143134to_ascii_default (PG_FUNCTION_ARGS )
144135{
145- PG_RETURN_TEXT_P
146- (
147- encode_to_ascii
148- (
149- PG_GETARG_TEXT_P_COPY (0 ),
150- GetDatabaseEncoding ()
151- )
152- );
136+ text * data = PG_GETARG_TEXT_P_COPY (0 );
137+ int enc = GetDatabaseEncoding ();
138+
139+ PG_RETURN_TEXT_P (encode_to_ascii (data ,enc ));
153140}