1 | ;;; chars.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003 Peter Graves |
---|
4 | ;;; $Id: chars.lisp,v 1.2 2003-03-14 02:11:21 piso Exp $ |
---|
5 | ;;; |
---|
6 | ;;; This program is free software; you can redistribute it and/or |
---|
7 | ;;; modify it under the terms of the GNU General Public License |
---|
8 | ;;; as published by the Free Software Foundation; either version 2 |
---|
9 | ;;; of the License, or (at your option) any later version. |
---|
10 | ;;; |
---|
11 | ;;; This program is distributed in the hope that it will be useful, |
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | ;;; GNU General Public License for more details. |
---|
15 | ;;; |
---|
16 | ;;; You should have received a copy of the GNU General Public License |
---|
17 | ;;; along with this program; if not, write to the Free Software |
---|
18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | |
---|
20 | (in-package "COMMON-LISP") |
---|
21 | |
---|
22 | (export '(digit-char-p alphanumericp |
---|
23 | char/= char< char> char<= char>= |
---|
24 | char-not-equal |
---|
25 | char-lessp char-greaterp)) |
---|
26 | |
---|
27 | |
---|
28 | ;;; From CMUCL. |
---|
29 | |
---|
30 | (defun digit-char-p (char &optional (radix 10)) |
---|
31 | "If char is a digit in the specified radix, returns the fixnum for |
---|
32 | which that digit stands, else returns NIL. Radix defaults to 10 |
---|
33 | (decimal)." |
---|
34 | (declare (character char) (type (integer 2 36) radix)) |
---|
35 | (let ((m (- (char-code char) 48))) |
---|
36 | (declare (fixnum m)) |
---|
37 | (cond ((<= radix 10) |
---|
38 | ;; Special-case decimal and smaller radices. |
---|
39 | (if (and (>= m 0) (< m radix)) m nil)) |
---|
40 | ;; Digits 0 - 9 are used as is, since radix is larger. |
---|
41 | ((and (>= m 0) (< m 10)) m) |
---|
42 | ;; Check for upper case A - Z. |
---|
43 | ((and (>= (setq m (- m 7)) 10) (< m radix)) m) |
---|
44 | ;; Also check lower case a - z. |
---|
45 | ((and (>= (setq m (- m 32)) 10) (< m radix)) m) |
---|
46 | ;; Else, fail. |
---|
47 | (t nil)))) |
---|
48 | |
---|
49 | |
---|
50 | (defun alphanumericp (char) |
---|
51 | "Given a character-object argument, alphanumericp returns T if the |
---|
52 | argument is either numeric or alphabetic." |
---|
53 | (declare (character char)) |
---|
54 | (let ((m (char-code char))) |
---|
55 | (or (< 47 m 58) (< 64 m 91) (< 96 m 123)))) |
---|
56 | |
---|
57 | |
---|
58 | (defun char/= (character &rest more-characters) |
---|
59 | "Returns T if no two of its arguments are the same character." |
---|
60 | (do* ((head character (car list)) |
---|
61 | (list more-characters (cdr list))) |
---|
62 | ((atom list) T) |
---|
63 | (unless (do* ((l list (cdr l))) ;inner loop returns T |
---|
64 | ((atom l) T) ; iff head /= rest. |
---|
65 | (if (eq head (car l)) (return nil))) |
---|
66 | (return nil)))) |
---|
67 | |
---|
68 | |
---|
69 | (defun char< (character &rest more-characters) |
---|
70 | "Returns T if its arguments are in strictly increasing alphabetic order." |
---|
71 | (do* ((c character (car list)) |
---|
72 | (list more-characters (cdr list))) |
---|
73 | ((atom list) T) |
---|
74 | (unless (< (char-int c) |
---|
75 | (char-int (car list))) |
---|
76 | (return nil)))) |
---|
77 | |
---|
78 | |
---|
79 | (defun char> (character &rest more-characters) |
---|
80 | "Returns T if its arguments are in strictly decreasing alphabetic order." |
---|
81 | (do* ((c character (car list)) |
---|
82 | (list more-characters (cdr list))) |
---|
83 | ((atom list) T) |
---|
84 | (unless (> (char-int c) |
---|
85 | (char-int (car list))) |
---|
86 | (return nil)))) |
---|
87 | |
---|
88 | |
---|
89 | (defun char<= (character &rest more-characters) |
---|
90 | "Returns T if its arguments are in strictly non-decreasing alphabetic order." |
---|
91 | (do* ((c character (car list)) |
---|
92 | (list more-characters (cdr list))) |
---|
93 | ((atom list) T) |
---|
94 | (unless (<= (char-int c) |
---|
95 | (char-int (car list))) |
---|
96 | (return nil)))) |
---|
97 | |
---|
98 | |
---|
99 | (defun char>= (character &rest more-characters) |
---|
100 | "Returns T if its arguments are in strictly non-increasing alphabetic order." |
---|
101 | (do* ((c character (car list)) |
---|
102 | (list more-characters (cdr list))) |
---|
103 | ((atom list) T) |
---|
104 | (unless (>= (char-int c) |
---|
105 | (char-int (car list))) |
---|
106 | (return nil)))) |
---|
107 | |
---|
108 | |
---|
109 | (defmacro equal-char-code (character) |
---|
110 | `(let ((ch (char-code ,character))) |
---|
111 | (if (< 96 ch 123) (- ch 32) ch))) |
---|
112 | |
---|
113 | |
---|
114 | (defun char-not-equal (character &rest more-characters) |
---|
115 | "Returns T if no two of its arguments are the same character. |
---|
116 | Font, bits, and case are ignored." |
---|
117 | (do* ((head character (car list)) |
---|
118 | (list more-characters (cdr list))) |
---|
119 | ((atom list) T) |
---|
120 | (unless (do* ((l list (cdr l))) |
---|
121 | ((atom l) T) |
---|
122 | (if (= (equal-char-code head) |
---|
123 | (equal-char-code (car l))) |
---|
124 | (return nil))) |
---|
125 | (return nil)))) |
---|
126 | |
---|
127 | |
---|
128 | (defun char-lessp (character &rest more-characters) |
---|
129 | "Returns T if its arguments are in strictly increasing alphabetic order. |
---|
130 | Font, bits, and case are ignored." |
---|
131 | (do* ((c character (car list)) |
---|
132 | (list more-characters (cdr list))) |
---|
133 | ((atom list) T) |
---|
134 | (unless (< (equal-char-code c) |
---|
135 | (equal-char-code (car list))) |
---|
136 | (return nil)))) |
---|
137 | |
---|
138 | |
---|
139 | (defun char-greaterp (character &rest more-characters) |
---|
140 | "Returns T if its arguments are in strictly decreasing alphabetic order. |
---|
141 | Font, bits, and case are ignored." |
---|
142 | (do* ((c character (car list)) |
---|
143 | (list more-characters (cdr list))) |
---|
144 | ((atom list) T) |
---|
145 | (unless (> (equal-char-code c) |
---|
146 | (equal-char-code (car list))) |
---|
147 | (return nil)))) |
---|