| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include "uninames_data.h" |
| 29 |
#include <assert.h> |
| 30 |
|
| 31 |
static char* read_lexicon(char* dst, unsigned int idx, int* bufsiz) { |
| 32 |
idx = lexicon_offset[idx] + lexicon_shift[idx >> LEXICON_SHIFT]; |
| 33 |
do { |
| 34 |
*dst++ = lexicon_data[idx] & 0x7f; |
| 35 |
} while (--*bufsiz > 1 && !(lexicon_data[idx++] & 0x80)); |
| 36 |
assert(lexicon_data[idx-1] & 0x80); |
| 37 |
return dst; |
| 38 |
} |
| 39 |
|
| 40 |
static const unsigned char* get_phrasebook_entry(unichar_t ch) { |
| 41 |
int index = 0, shift_index; |
| 42 |
if (ch < UNICODE_MAX) { |
| 43 |
index = phrasebook_index1[ch >> PHRASEBOOK_SHIFT1]; |
| 44 |
index = phrasebook_index2[(index << PHRASEBOOK_SHIFT1) + (ch & ((1 << PHRASEBOOK_SHIFT1) - 1))]; |
| 45 |
} |
| 46 |
if (index == 0) { |
| 47 |
return NULL; |
| 48 |
} |
| 49 |
shift_index = ch >> PHRASEBOOK_SHIFT2; |
| 50 |
if (shift_index > PHRASEBOOK_SHIFT2_CAP) { |
| 51 |
shift_index = PHRASEBOOK_SHIFT2_CAP; |
| 52 |
} |
| 53 |
index += phrasebook_shift[shift_index]; |
| 54 |
assert(index >= 0 && (size_t)index < sizeof(phrasebook_data)/sizeof(phrasebook_data[0])); |
| 55 |
return &phrasebook_data[index]; |
| 56 |
} |
| 57 |
|
| 58 |
static char* prettify_annotation_start(char* dst, int src, int* bufsiz) { |
| 59 |
switch(src) { |
| 60 |
case '#': src = 0x2248; *bufsiz -= 3; break; |
| 61 |
case '%': src = 0x203b; *bufsiz -= 3; break; |
| 62 |
case '*': src = 0x2022; *bufsiz -= 3; break; |
| 63 |
case ':': src = 0x2261; *bufsiz -= 3; break; |
| 64 |
case 'x': src = 0x2192; *bufsiz -= 3; break; |
| 65 |
case '~': src = 0x2053; *bufsiz -= 3; break; |
| 66 |
default: |
| 67 |
assert(false && "unknown annotation start char"); |
| 68 |
|
| 69 |
case '=': *bufsiz -= 1; |
| 70 |
} |
| 71 |
return utf8_idpb(dst, src, 0); |
| 72 |
} |
| 73 |
|
| 74 |
char* uniname_name(unichar_t ch) { |
| 75 |
const unsigned char *src; |
| 76 |
static char ret[MAX_NAME_LENGTH]; |
| 77 |
int bufsiz = sizeof(ret)/sizeof(ret[0]); |
| 78 |
char* dst = ret; |
| 79 |
|
| 80 |
src = get_phrasebook_entry(ch); |
| 81 |
if (src == NULL || *src == '\n' || *src == '\0') { |
| 82 |
return get_derived_name(ch); |
| 83 |
} |
| 84 |
|
| 85 |
while (*src != '\n' && *src != '\0' && bufsiz > 4) { |
| 86 |
switch (*src >> 4) { |
| 87 |
case 8: case 9: case 10: case 11: |
| 88 |
dst = read_lexicon(dst, ((src[0] & 0x3f) << 7) | (src[1] & 0x7f), &bufsiz); |
| 89 |
src += 2; |
| 90 |
break; |
| 91 |
case 15: |
| 92 |
*dst++ = *src++; |
| 93 |
--bufsiz; |
| 94 |
|
| 95 |
case 14: |
| 96 |
*dst++ = *src++; |
| 97 |
--bufsiz; |
| 98 |
|
| 99 |
case 12: case 13: |
| 100 |
*dst++ = *src++; |
| 101 |
--bufsiz; |
| 102 |
|
| 103 |
default: |
| 104 |
*dst++ = *src++; |
| 105 |
--bufsiz; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
assert(bufsiz > 4); |
| 110 |
if (bufsiz < 0) { |
| 111 |
bufsiz = 0; |
| 112 |
} |
| 113 |
return copyn(ret, (sizeof(ret)/sizeof(ret[0])) - bufsiz); |
| 114 |
} |
| 115 |
|
| 116 |
char* uniname_annotation(unichar_t ch, int prettify) { |
| 117 |
const unsigned char *src; |
| 118 |
char ret[MAX_ANNOTATION_LENGTH]; |
| 119 |
int bufsiz = sizeof(ret)/sizeof(ret[0]); |
| 120 |
char* dst = ret; |
| 121 |
|
| 122 |
src = get_phrasebook_entry(ch); |
| 123 |
if (src == NULL) { |
| 124 |
return NULL; |
| 125 |
} |
| 126 |
|
| 127 |
src = (const unsigned char*)strchr((const char*)src, '\n'); |
| 128 |
if (src == NULL) { |
| 129 |
return NULL; |
| 130 |
} |
| 131 |
++src; |
| 132 |
|
| 133 |
if (prettify) { |
| 134 |
*dst++ = '\t'; |
| 135 |
dst = prettify_annotation_start(dst, *src++, &bufsiz); |
| 136 |
*dst++ = ' '; |
| 137 |
} else { |
| 138 |
*dst++ = *src++; |
| 139 |
*dst++ = ' '; |
| 140 |
} |
| 141 |
bufsiz -= 2; |
| 142 |
|
| 143 |
while (*src != '\0' && bufsiz > 4) { |
| 144 |
switch (*src >> 4) { |
| 145 |
case 8: case 9: case 10: case 11: |
| 146 |
dst = read_lexicon(dst, ((src[0] & 0x3f) << 7) | (src[1] & 0x7f), &bufsiz); |
| 147 |
src += 2; |
| 148 |
break; |
| 149 |
case 15: |
| 150 |
*dst++ = *src++; |
| 151 |
--bufsiz; |
| 152 |
|
| 153 |
case 14: |
| 154 |
*dst++ = *src++; |
| 155 |
--bufsiz; |
| 156 |
|
| 157 |
case 12: case 13: |
| 158 |
*dst++ = *src++; |
| 159 |
--bufsiz; |
| 160 |
|
| 161 |
default: |
| 162 |
*dst = *src++; |
| 163 |
--bufsiz; |
| 164 |
if (*dst++ == '\n') { |
| 165 |
if (prettify) { |
| 166 |
*dst++ = '\t'; |
| 167 |
dst = prettify_annotation_start(dst, *src++, &bufsiz); |
| 168 |
*dst++ = ' '; |
| 169 |
} else { |
| 170 |
*dst++ = *src++; |
| 171 |
*dst++ = ' '; |
| 172 |
} |
| 173 |
bufsiz -= 2; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
assert(bufsiz > 4); |
| 179 |
if (bufsiz < 0) { |
| 180 |
bufsiz = 0; |
| 181 |
} |
| 182 |
return copyn(ret, sizeof(ret)/sizeof(ret[0]) - bufsiz); |
| 183 |
} |
| 184 |
|
| 185 |
char* uniname_formal_alias(unichar_t ch) { |
| 186 |
char *annot = uniname_annotation(ch, false), *ret = NULL; |
| 187 |
if (annot) { |
| 188 |
if (annot[0] == '%') { |
| 189 |
char* pt = strchr(annot, '\n'); |
| 190 |
if (pt) { |
| 191 |
*pt = '\0'; |
| 192 |
} |
| 193 |
ret = copy(annot + 2); |
| 194 |
} |
| 195 |
free(annot); |
| 196 |
} |
| 197 |
return ret; |
| 198 |
} |
| 199 |
|
| 200 |
static const struct unicode_range* find_range(unichar_t ch, const struct unicode_range* data, size_t hi) { |
| 201 |
size_t lo = 0, mid; |
| 202 |
|
| 203 |
while (lo <= hi) { |
| 204 |
mid = (lo+hi)/2; |
| 205 |
if (ch >= data[mid].start && ch <= data[mid].end) { |
| 206 |
return &data[mid]; |
| 207 |
} else if (data[mid].start < ch) { |
| 208 |
lo = mid + 1; |
| 209 |
} else { |
| 210 |
hi = mid - 1; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return NULL; |
| 215 |
} |
| 216 |
|
| 217 |
const struct unicode_range* uniname_block(unichar_t ch) { |
| 218 |
return find_range(ch, unicode_blocks, sizeof(unicode_blocks)/sizeof(unicode_blocks[0])); |
| 219 |
} |
| 220 |
|
| 221 |
const struct unicode_range* uniname_plane(unichar_t ch) { |
| 222 |
return find_range(ch, unicode_planes, sizeof(unicode_planes)/sizeof(unicode_planes[0])); |
| 223 |
} |
| 224 |
|
| 225 |
const struct unicode_range* uniname_blocks(int *sz) { |
| 226 |
if (sz) { |
| 227 |
*sz = sizeof(unicode_blocks)/sizeof(unicode_blocks[0]); |
| 228 |
} |
| 229 |
return unicode_blocks; |
| 230 |
} |
| 231 |
|
| 232 |
const struct unicode_range* uniname_planes(int *sz) { |
| 233 |
if (sz) { |
| 234 |
*sz = sizeof(unicode_planes)/sizeof(unicode_planes[0]); |
| 235 |
} |
| 236 |
return unicode_planes; |
| 237 |
} |