4
4
* (currently mule internal code (mic) is used)
5
5
* Tatsuo Ishii
6
6
*
7
- * $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.75 2008/11/11 03:01:20 tgl Exp $
7
+ * $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.76 2009/01/04 18:37:35 tgl Exp $
8
8
*/
9
9
#include "postgres.h"
10
10
@@ -710,14 +710,14 @@ pg_encoding_mb2wchar_with_len(int encoding,
710
710
return (* pg_wchar_table [encoding ].mb2wchar_with_len ) ((const unsigned char * ) from , to , len );
711
711
}
712
712
713
- /* returns the byte length of a multibyte word */
713
+ /* returns the byte length of a multibyte character */
714
714
int
715
715
pg_mblen (const char * mbstr )
716
716
{
717
717
return ((* pg_wchar_table [DatabaseEncoding -> encoding ].mblen ) ((const unsigned char * ) mbstr ));
718
718
}
719
719
720
- /* returns the display length of a multibyte word */
720
+ /* returns the display length of a multibyte character */
721
721
int
722
722
pg_dsplen (const char * mbstr )
723
723
{
@@ -767,23 +767,37 @@ pg_mbstrlen_with_len(const char *mbstr, int limit)
767
767
768
768
/*
769
769
* returns the byte length of a multibyte string
770
- * (not necessarily NULL terminated)
770
+ * (not necessarily NULL terminated)
771
771
* that is no longer than limit.
772
- * this function does not break multibyte word boundary.
772
+ * this function does not break multibyte character boundary.
773
773
*/
774
774
int
775
775
pg_mbcliplen (const char * mbstr , int len , int limit )
776
776
{
777
+ return pg_encoding_mbcliplen (DatabaseEncoding -> encoding , mbstr ,
778
+ len , limit );
779
+ }
780
+
781
+ /*
782
+ * pg_mbcliplen with specified encoding
783
+ */
784
+ int
785
+ pg_encoding_mbcliplen (int encoding , const char * mbstr ,
786
+ int len , int limit )
787
+ {
788
+ mblen_converter mblen_fn ;
777
789
int clen = 0 ;
778
790
int l ;
779
791
780
792
/* optimization for single byte encoding */
781
- if (pg_database_encoding_max_length ( ) == 1 )
793
+ if (pg_encoding_max_length ( encoding ) == 1 )
782
794
return cliplen (mbstr , len , limit );
783
795
796
+ mblen_fn = pg_wchar_table [encoding ].mblen ;
797
+
784
798
while (len > 0 && * mbstr )
785
799
{
786
- l = pg_mblen ( mbstr );
800
+ l = ( * mblen_fn ) (( const unsigned char * ) mbstr );
787
801
if ((clen + l ) > limit )
788
802
break ;
789
803
clen += l ;
@@ -797,7 +811,8 @@ pg_mbcliplen(const char *mbstr, int len, int limit)
797
811
798
812
/*
799
813
* Similar to pg_mbcliplen except the limit parameter specifies the
800
- * character length, not the byte length. */
814
+ * character length, not the byte length.
815
+ */
801
816
int
802
817
pg_mbcharcliplen (const char * mbstr , int len , int limit )
803
818
{
@@ -822,6 +837,18 @@ pg_mbcharcliplen(const char *mbstr, int len, int limit)
822
837
return clen ;
823
838
}
824
839
840
+ /* mbcliplen for any single-byte encoding */
841
+ static int
842
+ cliplen (const char * str , int len , int limit )
843
+ {
844
+ int l = 0 ;
845
+
846
+ len = Min (len , limit );
847
+ while (l < len && str [l ])
848
+ l ++ ;
849
+ return l ;
850
+ }
851
+
825
852
void
826
853
SetDatabaseEncoding (int encoding )
827
854
{
@@ -884,17 +911,3 @@ pg_client_encoding(PG_FUNCTION_ARGS)
884
911
Assert (ClientEncoding );
885
912
return DirectFunctionCall1 (namein , CStringGetDatum (ClientEncoding -> name ));
886
913
}
887
-
888
- static int
889
- cliplen (const char * str , int len , int limit )
890
- {
891
- int l = 0 ;
892
- const char * s ;
893
-
894
- for (s = str ; * s ; s ++ , l ++ )
895
- {
896
- if (l >= len || l >= limit )
897
- return l ;
898
- }
899
- return (s - str );
900
- }
0 commit comments