Debian Codegraph @

src/utilities.lisp

raw
1 ;;; -*- Mode: LISP; Syntax: Ansi-Common-Lisp; Base: 10; Package: UAX-15 -*-
2
3 (in-package :uax-15)
4
5 (define-condition bad-char-error (error)
6 ((message
7 :initarg :message
8 :accessor bad-char-error-message
9 :initform nil
10 :documentation "Text message indicating what went wrong with the validation.")
11 (value
12 :initarg :value
13 :accessor bad-char-error-value
14 :initform nil
15 :documentation "The value of the field for which the error is signalled.")
16 (normalization-form
17 :initarg :normalization-form
18 :accessor bad-char-error-normalization-form
19 :initform nil
20 :documentation "The normalization form for the error was signalled.")))
21
22 (defmethod print-object ((object bad-char-error) stream)
23 (print-unreadable-object (object stream :type t :identity t)
24 (format stream "~@[L~A ~]~S~@[: ~S~]"
25 (bad-char-error-normalization-form object)
26 (bad-char-error-message object)
27 (bad-char-error-value object))))
28
29 (defun bad-char-error (message &key value normalization-form)
30 (error 'bad-char-error
31 :message message
32 :value value
33 :normalization-form normalization-form))
34
35 (defmacro nconcf (list1 list2) `(setf ,list1 (nconc ,list1 ,list2)))
36
37 (defun parse-hex-string-to-string (str)
38 "Takes a string which may be one or more hex numbers e.g. '0044 0307', builds an array of characters, coerces to string and returns the string. Mostly used for testing."
39 (let* ((split-str (split-sequence:split-sequence #\Space str :remove-empty-subseqs t))
40 (arry (make-array (length split-str))))
41 (loop for x in split-str counting x into y do
42 (setf (aref arry (- y 1)) (parse-hex-string-to-char x)))
43 (coerce arry 'string)))
44
45 (defun parse-hex-string-to-int (str)
46 "Parse a string which is a single character in hex to a decimal."
47 (parse-integer str :radix 16))
48
49 (defun parse-hex-string-to-char (str)
50 "Parse a hex string which is a single character into a character using code-char."
51 (code-char (parse-hex-string-to-int str)))
52
53 (defun int-to-hex-string (int)
54 (write-to-string int :base 16))
55
56 (defun parse-hex-list-to-string (lst)
57 "Takes a list of numbers and returns a string of characters"
58 (let ((arry (make-array (length lst))))
59 (loop for x in lst counting x into y do
60 (setf (aref arry (- y 1)) (code-char x)))
61 (coerce arry 'string)))