Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbb2bf2d

Browse files
committed
Make lpad/rpad/translate multibyte aware. Also add Copright notice etc.
1 parentbe629ab commitbb2bf2d

File tree

1 file changed

+142
-5
lines changed

1 file changed

+142
-5
lines changed

‎src/backend/utils/adt/oracle_compat.c

Lines changed: 142 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
/*
2-
*Edmund Mergl <E.Mergl@bawue.de>
1+
/*-------------------------------------------------------------------------
2+
* oracle_compat.c
3+
*Oracle compatible functions.
34
*
4-
*$Header: /cvsroot/pgsql/src/backend/utils/adt/oracle_compat.c,v 1.342001/09/22 03:30:39 ishii Exp $
5+
* Copyright (c) 1997-2001, PostgreSQL Global Development Group
56
*
7+
*Author: Edmund Mergl <E.Mergl@bawue.de>
8+
*Multibyte enhancement: Tatsuo Ishii <ishii@postgresql.org>
9+
*
10+
*
11+
* IDENTIFICATION
12+
*$Header: /cvsroot/pgsql/src/backend/utils/adt/oracle_compat.c,v 1.35 2001/09/23 11:02:01 ishii Exp $
13+
*
14+
*-------------------------------------------------------------------------
615
*/
716

817
#include"postgres.h"
@@ -162,6 +171,9 @@ lpad(PG_FUNCTION_ARGS)
162171
intm,
163172
s1len,
164173
s2len;
174+
#ifdefMULTIBYTE
175+
intbytelen;
176+
#endif
165177

166178
/* Negative len is silently taken as zero */
167179
if (len<0)
@@ -175,32 +187,62 @@ lpad(PG_FUNCTION_ARGS)
175187
if (s2len<0)
176188
s2len=0;/* shouldn't happen */
177189

190+
#ifdefMULTIBYTE
191+
s1len=pg_mbstrlen_with_len(VARDATA(string1),s1len);
192+
#endif
178193
if (s1len>len)
179194
s1len=len;/* truncate string1 to len chars */
180195

181196
if (s2len <=0)
182197
len=s1len;/* nothing to pad with, so don't pad */
183198

199+
#ifdefMULTIBYTE
200+
bytelen=pg_database_encoding_max_length()*len;
201+
ret= (text*)palloc(VARHDRSZ+bytelen);
202+
VARATT_SIZEP(ret)=VARHDRSZ+bytelen;
203+
#else
184204
ret= (text*)palloc(VARHDRSZ+len);
185205
VARATT_SIZEP(ret)=VARHDRSZ+len;
186-
206+
#endif
187207
m=len-s1len;
188208

189209
ptr2=VARDATA(string2);
190210
ptr2end=ptr2+s2len;
191211
ptr_ret=VARDATA(ret);
192212

213+
#ifdefMULTIBYTE
214+
while (m--)
215+
{
216+
intmlen=pg_mblen(ptr2);
217+
memcpy(ptr_ret,ptr2,mlen);
218+
ptr_ret+=mlen;
219+
ptr2+=mlen;
220+
if (ptr2==ptr2end)/* wrap around at end of s2 */
221+
ptr2=VARDATA(string2);
222+
}
223+
#else
193224
while (m--)
194225
{
195226
*ptr_ret++=*ptr2++;
196227
if (ptr2==ptr2end)/* wrap around at end of s2 */
197228
ptr2=VARDATA(string2);
198229
}
230+
#endif
199231

200232
ptr1=VARDATA(string1);
201233

234+
#ifdefMULTIBYTE
235+
while (s1len--)
236+
{
237+
intmlen=pg_mblen(ptr1);
238+
memcpy(ptr_ret,ptr1,mlen);
239+
ptr_ret+=mlen;
240+
ptr1+=mlen;
241+
}
242+
#else
202243
while (s1len--)
203244
*ptr_ret++=*ptr1++;
245+
#endif
204246

205247
PG_RETURN_TEXT_P(ret);
206248
}
@@ -236,6 +278,9 @@ rpad(PG_FUNCTION_ARGS)
236278
intm,
237279
s1len,
238280
s2len;
281+
#ifdefMULTIBYTE
282+
intbytelen;
283+
#endif
239284

240285
/* Negative len is silently taken as zero */
241286
if (len<0)
@@ -249,32 +294,63 @@ rpad(PG_FUNCTION_ARGS)
249294
if (s2len<0)
250295
s2len=0;/* shouldn't happen */
251296

297+
#ifdefMULTIBYTE
298+
s1len=pg_mbstrlen_with_len(VARDATA(string1),s1len);
299+
#endif
300+
252301
if (s1len>len)
253302
s1len=len;/* truncate string1 to len chars */
254303

255304
if (s2len <=0)
256305
len=s1len;/* nothing to pad with, so don't pad */
257306

307+
#ifdefMULTIBYTE
308+
bytelen=pg_database_encoding_max_length()*len;
309+
ret= (text*)palloc(VARHDRSZ+bytelen);
310+
VARATT_SIZEP(ret)=VARHDRSZ+bytelen;
311+
#else
258312
ret= (text*)palloc(VARHDRSZ+len);
259313
VARATT_SIZEP(ret)=VARHDRSZ+len;
260-
314+
#endif
261315
m=len-s1len;
262316

263317
ptr1=VARDATA(string1);
264318
ptr_ret=VARDATA(ret);
265319

320+
#ifdefMULTIBYTE
321+
while (s1len--)
322+
{
323+
intmlen=pg_mblen(ptr1);
324+
memcpy(ptr_ret,ptr1,mlen);
325+
ptr_ret+=mlen;
326+
ptr1+=mlen;
327+
}
328+
#else
266329
while (s1len--)
267330
*ptr_ret++=*ptr1++;
331+
#endif
268332

269333
ptr2=VARDATA(string2);
270334
ptr2end=ptr2+s2len;
271335

336+
#ifdefMULTIBYTE
337+
while (m--)
338+
{
339+
intmlen=pg_mblen(ptr2);
340+
memcpy(ptr_ret,ptr2,mlen);
341+
ptr_ret+=mlen;
342+
ptr2+=mlen;
343+
if (ptr2==ptr2end)/* wrap around at end of s2 */
344+
ptr2=VARDATA(string2);
345+
}
346+
#else
272347
while (m--)
273348
{
274349
*ptr_ret++=*ptr2++;
275350
if (ptr2==ptr2end)/* wrap around at end of s2 */
276351
ptr2=VARDATA(string2);
277352
}
353+
#endif
278354

279355
PG_RETURN_TEXT_P(ret);
280356
}
@@ -708,6 +784,13 @@ translate(PG_FUNCTION_ARGS)
708784
tolen,
709785
retlen,
710786
i;
787+
#ifdefMULTIBYTE
788+
intstr_len;
789+
intestimate_len;
790+
intlen;
791+
intsource_len;
792+
intfrom_index;
793+
#endif
711794

712795
if ((m=VARSIZE(string)-VARHDRSZ) <=0)
713796
PG_RETURN_TEXT_P(string);
@@ -717,12 +800,65 @@ translate(PG_FUNCTION_ARGS)
717800
tolen=VARSIZE(to)-VARHDRSZ;
718801
to_ptr=VARDATA(to);
719802

803+
#ifdefMULTIBYTE
804+
str_len=VARSIZE(string);
805+
estimate_len= (tolen*1.0/fromlen+0.5)*str_len;
806+
estimate_len=estimate_len>str_len?estimate_len:str_len;
807+
result= (text*)palloc(estimate_len);
808+
#else
720809
result= (text*)palloc(VARSIZE(string));
810+
#endif
721811

722812
source=VARDATA(string);
723813
target=VARDATA(result);
724814
retlen=0;
725815

816+
#ifdefMULTIBYTE
817+
while (m>0)
818+
{
819+
source_len=pg_mblen(source);
820+
from_index=0;
821+
822+
for (i=0;i<fromlen;i+=len)
823+
{
824+
len=pg_mblen(&from_ptr[i]);
825+
if (len==source_len&&
826+
memcmp(source,&from_ptr[i],len)==0)
827+
break;
828+
829+
from_index++;
830+
}
831+
if (i<fromlen)
832+
{
833+
/* substitute */
834+
char*p=to_ptr;
835+
for (i=0;i<from_index;i++)
836+
{
837+
p+=pg_mblen(p);
838+
if (p >= (to_ptr+tolen))
839+
break;
840+
}
841+
if (p< (to_ptr+tolen))
842+
{
843+
len=pg_mblen(p);
844+
memcpy(target,p,len);
845+
target+=len;
846+
retlen+=len;
847+
}
848+
849+
}
850+
else
851+
{
852+
/* no match, so copy */
853+
memcpy(target,source,source_len);
854+
target+=source_len;
855+
retlen+=source_len;
856+
}
857+
858+
source+=source_len;
859+
m-=source_len;
860+
}
861+
#else
726862
while (m-->0)
727863
{
728864
charrep=*source++;
@@ -752,6 +888,7 @@ translate(PG_FUNCTION_ARGS)
752888
retlen++;
753889
}
754890
}
891+
#endif
755892

756893
VARATT_SIZEP(result)=retlen+VARHDRSZ;
757894

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp