KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > CharacterFunctions


1 /*
2  * CharacterFunctions.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: CharacterFunctions.java,v 1.8 2004/03/17 17:54:10 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.lisp;
23
24 public final class CharacterFunctions extends Lisp
25 {
26     // ### char=
27
private static final Primitive CHAR_EQUALS = new Primitive("char=","&rest characters") {
28         public LispObject execute(LispObject first, LispObject second)
29             throws ConditionThrowable
30         {
31             return LispCharacter.getValue(first) == LispCharacter.getValue(second) ? T : NIL;
32         }
33         public LispObject execute(LispObject[] array) throws ConditionThrowable
34         {
35             final int length = array.length;
36             if (length == 0)
37                 return signal(new WrongNumberOfArgumentsException(this));
38             if (length > 1) {
39                 final char c0 = LispCharacter.getValue(array[0]);
40                 for (int i = 0; i < length; i++) {
41                     if (c0 != LispCharacter.getValue(array[i]))
42                         return NIL;
43                 }
44             }
45             return T;
46         }
47     };
48
49     // ### char-equal
50
private static final Primitive CHAR_EQUAL = new Primitive("char-equal","&rest characters") {
51         public LispObject execute(LispObject first, LispObject second)
52             throws ConditionThrowable
53         {
54             char c1 = LispCharacter.getValue(first);
55             char c2 = LispCharacter.getValue(second);
56             if (c1 == c2)
57                 return T;
58             if (Utilities.toUpperCase(c1) == Utilities.toUpperCase(c2))
59                 return T;
60             if (Utilities.toLowerCase(c1) == Utilities.toLowerCase(c2))
61                 return T;
62             return NIL;
63         }
64         public LispObject execute(LispObject[] array) throws ConditionThrowable
65         {
66             final int length = array.length;
67             if (length == 0)
68                 return signal(new WrongNumberOfArgumentsException(this));
69             if (length > 1) {
70                 final char c0 = LispCharacter.getValue(array[0]);
71                 for (int i = 1; i < length; i++) {
72                     char c = LispCharacter.getValue(array[i]);
73                     if (c0 == c)
74                         continue;
75                     if (Utilities.toUpperCase(c0) == Utilities.toUpperCase(c))
76                         continue;
77                     if (Utilities.toLowerCase(c0) == Utilities.toLowerCase(c))
78                         continue;
79                     return NIL;
80                 }
81             }
82             return T;
83         }
84     };
85
86     // ### char-greaterp
87
private static final Primitive CHAR_GREATERP =
88         new Primitive("char-greaterp","&rest characters") {
89         public LispObject execute(LispObject first, LispObject second)
90             throws ConditionThrowable
91         {
92             char c1 = Utilities.toUpperCase(LispCharacter.getValue(first));
93             char c2 = Utilities.toUpperCase(LispCharacter.getValue(second));
94             return c1 > c2 ? T : NIL;
95         }
96         public LispObject execute(LispObject[] array) throws ConditionThrowable
97         {
98             final int length = array.length;
99             if (length == 0)
100                 return signal(new WrongNumberOfArgumentsException(this));
101             if (length > 1) {
102                 char[] chars = new char[length];
103                 for (int i = 0; i < length; i++)
104                     chars[i] = Utilities.toUpperCase(LispCharacter.getValue(array[i]));
105                 for (int i = 1; i < length; i++) {
106                     if (chars[i-1] <= chars[i])
107                         return NIL;
108                 }
109             }
110             return T;
111         }
112     };
113
114     // ### char-not-greaterp
115
private static final Primitive CHAR_NOT_GREATERP =
116         new Primitive("char-not-greaterp","&rest characters") {
117         public LispObject execute(LispObject first, LispObject second)
118             throws ConditionThrowable
119         {
120             char c1 = Utilities.toUpperCase(LispCharacter.getValue(first));
121             char c2 = Utilities.toUpperCase(LispCharacter.getValue(second));
122             return c1 <= c2 ? T : NIL;
123         }
124         public LispObject execute(LispObject[] array) throws ConditionThrowable
125         {
126             final int length = array.length;
127             if (length == 0)
128                 return signal(new WrongNumberOfArgumentsException(this));
129             if (length > 1) {
130                 char[] chars = new char[length];
131                 for (int i = 0; i < length; i++)
132                     chars[i] = Utilities.toUpperCase(LispCharacter.getValue(array[i]));
133                 for (int i = 1; i < length; i++) {
134                     if (chars[i] < chars[i-1])
135                         return NIL;
136                 }
137             }
138             return T;
139         }
140     };
141
142     // ### char<=
143
private static final Primitive CHAR_LE =
144         new Primitive("char<=", "&rest characters")
145     {
146         public LispObject execute() throws ConditionThrowable
147         {
148             return signal(new WrongNumberOfArgumentsException(this));
149         }
150
151         public LispObject execute(LispObject arg) throws ConditionThrowable
152         {
153             if (arg instanceof LispCharacter)
154                 return T;
155             return signal(new TypeError(arg, Symbol.CHARACTER));
156         }
157
158         public LispObject execute(LispObject first, LispObject second)
159             throws ConditionThrowable
160         {
161             try {
162                 return ((LispCharacter)first).value <= ((LispCharacter)second).value ? T : NIL;
163             }
164             catch (ClassCastException JavaDoc e) {
165                 LispObject datum;
166                 if (first instanceof LispCharacter)
167                     datum = second;
168                 else
169                     datum = first;
170                 return signal(new TypeError(datum, Symbol.CHARACTER));
171             }
172         }
173
174         public LispObject execute(LispObject[] args) throws ConditionThrowable
175         {
176             final int length = args.length;
177             char[] chars = new char[length];
178             for (int i = 0; i < length; i++) {
179                 try {
180                     chars[i] = ((LispCharacter)args[i]).value;
181                 }
182                 catch (ClassCastException JavaDoc e) {
183                     return signal(new TypeError(args[i], Symbol.CHARACTER));
184                 }
185             }
186             for (int i = 1; i < length; i++) {
187                 if (chars[i-1] > chars[i])
188                     return NIL;
189             }
190             return T;
191         }
192     };
193
194     // ### char-lessp
195
private static final Primitive CHAR_LESSP =
196         new Primitive("char-lessp","&rest characters") {
197         public LispObject execute(LispObject first, LispObject second)
198             throws ConditionThrowable
199         {
200             char c1 = Utilities.toUpperCase(LispCharacter.getValue(first));
201             char c2 = Utilities.toUpperCase(LispCharacter.getValue(second));
202             return c1 < c2 ? T : NIL;
203         }
204         public LispObject execute(LispObject[] array) throws ConditionThrowable
205         {
206             final int length = array.length;
207             if (length == 0)
208                 return signal(new WrongNumberOfArgumentsException(this));
209             if (length > 1) {
210                 char[] chars = new char[length];
211                 for (int i = 0; i < length; i++)
212                     chars[i] = Utilities.toUpperCase(LispCharacter.getValue(array[i]));
213                 for (int i = 1; i < length; i++) {
214                     if (chars[i-1] >= chars[i])
215                         return NIL;
216                 }
217             }
218             return T;
219         }
220     };
221
222     // ### char-not-lessp
223
private static final Primitive CHAR_NOT_LESSP =
224         new Primitive("char-not-lessp","&rest characters") {
225         public LispObject execute(LispObject first, LispObject second)
226             throws ConditionThrowable
227         {
228             char c1 = Utilities.toUpperCase(LispCharacter.getValue(first));
229             char c2 = Utilities.toUpperCase(LispCharacter.getValue(second));
230             return c1 >= c2 ? T : NIL;
231         }
232         public LispObject execute(LispObject[] array) throws ConditionThrowable
233         {
234             final int length = array.length;
235             if (length == 0)
236                 return signal(new WrongNumberOfArgumentsException(this));
237             if (length > 1) {
238                 char[] chars = new char[length];
239                 for (int i = 0; i < length; i++)
240                     chars[i] = Utilities.toUpperCase(LispCharacter.getValue(array[i]));
241                 for (int i = 1; i < length; i++) {
242                     if (chars[i] > chars[i-1])
243                         return NIL;
244                 }
245             }
246             return T;
247         }
248     };
249 }
250
Popular Tags