|
| 1 | +import java.util.stream.Stream; |
| 2 | + |
| 3 | +import static java.lang.String.format; |
| 4 | + |
| 5 | +/** |
| 6 | + * {filename} for Unicode {unicode_version} |
| 7 | + * See <a href="https://github.com/ridiculousfish/widecharwidth/">https://github.com/ridiculousfish/widecharwidth/</a> |
| 8 | + * |
| 9 | + * <p>SHA1 file hashes: |
| 10 | + * ( |
| 11 | + * the hashes for generate.py and the template are git object hashes, |
| 12 | + * use `git log --all --find-object=hash` in the widecharwidth repository |
| 13 | + * to see which commit they correspond to, |
| 14 | + * or run `git hash-object` on the file to compare. |
| 15 | + * The other hashes are simple `sha1sum` style hashes. |
| 16 | + * ) |
| 17 | + * |
| 18 | + * <ul> |
| 19 | + * <li>generate.py: {generate_hash}</li> |
| 20 | + * <li>template.java: {template_hash}</li> |
| 21 | + * <li>UnicodeData.txt: {unicode_hash}</li> |
| 22 | + * <li>EastAsianWidth.txt: {eaw_hash}</li> |
| 23 | + * <li>emoji-data.txt: {emoji_hash}</li> |
| 24 | + * </ul> |
| 25 | + */ |
| 26 | +public class WcWidth {{ |
| 27 | + |
| 28 | + private WcWidth() {{ |
| 29 | + }} |
| 30 | + |
| 31 | + public enum Type {{ |
| 32 | + ONE(1, ASCII_TABLE), // The character is single-width |
| 33 | + PRIVATE_USE(1, PRIVATE_TABLE), // The character is for private use. |
| 34 | + NON_PRINT(0, NON_PRINT_TABLE), // The character is not printable |
| 35 | + NON_CHARACTER(0, NON_CHARACTER_TABLE), // The character is a non-character. |
| 36 | + COMBINING(0, COMBINING_TABLE, COMBINING_LETTERS_TABLE), // The character is a zero-width combiner |
| 37 | + TWO(2, DOUBLE_WIDE_TABLE), // The character is double-width |
| 38 | + AMBIGUOUS(1, AMBIGUOUS_TABLE), // The character is East-Asian ambiguous width. |
| 39 | + UNASSIGNED(0, UNASSIGNED_TABLE), // The character is unassigned. |
| 40 | + WIDENED_IN_9(2, WIDENED_IN_9_TABLE); // Width is 1 in Unicode 8, 2 in Unicode 9+. |
| 41 | + |
| 42 | + private final int defaultWidth; |
| 43 | + private final int[][][] tables; |
| 44 | + |
| 45 | + Type(int defaultWidth, int[][]... tables) {{ |
| 46 | + this.defaultWidth = defaultWidth; |
| 47 | + this.tables = tables; |
| 48 | + }} |
| 49 | + |
| 50 | + public int defaultWidth() {{ |
| 51 | + return defaultWidth; |
| 52 | + }} |
| 53 | + |
| 54 | + private boolean contains(int c) {{ |
| 55 | + return Stream.of(tables).anyMatch(table -> contains(c, table)); |
| 56 | + }} |
| 57 | + |
| 58 | + private boolean contains(int c, int[][] table) {{ |
| 59 | + var min = 0; |
| 60 | + var max = table.length - 1; |
| 61 | + |
| 62 | + if (c < table[0][0] || c > table[max][1]) {{ |
| 63 | + return false; |
| 64 | + }} |
| 65 | + |
| 66 | + while (max >= min) {{ |
| 67 | + var mid = (min + max) / 2; |
| 68 | + |
| 69 | + if (c > table[mid][1]) {{ |
| 70 | + min = mid + 1; |
| 71 | + }} else if (c < table[mid][0]) {{ |
| 72 | + max = mid - 1; |
| 73 | + }} else {{ |
| 74 | + return true; |
| 75 | + }} |
| 76 | + }} |
| 77 | + |
| 78 | + return false; |
| 79 | + }} |
| 80 | + |
| 81 | + public static Type of(int c) {{ |
| 82 | + if (c < 0 || c > 0x10FFFF) {{ |
| 83 | + throw new IllegalArgumentException(format("'0x%X' is not a Unicode code point", c)) |
| 84 | + }} |
| 85 | + |
| 86 | + return Stream.of(Type.values()) |
| 87 | + .filter(type -> type.contains(c)) |
| 88 | + .findFirst() |
| 89 | + .orElse(ONE); |
| 90 | + }} |
| 91 | + |
| 92 | + }} |
| 93 | + |
| 94 | + // Simple ASCII characters - used a lot, so we check them first. |
| 95 | + private static final int[][] ASCII_TABLE = {{ |
| 96 | + {ascii} |
| 97 | + }}; |
| 98 | + |
| 99 | + // Private usage range. |
| 100 | + private static final int[][] PRIVATE_TABLE = {{ |
| 101 | + {private} |
| 102 | + }}; |
| 103 | + |
| 104 | + // Nonprinting characters. |
| 105 | + private static final int[][] NON_PRINT_TABLE = {{ |
| 106 | + {nonprint} |
| 107 | + }}; |
| 108 | + |
| 109 | + // Width 0 combining marks. |
| 110 | + private static final int[][] COMBINING_TABLE = {{ |
| 111 | + {combining} |
| 112 | + }}; |
| 113 | + |
| 114 | + // Width 0 combining letters. |
| 115 | + private static final int[][] COMBINING_LETTERS_TABLE = {{ |
| 116 | + {combiningletters} |
| 117 | + }}; |
| 118 | + |
| 119 | + // Width 2 characters. |
| 120 | + private static final int[][] DOUBLE_WIDE_TABLE = {{ |
| 121 | + {doublewide} |
| 122 | + }}; |
| 123 | + |
| 124 | + // Ambiguous-width characters. |
| 125 | + private static final int[][] AMBIGUOUS_TABLE = {{ |
| 126 | + {ambiguous} |
| 127 | + }}; |
| 128 | + |
| 129 | + // Unassigned characters. |
| 130 | + private static final int[][] UNASSIGNED_TABLE = {{ |
| 131 | + {unassigned} |
| 132 | + }}; |
| 133 | + |
| 134 | + // Non-characters. |
| 135 | + private static final int[][] NON_CHARACTER_TABLE = {{ |
| 136 | + {noncharacters} |
| 137 | + }}; |
| 138 | + |
| 139 | + // Characters that were widened from width 1 to 2 in Unicode 9. |
| 140 | + private static final int[][] WIDENED_IN_9_TABLE = {{ |
| 141 | + {widenedin9} |
| 142 | + }}; |
| 143 | + |
| 144 | +}} |
0 commit comments