;; -*- Mode: Lisp; -*- ;; --------------------------------------------------------------------------- ;; Title: ASCII variant of DIGIT-CHAR-P and friends ;; Created: 2020-01-14 ;; Author: Gilbert Baumann ;; License: MIT style (see below) ;; --------------------------------------------------------------------------- ;; (c) copyright 2020 by Gilbert Baumann ;; Permission is hereby granted, free of charge, to any person obtaining ;; a copy of this software and associated documentation files (the ;; "Software"), to deal in the Software without restriction, including ;; without limitation the rights to use, copy, modify, merge, publish, ;; distribute, sublicense, and/or sell copies of the Software, and to ;; permit persons to whom the Software is furnished to do so, subject to ;; the following conditions: ;; ;; The above copyright notice and this permission notice shall be ;; included in all copies or substantial portions of the Software. ;; ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. (defpackage :de.bauhh.ascii (:use :common-lisp) (:export #:ascii-char-p #:ascii-digit-char-p #:ascii-alpha-char-p #:ascii-alphanumeric-p #:ascii-graphic-char-p #:ascii-white-space-p)) (in-package :de.bauhh.ascii) ;;;; -- Overview -------------------------------------------------------------- ;; Some Lisps like to extend the definition of the character classification ;; predicates like ALPHA-CHAR-P etc to Unicode characters. However, when ;; parsing protocols and file formats this often is wrong as we may need to ;; match in the ASCII range only. ;;;; -- Implementation -------------------------------------------------------- (defun ascii-char-p (char) "Returns non-NIL, when `char` is an ASCII character, that is a 7-bit character and NIL otherwise." (< (char-code char) 128)) (defun ascii-digit-char-p (char &optional (radix 10)) "Like DIGIT-CHAR-P, but will return non-NIL only for ASCII characters." (and (<= (char-code char) 127) (digit-char-p char radix))) (defun ascii-alpha-char-p (char) "Like ALPHA-CHAR-P, but will return non-NIL only for ASCII characters." (or (char<= #\a char #\z) (char<= #\A char #\Z))) (defun ascii-alphanumeric-p (char) "Like ALPHANUMERICP, but will return non-NIL only for ASCII characters." (or (char<= #\0 char #\9) (char<= #\a char #\z) (char<= #\A char #\Z))) (defun ascii-graphic-char-p (char) "Like GRAPHIC-CHAR-P, but will return non-NIL only for ASCII characters." (char<= #\space char #\~)) (defun ascii-white-space-p (char) "Returns non-NIL when _char_ is an ASCII white space character. The following characters are regarded as white space: HT (decimal 9), NL (decimal 10), FF (decimal 12), CR (decimal 13), SP (decimal 32)." (case (char-code char) ((9 10 12 13 32) t) (t nil)))