- (string-replace-char-string
- (string-replace-char-string string #\& "&")
- #\< "<"))
-
-(defun string-replace-char-string (string repl-char repl-str)
- "Replace all occurances of repl-char with repl-str"
- (declare (simple-string string))
- (let ((count (count repl-char string)))
- (declare (fixnum count))
- (if (zerop count)
- string
- (locally (declare (optimize (speed 3) (safety 0)))
- (let* ((old-length (length string))
- (repl-length (length repl-str))
- (new-string (make-string (the fixnum
- (+ old-length
- (the fixnum
- (* count
- (the fixnum (1- repl-length)))))))))
- (declare (fixnum old-length repl-length)
- (simple-string new-string))
- (let ((newpos 0))
- (declare (fixnum newpos))
- (dotimes (oldpos (length string))
- (declare (fixnum oldpos))
- (if (char= repl-char (schar string oldpos))
- (dotimes (repl-pos repl-length)
- (declare (fixnum repl-pos))
- (setf (schar new-string newpos) (schar repl-str repl-pos))
- (incf newpos))
- (progn
- (setf (schar new-string newpos) (schar string oldpos))
- (incf newpos)))))
- new-string)))))
-
-
+ (substitute-chars-strings
+ string '((#\& . "&") (#\> . ">") (#\< . "<") (#\" . """))))
+
+
+(defun make-usb8-array (len)
+ (make-array len :adjustable nil
+ :fill-pointer nil
+ :element-type '(unsigned-byte 8)))
+
+(defun usb8-array-to-string (vec)
+ (let* ((len (length vec))
+ (str (make-string len)))
+ (declare (fixnum len)
+ (simple-string str)
+ (optimize (speed 3)))
+ (dotimes (i len)
+ (declare (fixnum i))
+ (setf (schar str i) (code-char (aref vec i))))
+ str))
+
+(defun string-to-usb8-array (str)
+ (let* ((len (length str))
+ (vec (make-usb8-array len)))
+ (declare (fixnum len)
+ (type (array fixnum (*)) vec)
+ (optimize (speed 3)))
+ (dotimes (i len)
+ (declare (fixnum i))
+ (setf (aref vec i) (char-code (schar str i))))
+ vec))
+
+(defun concat-separated-strings (separator &rest lists)
+ (format nil (format nil "~~{~~A~~^~A~~}" separator) (append-sublists lists)))
+
+(defun only-null-list-elements-p (lst)
+ (or (null lst) (every #'null lst)))
+
+(defun print-separated-strings (strm separator &rest lists)
+ (declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
+ (compilation-speed 0)))
+ (do* ((rest-lists lists (cdr rest-lists))
+ (list (car rest-lists) (car rest-lists))
+ (last-list (only-null-list-elements-p (cdr rest-lists))
+ (only-null-list-elements-p (cdr rest-lists))))
+ ((null rest-lists) strm)
+ (do* ((lst list (cdr lst))
+ (elem (car lst) (car lst))
+ (last-elem (null (cdr lst)) (null (cdr lst))))
+ ((null lst))
+ (write-string elem strm)
+ (unless (and last-elem last-list)
+ (write-string separator strm)))))
+
+(defun prefixed-fixnum-string (num pchar len)
+ "Outputs a string of LEN digit with an optional initial character PCHAR.
+Leading zeros are present."
+ (declare (optimize (speed 3) (safety 0) (space 0))
+ (type fixnum num len))
+ (when pchar
+ (incf len))
+ (do* ((zero-code (char-code #\0))
+ (result (make-string len :initial-element #\0))
+ (minus? (minusp num))
+ (val (if minus? (- 0 num) num) (floor (/ val 10)))
+ (pos (1- len) (1- pos))
+ (mod (mod val 10) (mod val 10)))
+ ((or (zerop val) (minusp pos))
+ (when pchar
+ (setf (schar result 0) pchar))
+ (when minus? (setf (schar result (if pchar 1 0)) #\-))
+ result)
+ (declare (fixnum val mod zero-code pos) (simple-string result))
+ (setf (schar result pos) (code-char (+ zero-code mod)))))
+
+(defun integer-string (num len)
+ "Outputs a string of LEN digit with an optional initial character PCHAR.
+Leading zeros are present."
+ (declare (optimize (speed 3) (safety 0) (space 0))
+ (type fixnum len) (type integer num))
+ (do* ((zero-code (char-code #\0))
+ (result (make-string len :initial-element #\0))
+ (minus? (minusp num))
+ (val (if minus? (- 0 num) num) (floor (/ val 10)))
+ (pos (1- len) (1- pos))
+ (mod (mod val 10) (mod val 10)))
+ ((or (zerop val) (minusp pos))
+ (when minus? (setf (schar result (if pchar 1 0)) #\-))
+ result)
+ (declare (fixnum mod zero-code pos) (simple-string result) (integer val))
+ (setf (schar result pos) (code-char (+ zero-code mod)))))