| 1 |
(defparameter *destination* |
| 2 |
(merge-pathnames "../../src/c/unicode/" |
| 3 |
(or *load-truename* *compile-pathname*))) |
| 4 |
|
| 5 |
(let* ((translated-data (copy-tree *compressed-data*)) |
| 6 |
(pairs (copy-tree *paired-data*)) |
| 7 |
(first-code (loop for (pair-code . pair) in pairs minimize pair-code)) |
| 8 |
(last-code (loop for (pair-code . pair) in pairs maximize pair-code))) |
| 9 |
;; |
| 10 |
;; We make sure that for each character there is a unique pair which is not |
| 11 |
;; used anywhere else |
| 12 |
;; |
| 13 |
(loop with used-code = (make-array (1+ last-code) :initial-element nil) |
| 14 |
for line in translated-data |
| 15 |
for pair-code = (third line) |
| 16 |
do (cond ((/= (length line) 3) |
| 17 |
(error "Error in compressed data: too long code ~A" line)) |
| 18 |
((or (aref used-code pair-code) |
| 19 |
(< pair-code first-code)) |
| 20 |
(let ((new-pair (cons pair-code 0))) |
| 21 |
(setf pairs (acons (incf last-code) new-pair pairs) |
| 22 |
(third line) last-code))) |
| 23 |
(t |
| 24 |
(setf (aref used-code pair-code) t)))) |
| 25 |
;; |
| 26 |
;; We now renumber all pairs. |
| 27 |
;; |
| 28 |
(let ((translation-table (make-array (1+ last-code) :initial-element nil)) |
| 29 |
(counter -1)) |
| 30 |
(flet ((add-code (code) |
| 31 |
(or (aref translation-table code) |
| 32 |
(setf (aref translation-table code) (incf counter)))) |
| 33 |
(translate (old-code) |
| 34 |
(or (aref translation-table old-code) |
| 35 |
(error "Unknown code ~A" old-code)))) |
| 36 |
;; First of all we add the words |
| 37 |
(loop for i from 0 below first-code |
| 38 |
do (add-code i)) |
| 39 |
;; Then we add all pairs that represent characters, so that they |
| 40 |
;; are consecutive, too. |
| 41 |
(loop for line in translated-data |
| 42 |
do (setf (third line) (add-code (third line)))) |
| 43 |
;; Finally, we add the remaining pairs |
| 44 |
(loop for record in pairs |
| 45 |
do (setf (car record) (add-code (car record)))) |
| 46 |
;; ... and we fix the definitions |
| 47 |
(loop for (code . pair) in pairs |
| 48 |
do (setf (car pair) (translate (car pair)) |
| 49 |
(cdr pair) (translate (cdr pair)))))) |
| 50 |
(defparameter *sorted-compressed-data* translated-data) |
| 51 |
(defparameter *sorted-pairs* (sort pairs #'< :key #'car)) |
| 52 |
(print 'finished) |
| 53 |
) |
| 54 |
|
| 55 |
(defparameter *grouped-characters* |
| 56 |
(loop with last-ucd-code = nil |
| 57 |
with start-ucd-code = nil |
| 58 |
with start-code = nil |
| 59 |
with output = '() |
| 60 |
with aux = '() |
| 61 |
for n from (third (first *sorted-compressed-data*)) |
| 62 |
for line in *sorted-compressed-data* |
| 63 |
for (ucd-code name code) = line |
| 64 |
do (cond ((/= code n) |
| 65 |
(error "Codes in *sorted-compressed-data* are not consecutive:~%~A" |
| 66 |
(cons line (subseq aux 0 10)))) |
| 67 |
((null start-ucd-code) |
| 68 |
(setf start-ucd-code ucd-code |
| 69 |
start-code code)) |
| 70 |
((= last-ucd-code (1- ucd-code)) |
| 71 |
) |
| 72 |
(t |
| 73 |
(push (list start-ucd-code last-ucd-code start-code) |
| 74 |
output) |
| 75 |
(setf start-ucd-code ucd-code |
| 76 |
start-code code))) |
| 77 |
(setf last-ucd-code ucd-code aux (cons line aux)) |
| 78 |
finally (return (nreverse output)))) |
| 79 |
|
| 80 |
(with-open-file (s (merge-pathnames "ucd_names.h" *destination*) |
| 81 |
:direction :output |
| 82 |
:if-exists :supersede) |
| 83 |
(format s "/* |
| 84 |
* UNICODE NAMES DATABASE |
| 85 |
* |
| 86 |
* auto-generated, do not edit! (see contrib/unicode/) |
| 87 |
*/ |
| 88 |
#ifndef ECL_UCD_NAMES_H |
| 89 |
#define ECL_UCD_NAMES_H 1 |
| 90 |
|
| 91 |
#define ECL_UCD_FIRST_PAIR ~D |
| 92 |
#define ECL_UCD_TOTAL_PAIRS ~D |
| 93 |
#define ECL_UCD_TOTAL_GROUPS ~D |
| 94 |
#define ECL_UCD_LARGEST_CHAR_NAME ~D |
| 95 |
#define ECL_UCD_TOTAL_NAMES ~D |
| 96 |
|
| 97 |
typedef struct { |
| 98 |
unsigned char codes[6]; |
| 99 |
} ecl_ucd_names_pair_type; |
| 100 |
|
| 101 |
typedef struct { |
| 102 |
int smallest, largest, pair_code; |
| 103 |
} ecl_ucd_names_char_group; |
| 104 |
|
| 105 |
typedef struct { |
| 106 |
unsigned char pair[2]; |
| 107 |
unsigned char code[3]; |
| 108 |
} ecl_ucd_code_and_pair; |
| 109 |
|
| 110 |
extern const ecl_ucd_names_pair_type ecl_ucd_names_pair[ECL_UCD_TOTAL_PAIRS]; |
| 111 |
extern const ecl_ucd_names_char_group ecl_ucd_names_char[ECL_UCD_TOTAL_GROUPS]; |
| 112 |
extern const char *ecl_ucd_names_word[ECL_UCD_FIRST_PAIR]; |
| 113 |
extern const ecl_ucd_code_and_pair ecl_ucd_sorted_pairs[ECL_UCD_TOTAL_NAMES]; |
| 114 |
|
| 115 |
#endif |
| 116 |
" |
| 117 |
(1+ *last-word-index*) |
| 118 |
(length *sorted-pairs*) |
| 119 |
(length *grouped-characters*) |
| 120 |
(loop for (code name . rest) in *compressed-data* |
| 121 |
maximize (length name)) |
| 122 |
(length *compressed-data*) |
| 123 |
)) |
| 124 |
|
| 125 |
(with-open-file (s (merge-pathnames "ucd_names_pair.c" *destination*) |
| 126 |
:direction :output |
| 127 |
:if-exists :supersede) |
| 128 |
(format s "/* |
| 129 |
* Pairs of symbols. |
| 130 |
* |
| 131 |
* auto-generated, do not edit! (see contrib/unicode/) |
| 132 |
*/ |
| 133 |
|
| 134 |
#include <ecl/ecl.h> |
| 135 |
#include \"ucd_names.h\" |
| 136 |
|
| 137 |
const ecl_ucd_names_pair_type ecl_ucd_names_pair[ECL_UCD_TOTAL_PAIRS] = { |
| 138 |
" |
| 139 |
(length *sorted-pairs*) (length *sorted-pairs*)) |
| 140 |
(loop for i from 0 |
| 141 |
for (pair-code . (a . b)) in *sorted-pairs* |
| 142 |
do (assert (< a (ash 1 24))) |
| 143 |
(assert (< b (ash 1 24))) |
| 144 |
(format s "~A{~D, ~D, ~D, ~D, ~D, ~D}~%" |
| 145 |
(if (plusp i) "," "") |
| 146 |
(logand a #xff) (logand (ash a -8) #xff) (ash a -16) |
| 147 |
(logand b #xff) (logand (ash b -8) #xff) (ash b -16))) |
| 148 |
(format s "};~%")) |
| 149 |
|
| 150 |
(with-open-file (s (merge-pathnames "ucd_names_codes.c" *destination*) |
| 151 |
:direction :output |
| 152 |
:if-exists :supersede) |
| 153 |
(format s "/* |
| 154 |
* Sorted character names. |
| 155 |
* |
| 156 |
* auto-generated, do not edit! (see contrib/unicode/) |
| 157 |
*/ |
| 158 |
|
| 159 |
#include <ecl/ecl.h> |
| 160 |
#include \"ucd_names.h\" |
| 161 |
|
| 162 |
const ecl_ucd_code_and_pair ecl_ucd_sorted_pairs[ECL_UCD_TOTAL_NAMES] = { |
| 163 |
") |
| 164 |
(loop with l = (sort (copy-tree *sorted-compressed-data*) #'string<= :key #'second) |
| 165 |
for (ucd-code name code) in l |
| 166 |
for i from 0 |
| 167 |
do (assert (< code (ash 1 16))) |
| 168 |
(assert (< ucd-code (ash 1 24))) |
| 169 |
(format s "~A{{~D, ~D}, {~D, ~D, ~D}}~%" |
| 170 |
(if (plusp i) "," "") |
| 171 |
(logand code #xff) (ash code -8) |
| 172 |
(logand ucd-code #xff) (logand (ash ucd-code -8) #xff) |
| 173 |
(logand (ash ucd-code -16) #xff))) |
| 174 |
(format s "};")) |
| 175 |
|
| 176 |
(with-open-file (s (merge-pathnames "ucd_names_str.c" *destination*) |
| 177 |
:direction :output |
| 178 |
:if-exists :supersede) |
| 179 |
(format s "/* |
| 180 |
* Dictionary words. |
| 181 |
* |
| 182 |
* auto-generated, do not edit! (see contrib/unicode/) |
| 183 |
*/ |
| 184 |
|
| 185 |
#include <ecl/ecl.h> |
| 186 |
#include \"ucd_names.h\" |
| 187 |
|
| 188 |
const char *ecl_ucd_names_word[ECL_UCD_FIRST_PAIR] = { |
| 189 |
") |
| 190 |
(loop for i from 0 |
| 191 |
for c across *words-array* |
| 192 |
do (format s "~A~S~%" (if (plusp i) "," "") (or c ""))) |
| 193 |
(format s "};~%")) |
| 194 |
|
| 195 |
(with-open-file (s (merge-pathnames "ucd_names_char.c" *destination*) |
| 196 |
:direction :output |
| 197 |
:if-exists :supersede) |
| 198 |
(format s "/* |
| 199 |
* Dictionary words. |
| 200 |
* |
| 201 |
* auto-generated, do not edit! (see contrib/unicode/) |
| 202 |
*/ |
| 203 |
|
| 204 |
#include <string.h> |
| 205 |
#include <ecl/ecl.h> |
| 206 |
#include \"ucd_names.h\" |
| 207 |
|
| 208 |
const ecl_ucd_names_char_group ecl_ucd_names_char[ECL_UCD_TOTAL_GROUPS] = { |
| 209 |
" |
| 210 |
(length *grouped-characters*)) |
| 211 |
(loop for i from 0 |
| 212 |
for (start end pair-code) in *grouped-characters* |
| 213 |
do (format s "~A{~D,~D,~D}~%" (if (plusp i) "," "") |
| 214 |
start end pair-code)) |
| 215 |
(format s "}; |
| 216 |
|
| 217 |
static int |
| 218 |
search_pair(ecl_character c) |
| 219 |
{ |
| 220 |
int mid, low = 0, up = ECL_UCD_TOTAL_GROUPS-1; |
| 221 |
do { |
| 222 |
mid = (up + low) / 2; |
| 223 |
if (c < ecl_ucd_names_char[mid].smallest) |
| 224 |
up = mid-1; |
| 225 |
else if (c > ecl_ucd_names_char[mid].largest) |
| 226 |
low = mid+1; |
| 227 |
else |
| 228 |
return (c - ecl_ucd_names_char[mid].smallest) + |
| 229 |
ecl_ucd_names_char[mid].pair_code; |
| 230 |
} while (low <= up && (low >= 0) && (up < ECL_UCD_TOTAL_GROUPS)); |
| 231 |
return -1; |
| 232 |
} |
| 233 |
|
| 234 |
static void |
| 235 |
fill_pair_name(char *buffer, int pair) |
| 236 |
{ |
| 237 |
if (pair < ECL_UCD_FIRST_PAIR) { |
| 238 |
strncat(buffer, ecl_ucd_names_word[pair], ECL_UCD_LARGEST_CHAR_NAME+1); |
| 239 |
/* |
| 240 |
printf(\"text=%s\\n\", ecl_ucd_names_word[pair]); |
| 241 |
*/ |
| 242 |
} else { |
| 243 |
const ecl_ucd_names_pair_type p = ecl_ucd_names_pair[pair - ECL_UCD_FIRST_PAIR]; |
| 244 |
/* |
| 245 |
printf(\"ndx=%d\\n\", pair - ECL_UCD_FIRST_PAIR); |
| 246 |
printf(\"c0=%d\\n\", ecl_ucd_names_pair[pair - ECL_UCD_FIRST_PAIR].codes[0]); |
| 247 |
printf(\"c1=%d\\n\", ecl_ucd_names_pair[pair - ECL_UCD_FIRST_PAIR].codes[1]); |
| 248 |
printf(\"c2=%d\\n\", ecl_ucd_names_pair[pair - ECL_UCD_FIRST_PAIR].codes[2]); |
| 249 |
printf(\"c3=%d\\n\", ecl_ucd_names_pair[pair - ECL_UCD_FIRST_PAIR].codes[3]); |
| 250 |
printf(\"c4=%d\\n\", ecl_ucd_names_pair[pair - ECL_UCD_FIRST_PAIR].codes[4]); |
| 251 |
printf(\"c5=%d\\n\", ecl_ucd_names_pair[pair - ECL_UCD_FIRST_PAIR].codes[5]); |
| 252 |
*/ |
| 253 |
fill_pair_name(buffer, (((unsigned int)p.codes[2]) << 16) | (((unsigned int)p.codes[1]) << 8) | p.codes[0]); |
| 254 |
fill_pair_name(buffer, (((unsigned int)p.codes[5]) << 16) | (((unsigned int)p.codes[4]) << 8) | p.codes[3]); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
cl_object |
| 259 |
_ecl_ucd_code_to_name(ecl_character c) |
| 260 |
{ |
| 261 |
int pair = search_pair(c); |
| 262 |
if (pair < 0) |
| 263 |
return ECL_NIL; |
| 264 |
else { |
| 265 |
char buffer[ECL_UCD_LARGEST_CHAR_NAME+1]; |
| 266 |
buffer[0] = 0; |
| 267 |
fill_pair_name(buffer, pair); |
| 268 |
return ecl_make_simple_base_string(buffer,-1); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
cl_object |
| 273 |
_ecl_ucd_name_to_code(cl_object name) |
| 274 |
{ |
| 275 |
int mid, low = 0, up = ECL_UCD_TOTAL_NAMES-1; |
| 276 |
int l = ecl_length(name); |
| 277 |
if (l <= ECL_UCD_LARGEST_CHAR_NAME) { |
| 278 |
char buffer1[ECL_UCD_LARGEST_CHAR_NAME+1]; |
| 279 |
char buffer2[ECL_UCD_LARGEST_CHAR_NAME+1]; |
| 280 |
for (mid = 0; mid < l; mid++) { |
| 281 |
ecl_character c = ecl_char_upcase(ecl_char(name, mid)); |
| 282 |
buffer1[mid] = c; |
| 283 |
if (c < 32 || c > 127) /* All character names are [-A-Z_0-9]* */ |
| 284 |
return ECL_NIL; |
| 285 |
} |
| 286 |
buffer1[mid] = 0; |
| 287 |
do { |
| 288 |
ecl_ucd_code_and_pair p = ecl_ucd_sorted_pairs[mid = (low + up) / 2]; |
| 289 |
int flag, pair = ((unsigned int)p.pair[1] << 8) | p.pair[0]; |
| 290 |
buffer2[0] = 0; |
| 291 |
fill_pair_name(buffer2, pair); |
| 292 |
flag = strcmp(buffer1, buffer2); |
| 293 |
/* |
| 294 |
printf(\"[%d,%d,%d] %s <> (%d)%s -> %d\\n\", |
| 295 |
low, mid, up, buffer1, pair, buffer2, flag); |
| 296 |
*/ |
| 297 |
if (flag == 0) { |
| 298 |
return ecl_make_fixnum(((unsigned int)p.code[2] << 16) | |
| 299 |
((unsigned int)p.code[1] << 8) | |
| 300 |
p.code[0]); |
| 301 |
} else if (flag < 0) { |
| 302 |
up = mid - 1; |
| 303 |
} else { |
| 304 |
low = mid + 1; |
| 305 |
} |
| 306 |
} while (low <= up); |
| 307 |
} |
| 308 |
return ECL_NIL; |
| 309 |
} |
| 310 |
|
| 311 |
")) |