diff options
Diffstat (limited to 'src/common/wchar.c')
-rw-r--r-- | src/common/wchar.c | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/src/common/wchar.c b/src/common/wchar.c index bb97b5f54f4..8242bcd3873 100644 --- a/src/common/wchar.c +++ b/src/common/wchar.c @@ -585,17 +585,18 @@ struct mbinterval { unsigned short first; unsigned short last; + signed int width; }; /* auxiliary function for binary search in interval table */ -static int +static const struct mbinterval * mbbisearch(pg_wchar ucs, const struct mbinterval *table, int max) { int min = 0; int mid; if (ucs < table[0].first || ucs > table[max].last) - return 0; + return NULL; while (max >= min) { mid = (min + max) / 2; @@ -604,10 +605,10 @@ mbbisearch(pg_wchar ucs, const struct mbinterval *table, int max) else if (ucs < table[mid].first) max = mid - 1; else - return 1; + return &table[mid]; } - return 0; + return NULL; } @@ -646,6 +647,8 @@ ucs_wcwidth(pg_wchar ucs) { #include "common/unicode_width_table.h" + const struct mbinterval *range; + /* test for 8-bit control characters */ if (ucs == 0) return 0; @@ -653,10 +656,12 @@ ucs_wcwidth(pg_wchar ucs) if (ucs < 0x20 || (ucs >= 0x7f && ucs < 0xa0) || ucs > 0x0010ffff) return -1; - /* binary search in table of non-spacing characters */ - if (mbbisearch(ucs, combining, - sizeof(combining) / sizeof(struct mbinterval) - 1)) - return 0; + /* binary search in table of character widths */ + range = mbbisearch(ucs, wcwidth, + sizeof(wcwidth) / sizeof(struct mbinterval) - 1); + + if (range != NULL) + return range->width; /* * if we arrive here, ucs is not a combining or C0/C1 control character |