charclass.c revision 2eaa94a1
1/* $XTermId: charclass.c,v 1.21 2008/12/30 17:35:09 tom Exp $ */ 2 3/* 4 * Compact and efficient reimplementation of the 5 * xterm character class mechanism for large character sets 6 * 7 * Markus Kuhn -- mkuhn@acm.org -- 2000-07-03 8 * 9 * Xterm allows users to select entire words with a double-click on the left 10 * mouse button. Opinions might differ on what type of characters are part of 11 * separate words, therefore xterm allows users to configure a class code for 12 * each 8-bit character. Words are maximum length sequences of neighboring 13 * characters with identical class code. Extending this mechanism to Unicode 14 * naively would create an at least 2^16 entries (128 kB) long class code 15 * table. 16 * 17 * Instead, we transform the character class table into a list of intervals, 18 * that will be accessed via a linear search. Changes made to the table by the 19 * user will be appended. A special class code IDENT (default) marks 20 * characters who have their code number as the class code. 21 * 22 * We could alternatively use a sorted table of non-overlapping intervals that 23 * can be accessed via binary search, but merging in new intervals is 24 * significantly more hassle and not worth the effort here. 25 */ 26 27#include <xterm.h> 28#include <charclass.h> 29 30#if OPT_WIDE_CHARS 31 32static struct classentry { 33 int cclass; 34 int first; 35 int last; 36} *classtab; 37 38/* 39 * Special convention for classtab[0]: 40 * - classtab[0].cclass is the allocated number of entries in classtab 41 * - classtab[0].first = 1 (first used entry in classtab) 42 * - classtab[0].last is the last used entry in classtab 43 */ 44 45int 46SetCharacterClassRange(int low, int high, int value) 47{ 48 if (high < low) 49 return -1; /* nothing to do */ 50 51 /* make sure we have at least one free entry left at table end */ 52 if (classtab[0].last > classtab[0].cclass - 2) { 53 classtab[0].cclass += 5 + classtab[0].cclass / 4; 54 classtab = TypeRealloc(struct classentry, (unsigned) classtab[0].cclass, classtab); 55 if (!classtab) 56 abort(); 57 } 58 59 /* simply append new interval to end of interval array */ 60 classtab[0].last++; 61 classtab[classtab[0].last].first = low; 62 classtab[classtab[0].last].last = high; 63 classtab[classtab[0].last].cclass = value; 64 65 return 0; 66} 67 68typedef enum { 69 IDENT = -1, 70 ALNUM = 48, 71 CNTRL = 1, 72 BLANK = 32 73} Classes; 74 75void 76init_classtab(void) 77{ 78 const int size = 50; 79 80 classtab = TypeMallocN(struct classentry, size); 81 if (!classtab) 82 abort(); 83 classtab[0].cclass = size; 84 classtab[0].first = 1; 85 classtab[0].last = 0; 86 87 /* old xterm default classes */ 88 SetCharacterClassRange(0, 0, BLANK); 89 SetCharacterClassRange(1, 31, CNTRL); 90 SetCharacterClassRange('\t', '\t', BLANK); 91 SetCharacterClassRange('0', '9', ALNUM); 92 SetCharacterClassRange('A', 'Z', ALNUM); 93 SetCharacterClassRange('_', '_', ALNUM); 94 SetCharacterClassRange('a', 'z', ALNUM); 95 SetCharacterClassRange(127, 159, CNTRL); 96 SetCharacterClassRange(160, 191, IDENT); 97 SetCharacterClassRange(192, 255, ALNUM); 98 SetCharacterClassRange(215, 215, IDENT); 99 SetCharacterClassRange(247, 247, IDENT); 100 101 /* added Unicode classes */ 102 SetCharacterClassRange(0x0100, 0xffdf, ALNUM); /* mostly characters */ 103 SetCharacterClassRange(0x037e, 0x037e, IDENT); /* Greek question mark */ 104 SetCharacterClassRange(0x0387, 0x0387, IDENT); /* Greek ano teleia */ 105 SetCharacterClassRange(0x055a, 0x055f, IDENT); /* Armenian punctuation */ 106 SetCharacterClassRange(0x0589, 0x0589, IDENT); /* Armenian full stop */ 107 SetCharacterClassRange(0x0700, 0x070d, IDENT); /* Syriac punctuation */ 108 SetCharacterClassRange(0x104a, 0x104f, IDENT); /* Myanmar punctuation */ 109 SetCharacterClassRange(0x10fb, 0x10fb, IDENT); /* Georgian punctuation */ 110 SetCharacterClassRange(0x1361, 0x1368, IDENT); /* Ethiopic punctuation */ 111 SetCharacterClassRange(0x166d, 0x166e, IDENT); /* Canadian Syl. punctuation */ 112 SetCharacterClassRange(0x17d4, 0x17dc, IDENT); /* Khmer punctuation */ 113 SetCharacterClassRange(0x1800, 0x180a, IDENT); /* Mongolian punctuation */ 114 SetCharacterClassRange(0x2000, 0x200a, BLANK); /* spaces */ 115 SetCharacterClassRange(0x200b, 0x27ff, IDENT); /* punctuation and symbols */ 116 SetCharacterClassRange(0x2070, 0x207f, 0x2070); /* superscript */ 117 SetCharacterClassRange(0x2080, 0x208f, 0x2080); /* subscript */ 118 SetCharacterClassRange(0x3000, 0x3000, BLANK); /* ideographic space */ 119 SetCharacterClassRange(0x3001, 0x3020, IDENT); /* ideographic punctuation */ 120 SetCharacterClassRange(0x3040, 0x309f, 0x3040); /* Hiragana */ 121 SetCharacterClassRange(0x30a0, 0x30ff, 0x30a0); /* Katakana */ 122 SetCharacterClassRange(0x3300, 0x9fff, 0x4e00); /* CJK Ideographs */ 123 SetCharacterClassRange(0xac00, 0xd7a3, 0xac00); /* Hangul Syllables */ 124 SetCharacterClassRange(0xf900, 0xfaff, 0x4e00); /* CJK Ideographs */ 125 SetCharacterClassRange(0xfe30, 0xfe6b, IDENT); /* punctuation forms */ 126 SetCharacterClassRange(0xff00, 0xff0f, IDENT); /* half/fullwidth ASCII */ 127 SetCharacterClassRange(0xff1a, 0xff20, IDENT); /* half/fullwidth ASCII */ 128 SetCharacterClassRange(0xff3b, 0xff40, IDENT); /* half/fullwidth ASCII */ 129 SetCharacterClassRange(0xff5b, 0xff64, IDENT); /* half/fullwidth ASCII */ 130 131 return; 132} 133 134int 135CharacterClass(int c) 136{ 137 int i, cclass = IDENT; 138 139 for (i = classtab[0].first; i <= classtab[0].last; i++) 140 if (classtab[i].first <= c && classtab[i].last >= c) 141 cclass = classtab[i].cclass; 142 143 if (cclass < 0) 144 cclass = c; 145 146 return cclass; 147} 148 149#ifdef NO_LEAKS 150void 151noleaks_CharacterClass(void) 152{ 153 if (classtab != 0) { 154 free(classtab); 155 classtab = 0; 156 } 157} 158#endif 159 160#endif /* OPT_WIDE_CHARS */ 161