001: /*
002: * CharacterFunctions.java
003: *
004: * Copyright (C) 2003 Peter Graves
005: * $Id: CharacterFunctions.java,v 1.6 2003/11/15 11:03:30 beedlem Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: public final class CharacterFunctions extends Lisp {
025: // ### char=
026: private static final Primitive CHAR_EQUALS = new Primitive("char=") {
027: public LispObject execute(LispObject first, LispObject second)
028: throws ConditionThrowable {
029: return LispCharacter.getValue(first) == LispCharacter
030: .getValue(second) ? T : NIL;
031: }
032:
033: public LispObject execute(LispObject[] array)
034: throws ConditionThrowable {
035: final int length = array.length;
036: if (length == 0)
037: throw new ConditionThrowable(
038: new WrongNumberOfArgumentsException(this ));
039: if (length > 1) {
040: final char c0 = LispCharacter.getValue(array[0]);
041: for (int i = 0; i < length; i++) {
042: if (c0 != LispCharacter.getValue(array[i]))
043: return NIL;
044: }
045: }
046: return T;
047: }
048: };
049:
050: // ### char-equal
051: private static final Primitive CHAR_EQUAL = new Primitive(
052: "char-equal") {
053: public LispObject execute(LispObject first, LispObject second)
054: throws ConditionThrowable {
055: char c1 = LispCharacter.getValue(first);
056: char c2 = LispCharacter.getValue(second);
057: if (c1 == c2)
058: return T;
059: if (Utilities.toUpperCase(c1) == Utilities.toUpperCase(c2))
060: return T;
061: if (Utilities.toLowerCase(c1) == Utilities.toLowerCase(c2))
062: return T;
063: return NIL;
064: }
065:
066: public LispObject execute(LispObject[] array)
067: throws ConditionThrowable {
068: final int length = array.length;
069: if (length == 0)
070: throw new ConditionThrowable(
071: new WrongNumberOfArgumentsException(this ));
072: if (length > 1) {
073: final char c0 = LispCharacter.getValue(array[0]);
074: for (int i = 1; i < length; i++) {
075: char c = LispCharacter.getValue(array[i]);
076: if (c0 == c)
077: continue;
078: if (Utilities.toUpperCase(c0) == Utilities
079: .toUpperCase(c))
080: continue;
081: if (Utilities.toLowerCase(c0) == Utilities
082: .toLowerCase(c))
083: continue;
084: return NIL;
085: }
086: }
087: return T;
088: }
089: };
090:
091: // ### char-greaterp
092: private static final Primitive CHAR_GREATERP = new Primitive(
093: "char-greaterp") {
094: public LispObject execute(LispObject first, LispObject second)
095: throws ConditionThrowable {
096: char c1 = Utilities.toUpperCase(LispCharacter
097: .getValue(first));
098: char c2 = Utilities.toUpperCase(LispCharacter
099: .getValue(second));
100: return c1 > c2 ? T : NIL;
101: }
102:
103: public LispObject execute(LispObject[] array)
104: throws ConditionThrowable {
105: final int length = array.length;
106: if (length == 0)
107: throw new ConditionThrowable(
108: new WrongNumberOfArgumentsException(this ));
109: if (length > 1) {
110: char[] chars = new char[length];
111: for (int i = 0; i < length; i++)
112: chars[i] = Utilities.toUpperCase(LispCharacter
113: .getValue(array[i]));
114: for (int i = 1; i < length; i++) {
115: if (chars[i - 1] <= chars[i])
116: return NIL;
117: }
118: }
119: return T;
120: }
121: };
122:
123: // ### char-not-greaterp
124: private static final Primitive CHAR_NOT_GREATERP = new Primitive(
125: "char-not-greaterp") {
126: public LispObject execute(LispObject first, LispObject second)
127: throws ConditionThrowable {
128: char c1 = Utilities.toUpperCase(LispCharacter
129: .getValue(first));
130: char c2 = Utilities.toUpperCase(LispCharacter
131: .getValue(second));
132: return c1 <= c2 ? T : NIL;
133: }
134:
135: public LispObject execute(LispObject[] array)
136: throws ConditionThrowable {
137: final int length = array.length;
138: if (length == 0)
139: throw new ConditionThrowable(
140: new WrongNumberOfArgumentsException(this ));
141: if (length > 1) {
142: char[] chars = new char[length];
143: for (int i = 0; i < length; i++)
144: chars[i] = Utilities.toUpperCase(LispCharacter
145: .getValue(array[i]));
146: for (int i = 1; i < length; i++) {
147: if (chars[i] < chars[i - 1])
148: return NIL;
149: }
150: }
151: return T;
152: }
153: };
154:
155: // ### char-lessp
156: private static final Primitive CHAR_LESSP = new Primitive(
157: "char-lessp") {
158: public LispObject execute(LispObject first, LispObject second)
159: throws ConditionThrowable {
160: char c1 = Utilities.toUpperCase(LispCharacter
161: .getValue(first));
162: char c2 = Utilities.toUpperCase(LispCharacter
163: .getValue(second));
164: return c1 < c2 ? T : NIL;
165: }
166:
167: public LispObject execute(LispObject[] array)
168: throws ConditionThrowable {
169: final int length = array.length;
170: if (length == 0)
171: throw new ConditionThrowable(
172: new WrongNumberOfArgumentsException(this ));
173: if (length > 1) {
174: char[] chars = new char[length];
175: for (int i = 0; i < length; i++)
176: chars[i] = Utilities.toUpperCase(LispCharacter
177: .getValue(array[i]));
178: for (int i = 1; i < length; i++) {
179: if (chars[i - 1] >= chars[i])
180: return NIL;
181: }
182: }
183: return T;
184: }
185: };
186:
187: // ### char-not-lessp
188: private static final Primitive CHAR_NOT_LESSP = new Primitive(
189: "char-not-lessp") {
190: public LispObject execute(LispObject first, LispObject second)
191: throws ConditionThrowable {
192: char c1 = Utilities.toUpperCase(LispCharacter
193: .getValue(first));
194: char c2 = Utilities.toUpperCase(LispCharacter
195: .getValue(second));
196: return c1 >= c2 ? T : NIL;
197: }
198:
199: public LispObject execute(LispObject[] array)
200: throws ConditionThrowable {
201: final int length = array.length;
202: if (length == 0)
203: throw new ConditionThrowable(
204: new WrongNumberOfArgumentsException(this ));
205: if (length > 1) {
206: char[] chars = new char[length];
207: for (int i = 0; i < length; i++)
208: chars[i] = Utilities.toUpperCase(LispCharacter
209: .getValue(array[i]));
210: for (int i = 1; i < length; i++) {
211: if (chars[i] > chars[i - 1])
212: return NIL;
213: }
214: }
215: return T;
216: }
217: };
218: }
|