| 1 |
;;; -*- Mode: LISP; Syntax: Ansi-Common-Lisp; Base: 10; Package: UAX-15 -*- |
| 2 |
|
| 3 |
(in-package :uax-15) |
| 4 |
|
| 5 |
(defun normalize (str normalization-form &key (rfc 3454)) |
| 6 |
"Base external function which calls the appropriate normalization for the normalization form. The default normaliation form is :nfkc, but :nfd, :nfkd and :nfc are also available." |
| 7 |
(if (= rfc 3454) |
| 8 |
(ecase normalization-form |
| 9 |
(:nfd (from-unicode-string (nfd (to-unicode-string str)))) |
| 10 |
(:nfkd (from-unicode-string (nfkd (to-unicode-string str)))) |
| 11 |
(:nfc (from-unicode-string (nfc (to-unicode-string str)))) |
| 12 |
(:nfkc (from-unicode-string (nfkc (to-unicode-string str))))) |
| 13 |
(cerror "RFCs other than 3454 and 4013 not yet supported. Sorry" (format nil "~a" rfc)))) |
| 14 |
|
| 15 |
(defun normalize-char (chr normalization-form) |
| 16 |
"Runs normalize on a single character input and returns a single character string. You must provide the normalization form (:nfd, :nfkd, :nfc, or :nfkc)" |
| 17 |
(ecase normalization-form |
| 18 |
(:nfd (from-unicode-string (nfd (to-unicode-string (format nil "~a" chr))))) |
| 19 |
(:nfkd (from-unicode-string (nfkd (to-unicode-string (format nil "~a" chr))))) |
| 20 |
(:nfc (from-unicode-string (nfc (to-unicode-string (format nil "~a" chr))))) |
| 21 |
(:nfkc (from-unicode-string (nfkc (to-unicode-string (format nil "~a" chr))))))) |
| 22 |
|
| 23 |
(defun get-mapping (normalization-form &aux (mapping '())) |
| 24 |
"Note no mapping for :nfkc" |
| 25 |
(dolist (map (ecase normalization-form |
| 26 |
(:nfd (list *canonical-decomp-map*)) |
| 27 |
(:nfkd (list *canonical-decomp-map* *compatible-decomp-map*)) |
| 28 |
(:nfc (list *canonical-comp-map*)))) |
| 29 |
(maphash |
| 30 |
(lambda (from to) |
| 31 |
(flet ((to-str (x) |
| 32 |
(if (listp x) (coerce x 'string) (string x)))) |
| 33 |
(case normalization-form |
| 34 |
(:nfd (push (list (to-str from) (decompose (to-str to) :canonical)) mapping)) |
| 35 |
(:nfkd (push (list (to-str from) (decompose (to-str to) :compatible)) mapping)) |
| 36 |
(:nfc (when (string= (compose (to-str from)) (to-str to)) |
| 37 |
(push (list (decompose (to-str from) :canonical) (to-str to)) mapping)))))) |
| 38 |
map)) |
| 39 |
|
| 40 |
;; hangul |
| 41 |
(loop for code from #xAC00 below (+ #xAC00 11172) |
| 42 |
for char = (string (code-char code)) |
| 43 |
do |
| 44 |
(case normalization-form |
| 45 |
((:nfd :nfkd) (push (list char (decompose char :canonical)) mapping)) |
| 46 |
((:nfc) (push (list (decompose char :canonical) char) mapping)))) |
| 47 |
|
| 48 |
(nreverse mapping)) |
| 49 |
|
| 50 |
(defparameter *derived-normalization-props-data* (make-hash-table :test #'equal)) |
| 51 |
|
| 52 |
(defparameter *derived-normalization-props-data-file* |
| 53 |
(uiop:merge-pathnames* *data-directory* "DerivedNormalizationProps.txt")) |
| 54 |
|
| 55 |
(defun get-canonical-combining-class-map () |
| 56 |
*canonical-combining-class*) |
| 57 |
|
| 58 |
(let ((nfd-illegal-list '()) |
| 59 |
(nfkd-illegal-list '()) |
| 60 |
(nfc-illegal-list '()) |
| 61 |
(nfkc-illegal-list '())) |
| 62 |
|
| 63 |
(flet ((parse-line (maybe-key line) |
| 64 |
(let* ((fst (string-trim " " (car (cl-ppcre:split ";" line)))) |
| 65 |
(range (mapcar #'parse-hex-string-to-int (cl-ppcre:split "\\.\\." fst))) |
| 66 |
(maybe? (and maybe-key (not (null (search maybe-key line)))))) |
| 67 |
(loop for code from (first range) to (or (second range) (first range)) |
| 68 |
for char = code |
| 69 |
collect (list char maybe?))))) |
| 70 |
(with-open-file (in *derived-normalization-props-data-file* |
| 71 |
#-(or :lispworks :genera :clisp) :external-format |
| 72 |
#-(or :lispworks :genera :clisp) :UTF-8) |
| 73 |
(loop for line = (read-line in nil nil) |
| 74 |
while line |
| 75 |
do |
| 76 |
(cond ((or (search "NFKC_QC; N" line) (search "NFKC_QC; M" line)) |
| 77 |
(nconcf nfkc-illegal-list (parse-line "NFKC_QC; M" line))) |
| 78 |
((or (search "NFC_QC; N" line) (search "NFC_QC; M" line)) |
| 79 |
(nconcf nfc-illegal-list (parse-line "NFC_QC; M" line))) |
| 80 |
((search "NFKD_QC; N" line) |
| 81 |
(nconcf nfkd-illegal-list (parse-line nil line))) |
| 82 |
((search "NFD_QC; N" line) |
| 83 |
(nconcf nfd-illegal-list (parse-line nil line))))))) |
| 84 |
|
| 85 |
(defun get-illegal-char-list (normalization-form) |
| 86 |
"Takes a normalization form, e.g. :nfkc and returns a list of lists of form (#\NO-BREAK_SPACE NIL) where the first item is the character name and the second item has the value N or M or nil indicating whether the character may require renormalization." |
| 87 |
(ecase normalization-form |
| 88 |
(:nfd nfd-illegal-list) |
| 89 |
(:nfkd nfkd-illegal-list) |
| 90 |
(:nfc nfc-illegal-list) |
| 91 |
(:nfkc nfkc-illegal-list)))) |
| 92 |
|
| 93 |
(defun unicode-letter-p (char) |
| 94 |
"Returns T if the character is one of the unicode characters falling into a letter category: uppercase, lowercase, titlecase, modifier and other." |
| 95 |
(when (gethash char *unicode-letters*) |
| 96 |
t)) |