Skip to content

Commit cd0ea64

Browse files
committed
mbfl_strwidth does not need to use legacy conversion filters now
...Because we have the new (faster) conversion filters now for ALL text encodings supported by mbstring.
1 parent cbb6047 commit cd0ea64

File tree

1 file changed

+12
-33
lines changed

1 file changed

+12
-33
lines changed

ext/mbstring/libmbfl/mbfl/mbfilter.c

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,47 +1216,26 @@ static size_t character_width(unsigned int c)
12161216
return 1;
12171217
}
12181218

1219-
static int filter_count_width(int c, void* data)
1220-
{
1221-
(*(size_t *)data) += character_width(c);
1222-
return 0;
1223-
}
1224-
12251219
size_t mbfl_strwidth(mbfl_string *string)
12261220
{
12271221
if (!string->len) {
12281222
return 0;
12291223
}
12301224

12311225
size_t width = 0;
1232-
1233-
if (string->encoding->to_wchar) {
1234-
uint32_t wchar_buf[128];
1235-
unsigned char *in = string->val;
1236-
size_t in_len = string->len;
1237-
unsigned int state = 0;
1238-
1239-
while (in_len) {
1240-
size_t out_len = string->encoding->to_wchar(&in, &in_len, wchar_buf, 128, &state);
1241-
while (out_len) {
1242-
/* NOTE: 'bad input' marker will be counted as 1 unit of width
1243-
* If text conversion is performed with an ordinary ASCII character as
1244-
* the 'replacement character', this will give us the correct display width. */
1245-
width += character_width(wchar_buf[--out_len]);
1246-
}
1226+
uint32_t wchar_buf[128];
1227+
unsigned char *in = string->val;
1228+
size_t in_len = string->len;
1229+
unsigned int state = 0;
1230+
1231+
while (in_len) {
1232+
size_t out_len = string->encoding->to_wchar(&in, &in_len, wchar_buf, 128, &state);
1233+
while (out_len) {
1234+
/* NOTE: 'bad input' marker will be counted as 1 unit of width
1235+
* If text conversion is performed with an ordinary ASCII character as
1236+
* the 'replacement character', this will give us the correct display width. */
1237+
width += character_width(wchar_buf[--out_len]);
12471238
}
1248-
} else {
1249-
mbfl_convert_filter *filter = mbfl_convert_filter_new(string->encoding, &mbfl_encoding_wchar, filter_count_width, 0, &width);
1250-
ZEND_ASSERT(filter);
1251-
1252-
/* feed data */
1253-
unsigned char *p = string->val, *e = p + string->len;
1254-
while (p < e) {
1255-
(*filter->filter_function)(*p++, filter);
1256-
}
1257-
1258-
mbfl_convert_filter_flush(filter);
1259-
mbfl_convert_filter_delete(filter);
12601239
}
12611240

12621241
return width;

0 commit comments

Comments
 (0)