Skip to content

Commit 7845bfc

Browse files
committed
Dept of second thoughts: don't use the new wide-character upper/lower
code if we are running in a single-byte encoding. No point in the extra overhead in that case.
1 parent b8312c5 commit 7845bfc

File tree

1 file changed

+99
-93
lines changed

1 file changed

+99
-93
lines changed

src/backend/utils/adt/oracle_compat.c

Lines changed: 99 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/utils/adt/oracle_compat.c,v 1.52 2004/05/26 16:16:03 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/utils/adt/oracle_compat.c,v 1.53 2004/06/06 22:17:01 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -166,40 +166,44 @@ Datum
166166
lower(PG_FUNCTION_ARGS)
167167
{
168168
#ifdef USE_WIDE_UPPER_LOWER
169-
text *string = PG_GETARG_TEXT_P(0);
170-
text *result;
171-
wchar_t *workspace;
172-
int i;
173-
174-
workspace = texttowcs(string);
169+
/* use wide char code only when max encoding length > one */
170+
if (pg_database_encoding_max_length() > 1)
171+
{
172+
text *string = PG_GETARG_TEXT_P(0);
173+
text *result;
174+
wchar_t *workspace;
175+
int i;
175176

176-
for (i = 0; workspace[i] != 0; i++)
177-
workspace[i] = towlower(workspace[i]);
177+
workspace = texttowcs(string);
178178

179-
result = wcstotext(workspace, i);
179+
for (i = 0; workspace[i] != 0; i++)
180+
workspace[i] = towlower(workspace[i]);
180181

181-
pfree(workspace);
182+
result = wcstotext(workspace, i);
182183

183-
PG_RETURN_TEXT_P(result);
184+
pfree(workspace);
184185

185-
#else /* !USE_WIDE_UPPER_LOWER */
186+
PG_RETURN_TEXT_P(result);
187+
}
188+
else
189+
#endif /* USE_WIDE_UPPER_LOWER */
190+
{
191+
text *string = PG_GETARG_TEXT_P_COPY(0);
192+
char *ptr;
193+
int m;
186194

187-
text *string = PG_GETARG_TEXT_P_COPY(0);
188-
char *ptr;
189-
int m;
195+
/* Since we copied the string, we can scribble directly on the value */
196+
ptr = VARDATA(string);
197+
m = VARSIZE(string) - VARHDRSZ;
190198

191-
/* Since we copied the string, we can scribble directly on the value */
192-
ptr = VARDATA(string);
193-
m = VARSIZE(string) - VARHDRSZ;
199+
while (m-- > 0)
200+
{
201+
*ptr = tolower((unsigned char) *ptr);
202+
ptr++;
203+
}
194204

195-
while (m-- > 0)
196-
{
197-
*ptr = tolower((unsigned char) *ptr);
198-
ptr++;
205+
PG_RETURN_TEXT_P(string);
199206
}
200-
201-
PG_RETURN_TEXT_P(string);
202-
#endif /* USE_WIDE_UPPER_LOWER */
203207
}
204208

205209

@@ -221,40 +225,44 @@ Datum
221225
upper(PG_FUNCTION_ARGS)
222226
{
223227
#ifdef USE_WIDE_UPPER_LOWER
224-
text *string = PG_GETARG_TEXT_P(0);
225-
text *result;
226-
wchar_t *workspace;
227-
int i;
228-
229-
workspace = texttowcs(string);
228+
/* use wide char code only when max encoding length > one */
229+
if (pg_database_encoding_max_length() > 1)
230+
{
231+
text *string = PG_GETARG_TEXT_P(0);
232+
text *result;
233+
wchar_t *workspace;
234+
int i;
230235

231-
for (i = 0; workspace[i] != 0; i++)
232-
workspace[i] = towupper(workspace[i]);
236+
workspace = texttowcs(string);
233237

234-
result = wcstotext(workspace, i);
238+
for (i = 0; workspace[i] != 0; i++)
239+
workspace[i] = towupper(workspace[i]);
235240

236-
pfree(workspace);
241+
result = wcstotext(workspace, i);
237242

238-
PG_RETURN_TEXT_P(result);
243+
pfree(workspace);
239244

240-
#else /* !USE_WIDE_UPPER_LOWER */
245+
PG_RETURN_TEXT_P(result);
246+
}
247+
else
248+
#endif /* USE_WIDE_UPPER_LOWER */
249+
{
250+
text *string = PG_GETARG_TEXT_P_COPY(0);
251+
char *ptr;
252+
int m;
241253

242-
text *string = PG_GETARG_TEXT_P_COPY(0);
243-
char *ptr;
244-
int m;
254+
/* Since we copied the string, we can scribble directly on the value */
255+
ptr = VARDATA(string);
256+
m = VARSIZE(string) - VARHDRSZ;
245257

246-
/* Since we copied the string, we can scribble directly on the value */
247-
ptr = VARDATA(string);
248-
m = VARSIZE(string) - VARHDRSZ;
258+
while (m-- > 0)
259+
{
260+
*ptr = toupper((unsigned char) *ptr);
261+
ptr++;
262+
}
249263

250-
while (m-- > 0)
251-
{
252-
*ptr = toupper((unsigned char) *ptr);
253-
ptr++;
264+
PG_RETURN_TEXT_P(string);
254265
}
255-
256-
PG_RETURN_TEXT_P(string);
257-
#endif /* USE_WIDE_UPPER_LOWER */
258266
}
259267

260268

@@ -279,58 +287,56 @@ Datum
279287
initcap(PG_FUNCTION_ARGS)
280288
{
281289
#ifdef USE_WIDE_UPPER_LOWER
282-
text *string = PG_GETARG_TEXT_P(0);
283-
text *result;
284-
wchar_t *workspace;
285-
int wasalnum = 0;
286-
int i;
287-
288-
workspace = texttowcs(string);
289-
290-
for (i = 0; workspace[i] != 0; i++)
290+
/* use wide char code only when max encoding length > one */
291+
if (pg_database_encoding_max_length() > 1)
291292
{
292-
if (wasalnum)
293-
workspace[i] = towlower(workspace[i]);
294-
else
295-
workspace[i] = towupper(workspace[i]);
296-
wasalnum = iswalnum(workspace[i]);
297-
}
293+
text *string = PG_GETARG_TEXT_P(0);
294+
text *result;
295+
wchar_t *workspace;
296+
int wasalnum = 0;
297+
int i;
298298

299-
result = wcstotext(workspace, i);
299+
workspace = texttowcs(string);
300300

301-
pfree(workspace);
301+
for (i = 0; workspace[i] != 0; i++)
302+
{
303+
if (wasalnum)
304+
workspace[i] = towlower(workspace[i]);
305+
else
306+
workspace[i] = towupper(workspace[i]);
307+
wasalnum = iswalnum(workspace[i]);
308+
}
302309

303-
PG_RETURN_TEXT_P(result);
310+
result = wcstotext(workspace, i);
304311

305-
#else /* !USE_WIDE_UPPER_LOWER */
312+
pfree(workspace);
306313

307-
text *string = PG_GETARG_TEXT_P_COPY(0);
308-
char *ptr;
309-
int m;
314+
PG_RETURN_TEXT_P(result);
315+
}
316+
else
317+
#endif /* USE_WIDE_UPPER_LOWER */
318+
{
319+
text *string = PG_GETARG_TEXT_P_COPY(0);
320+
int wasalnum = 0;
321+
char *ptr;
322+
int m;
310323

311-
/* Since we copied the string, we can scribble directly on the value */
312-
ptr = VARDATA(string);
313-
m = VARSIZE(string) - VARHDRSZ;
324+
/* Since we copied the string, we can scribble directly on the value */
325+
ptr = VARDATA(string);
326+
m = VARSIZE(string) - VARHDRSZ;
314327

315-
if (m > 0)
316-
{
317-
*ptr = toupper((unsigned char) *ptr);
318-
ptr++;
319-
m--;
320-
}
328+
while (m-- > 0)
329+
{
330+
if (wasalnum)
331+
*ptr = tolower((unsigned char) *ptr);
332+
else
333+
*ptr = toupper((unsigned char) *ptr);
334+
wasalnum = isalnum((unsigned char) *ptr);
335+
ptr++;
336+
}
321337

322-
while (m-- > 0)
323-
{
324-
/* Oracle capitalizes after all non-alphanumeric */
325-
if (!isalnum((unsigned char) ptr[-1]))
326-
*ptr = toupper((unsigned char) *ptr);
327-
else
328-
*ptr = tolower((unsigned char) *ptr);
329-
ptr++;
338+
PG_RETURN_TEXT_P(string);
330339
}
331-
332-
PG_RETURN_TEXT_P(string);
333-
#endif /* USE_WIDE_UPPER_LOWER */
334340
}
335341

336342

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy