[armedbear-cvs] r12496 - trunk/abcl/src/org/armedbear/lisp

Ville Voutilainen vvoutilainen at common-lisp.net
Sun Feb 21 17:29:42 UTC 2010


Author: vvoutilainen
Date: Sun Feb 21 12:29:39 2010
New Revision: 12496

Log:
Consolidate case-sensitive/insensitive comparisons.


Modified:
   trunk/abcl/src/org/armedbear/lisp/StringFunctions.java

Modified: trunk/abcl/src/org/armedbear/lisp/StringFunctions.java
==============================================================================
--- trunk/abcl/src/org/armedbear/lisp/StringFunctions.java	(original)
+++ trunk/abcl/src/org/armedbear/lisp/StringFunctions.java	Sun Feb 21 12:29:39 2010
@@ -38,7 +38,8 @@
 import java.util.Arrays;
 public final class StringFunctions {
     private final static class StringIndicesAndChars {
-        AbstractString string1;
+        public AbstractString string1;
+        public boolean convertCase = false;
         public char[] array1;
         public char[] array2;
         public int start1 = 0;
@@ -79,6 +80,10 @@
         }
 
     }
+    
+    private final static char upcaseIfNeeded(char c, boolean convert) {
+        return convert ? LispCharacter.toUpperCase(c) : c;
+    }
 
     private final static StringIndicesAndChars
         stringIndicesAndChars(LispObject... params) {
@@ -270,8 +275,10 @@
                 // Reached end of string2.
                 return -1;
             }
-            char c1 = indicesAndChars.array1[i];
-            char c2 = indicesAndChars.array2[j];
+            char c1 = upcaseIfNeeded(indicesAndChars.array1[i], 
+                                     indicesAndChars.convertCase);
+            char c2 = upcaseIfNeeded(indicesAndChars.array2[j],
+                                     indicesAndChars.convertCase);
             if (c1 == c2) {
                 ++i;
                 ++j;
@@ -349,8 +356,10 @@
                 // Reached end of string2.
                 return -1;
             }
-            char c1 = indicesAndChars.array1[i];
-            char c2 = indicesAndChars.array2[j];
+            char c1 = upcaseIfNeeded(indicesAndChars.array1[i], 
+                                     indicesAndChars.convertCase);
+            char c2 = upcaseIfNeeded(indicesAndChars.array2[j],
+                                     indicesAndChars.convertCase);
             if (c1 == c2) {
                 ++i;
                 ++j;
@@ -405,33 +414,7 @@
         }
     };
 
-    private static int stringLessp(StringIndicesAndChars indicesAndChars) {
-        int i = indicesAndChars.start1;
-        int j = indicesAndChars.start2;
-        while (true) {
-            if (i == indicesAndChars.end1) {
-                // Reached end of string1.
-                if (j == indicesAndChars.end2)
-                    return -1; // Strings are identical.
-                return i;
-            }
-            if (j == indicesAndChars.end2) {
-                // Reached end of string2.
-                return -1;
-            }
-            char c1 = LispCharacter.toUpperCase(indicesAndChars.array1[i]);
-            char c2 = LispCharacter.toUpperCase(indicesAndChars.array2[j]);
-            if (c1 == c2) {
-                ++i;
-                ++j;
-                continue;
-            }
-            if (c1 > c2)
-                return -1;
-            // c1 < c2
-            return i;
-        }
-    }
+
     // ### %string-lessp
     // Case insensitive.
     private static final Primitive _STRING_LESSP = new pf__string_lessp();
@@ -447,7 +430,8 @@
             StringIndicesAndChars indicesAndChars = 
                 stringIndicesAndChars(string1, string2, 
                                       start1, end1, start2, end2);
-            int retVal = stringLessp(indicesAndChars);
+            indicesAndChars.convertCase = true;
+            int retVal = lessThan(indicesAndChars);
             return (retVal >= 0) ? Fixnum.getInstance(retVal) : NIL;
         }
     };
@@ -469,39 +453,11 @@
                 stringIndicesAndChars(string2, string1,
                                       start2, end2,
                                       start1, end1);
-            int tmp = stringLessp(indicesAndChars);
+            indicesAndChars.convertCase = true;
+            int tmp = lessThan(indicesAndChars);
             return swapReturnValue(tmp, indicesAndChars);
         }
     };
-
-    private static int stringNotLessp(StringIndicesAndChars indicesAndChars) {
-        int i = indicesAndChars.start1;
-        int j = indicesAndChars.start2;
-        while (true) {
-            if (i == indicesAndChars.end1) {
-                // Reached end of string1.
-                if (j == indicesAndChars.end2)
-                    return i; // Strings are identical.
-                return -1;
-            }
-            if (j == indicesAndChars.end2) {
-                // Reached end of string2.
-                return i;
-            }
-            char c1 = LispCharacter.toUpperCase(indicesAndChars.array1[i]);
-            char c2 = LispCharacter.toUpperCase(indicesAndChars.array2[j]);
-            if (c1 == c2) {
-                ++i;
-                ++j;
-                continue;
-            }
-            if (c1 > c2)
-                return i;
-            // c1 < c2
-            return -1;
-        }
-    }
-
     // ### %string-not-lessp
     // Case insensitive.
     private static final Primitive _STRING_NOT_LESSP = new pf__string_not_lessp();
@@ -514,11 +470,14 @@
         public LispObject execute(LispObject string1, LispObject string2,
                                   LispObject start1, LispObject end1,
                                   LispObject start2, LispObject end2) {
+            // note the swap of the strings and lengths here..
             StringIndicesAndChars indicesAndChars = 
-                stringIndicesAndChars(string1, string2, 
-                                      start1, end1, start2, end2);
-            int retVal = stringNotLessp(indicesAndChars);
-            return (retVal >= 0) ? Fixnum.getInstance(retVal) : NIL;
+                stringIndicesAndChars(string2, string1,
+                                      start2, end2,
+                                      start1, end1);
+            indicesAndChars.convertCase = true;
+            int tmp = lessThanOrEqual(indicesAndChars);
+            return swapReturnValue(tmp, indicesAndChars);
         }
     };
 
@@ -534,13 +493,13 @@
         public LispObject execute(LispObject string1, LispObject string2,
                                   LispObject start1, LispObject end1,
                                   LispObject start2, LispObject end2) {
-            // note the swap of the strings and lengths here..
             StringIndicesAndChars indicesAndChars = 
-                stringIndicesAndChars(string2, string1,
-                                      start2, end2,
-                                      start1, end1);
-            int tmp = stringNotLessp(indicesAndChars);
-            return swapReturnValue(tmp, indicesAndChars);
+                stringIndicesAndChars(string1, string2,
+                                      start1, end1,
+                                      start2, end2);
+            indicesAndChars.convertCase = true;
+            int tmp = lessThanOrEqual(indicesAndChars);
+            return (tmp >= 0) ? Fixnum.getInstance(tmp) : NIL;
         }
     };
 




More information about the armedbear-cvs mailing list