1
- /* -----------------------------------------------------------------------
1
+ /*-----------------------------------------------------------------------
2
2
* ascii.c
3
+ * The PostgreSQL routine for string to ascii conversion.
3
4
*
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
8
6
*
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 $
10
9
*
11
- * The PostgreSQL routine for string to ascii conversion.
12
- *
13
- * -----------------------------------------------------------------------
10
+ *-----------------------------------------------------------------------
14
11
*/
15
-
16
12
#include "postgres.h"
13
+
17
14
#include "utils/builtins.h"
18
15
#include "mb/pg_wchar.h"
19
16
#include "utils/ascii.h"
20
17
18
+ static void pg_to_ascii (unsigned char * src , unsigned char * src_end ,
19
+ unsigned char * dest , int enc );
21
20
static text * encode_to_ascii (text * data , int enc );
22
21
22
+
23
23
/* ----------
24
24
* to_ascii
25
25
* ----------
26
26
*/
27
- char *
28
- pg_to_ascii (unsigned char * src , unsigned char * src_end , unsigned char * desc , int enc )
27
+ static void
28
+ pg_to_ascii (unsigned char * src , unsigned char * src_end , unsigned char * dest , int enc )
29
29
{
30
30
unsigned char * x ;
31
31
unsigned char * ascii ;
@@ -37,7 +37,6 @@ pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *desc, int
37
37
#define RANGE_128 128
38
38
#define RANGE_160 160
39
39
40
-
41
40
if (enc == PG_LATIN1 )
42
41
{
43
42
/*
@@ -64,9 +63,9 @@ pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *desc, int
64
63
}
65
64
else
66
65
{
67
- elog (ERROR , "pg_to_ascii(): unsupported encoding from %s" ,
66
+ elog (ERROR , "unsupported encoding conversion from %s to ASCII " ,
68
67
pg_encoding_to_char (enc ));
69
- return NULL ; /* keep compiler quiet */
68
+ return ; /* keep compiler quiet */
70
69
}
71
70
72
71
/*
@@ -75,27 +74,27 @@ pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *desc, int
75
74
for (x = src ; x < src_end ; x ++ )
76
75
{
77
76
if (* x < 128 )
78
- * desc ++ = * x ;
77
+ * dest ++ = * x ;
79
78
else if (* x < range )
80
- * desc ++ = ' ' ; /* bogus 128 to 'range' */
79
+ * dest ++ = ' ' ; /* bogus 128 to 'range' */
81
80
else
82
- * desc ++ = ascii [* x - range ];
81
+ * dest ++ = ascii [* x - range ];
83
82
}
84
-
85
- return desc ;
86
83
}
87
84
88
85
/* ----------
89
86
* encode text
87
+ *
88
+ * The text datum is overwritten in-place, therefore this coding method
89
+ * cannot support conversions that change the string length!
90
90
* ----------
91
91
*/
92
92
static text *
93
93
encode_to_ascii (text * data , int enc )
94
94
{
95
- pg_to_ascii (
96
- (unsigned char * ) VARDATA (data ), /* src */
97
- VARDATA (data ) + VARSIZE (data ), /* src end */
98
- (unsigned char * ) VARDATA (data ), /* desc */
95
+ pg_to_ascii ((unsigned char * ) VARDATA (data ), /* src */
96
+ (unsigned char * ) (data ) + VARSIZE (data ), /* src end */
97
+ (unsigned char * ) VARDATA (data ), /* dest */
99
98
enc ); /* encoding */
100
99
101
100
return data ;
@@ -108,14 +107,10 @@ encode_to_ascii(text *data, int enc)
108
107
Datum
109
108
to_ascii_encname (PG_FUNCTION_ARGS )
110
109
{
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 ));
119
114
}
120
115
121
116
/* ----------
@@ -125,14 +120,10 @@ to_ascii_encname(PG_FUNCTION_ARGS)
125
120
Datum
126
121
to_ascii_enc (PG_FUNCTION_ARGS )
127
122
{
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 ));
136
127
}
137
128
138
129
/* ----------
@@ -142,12 +133,8 @@ to_ascii_enc(PG_FUNCTION_ARGS)
142
133
Datum
143
134
to_ascii_default (PG_FUNCTION_ARGS )
144
135
{
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 ));
153
140
}
0 commit comments