misc:software:Common Lisp:sbcl:utf-16の文字のリストを取得する

misc:software:Common Lisp:sbcl:utf-16の文字のリストを取得する

文字のリストを取得する

code-char 関数と char-code 関数のつぎのような使い方を利用する。

(char-code #\あ)
; => 12354

(char-code "あ")
; => The value "あ" is not of type CHARACTER.

(char-code (coerce "あ" 'character))
; => 12354


(let ((x (char-code #\あ)))
  (code-char x))
; => #\HIRAGANA_LETTER_A

(let* ((x1 (char-code #\あ))
       (x2 (code-char x1)))
  (coerce (list x2) 'string))
; => "あ"
(defun list-of-chars-utf-16 (start end)
  (labels ((get-char-code (char-or-string)
             (char-code (coerce char-or-string 'character)))
           (to-code (x)
             (if (typep x 'number)
                 x
                 (get-char-code x))))
    (loop for i
       from (to-code start)
       to (to-code end)
       collect i)))

(defun list-of-chars-with-the-code-attribute (start end)
  (mapcar #'(lambda (x) (code-char x))
          (list-of-chars-utf-16 start end)))

(defun list-of-chars-string (start end)
  (labels ((code-string (code)
             (coerce (list (code-char code)) 'string)))
    (mapcar #'(lambda (x) (code-string x))
            (list-of-chars-utf-16 start end))))

(defun list-of-string (start end)
  (format nil "~{~A~}" (list-of-chars-string start end)))
(list-of-chars-with-the-code-attribute #\あ #\ん)
; =>

(list-of-chars-utf-16 #\あ #\ん)
; =>

文字から文字のリストを取得する

(list-of-chars-string #\あ #\ん)
; =>

(list-of-chars-string "ア" "ン")
; =>

(list-of-string #\あ #\ん)
; => "あぃいぅうぇえぉおかがきぎくぐけげこごさざしじすずせぜそぞただちぢっつづてでとどなにぬねのはばぱひびぴふぶぷへべぺほぼぽまみむめもゃやゅゆょよらりるれろゎわゐゑをん"

数値から文字のリストを取得する

上記と同一の関数を利用し、引数として数値をわたす。

(list-of-chars-string #x3042 #x3093)
; =>

Last modified : 2016/12/28 17:29:06 JST