diff options
author | John Naylor <john.naylor@postgresql.org> | 2021-08-20 11:10:17 -0400 |
---|---|---|
committer | John Naylor <john.naylor@postgresql.org> | 2021-08-25 13:08:11 -0400 |
commit | 78ab944cd4b9977732becd9d0bc83223b88af9a2 (patch) | |
tree | 465e9d0774be9df806ea03640f5637bbea9eefb3 /src/common/wchar.c | |
parent | eb0d0d2c7300c9c5c22b35975c11265aa4becc84 (diff) |
Change mbbisearch to return the character range
Add a width field to mbinterval and have mbbisearch return a
pointer to the found range rather than just bool for success.
A future commit will add another width besides zero, and this
will allow that to use the same search.
Reviewed by Jacob Champion
Discussion: https://www.postgresql.org/message-id/CAFBsxsGOCpzV7c-f3a8ADsA1n4uZ%3D8puCctQp%2Bx7W0vgkv%3Dw%2Bg%40mail.gmail.com
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 |