Changeset 12497


Ignore:
Timestamp:
02/21/10 17:46:10 (14 years ago)
Author:
vvoutilainen
Message:

Consolidate string equality comparisons.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/src/org/armedbear/lisp/StringFunctions.java

    r12496 r12497  
    163163
    164164
    165     // ### %string/=
    166     // Case sensitive.
    167     private static final Primitive _STRING_NOT_EQUAL = new pf__string_not_equal();
    168     private static final class pf__string_not_equal extends Primitive {
    169         pf__string_not_equal() {
    170             super("%string/=", PACKAGE_SYS, true);
    171         }
    172 
    173         @Override
    174         public LispObject execute(LispObject string1, LispObject string2,
    175                                   LispObject start1, LispObject end1,
    176                                   LispObject start2, LispObject end2) {
    177             StringIndicesAndChars indicesAndChars =
    178                 stringIndicesAndChars(string1, string2, start1, end1,
    179                                       start2, end2);
    180             int i = indicesAndChars.start1;
    181             int j = indicesAndChars.start2;
    182             while (true) {
    183                 if (i == indicesAndChars.end1) {
    184                     // Reached end of string1.
    185                     if (j == indicesAndChars.end2)
    186                         return NIL; // Strings are identical.
    187                     return Fixnum.getInstance(i);
    188                 }
    189                 if (j == indicesAndChars.end2) {
    190                     // Reached end of string2 before end of string1.
    191                     return Fixnum.getInstance(i);
    192                 }
    193                 if (indicesAndChars.array1[i] != indicesAndChars.array2[j])
    194                     return Fixnum.getInstance(i);
    195                 ++i;
    196                 ++j;
    197             }
    198         }
    199     };
    200 
    201     // ### %string-equal
    202     // Case insensitive.
    203     private static final Primitive _STRING_EQUAL_IGNORE_CASE = new pf__string_equal_ignore_case();
    204     private static final class pf__string_equal_ignore_case extends Primitive {
    205         pf__string_equal_ignore_case() {
    206             super("%string-equal", PACKAGE_SYS, true);
    207         }
    208 
    209         @Override
    210         public LispObject execute(LispObject string1, LispObject string2,
    211                                   LispObject start1, LispObject end1,
    212                                   LispObject start2, LispObject end2)
    213 
    214         {
    215             return (_STRING_NOT_EQUAL_IGNORE_CASE.execute(string1, string2,
    216                                                           start1, end1,
    217                                                           start2, end2)
    218                     == NIL) ? T : NIL;
    219         }
    220     };
    221 
    222     // ### %string-not-equal
    223     // Case insensitive.
    224     private static final Primitive _STRING_NOT_EQUAL_IGNORE_CASE = new pf__string_not_equal_ignore_case();
    225     private static final class pf__string_not_equal_ignore_case extends Primitive {
    226         pf__string_not_equal_ignore_case() {
    227             super("%string-not-equal", PACKAGE_SYS, true);
    228         }
    229 
    230         @Override
    231         public LispObject execute(LispObject string1, LispObject string2,
    232                                   LispObject start1, LispObject end1,
    233                                   LispObject start2, LispObject end2) {
    234             StringIndicesAndChars indicesAndChars =
    235                 stringIndicesAndChars(string1, string2, start1, end1,
    236                                       start2, end2);
    237             int i = indicesAndChars.start1;
    238             int j = indicesAndChars.start2;
    239             while (true) {
    240                 if (i == indicesAndChars.end1) {
    241                     // Reached end of string1.
    242                     if (j == indicesAndChars.end2)
    243                         return NIL; // Strings are identical.
    244                     return Fixnum.getInstance(i);
    245                 }
    246                 if (j == indicesAndChars.end2) {
    247                     // Reached end of string2.
    248                     return Fixnum.getInstance(i);
    249                 }
    250                 char c1 = indicesAndChars.array1[i];
    251                 char c2 = indicesAndChars.array2[j];
    252                 if (c1 == c2 ||
    253                         LispCharacter.toUpperCase(c1) == LispCharacter.toUpperCase(c2) ||
    254                         LispCharacter.toLowerCase(c1) == LispCharacter.toLowerCase(c2)) {
    255                     ++i;
    256                     ++j;
    257                     continue;
    258                 }
    259                 return Fixnum.getInstance(i);
    260             }
    261         }
    262     };
    263 
    264     private static int lessThan(StringIndicesAndChars indicesAndChars) {
     165    private static final int notEqual(StringIndicesAndChars indicesAndChars) {
    265166        int i = indicesAndChars.start1;
    266167        int j = indicesAndChars.start2;
     
    273174            }
    274175            if (j == indicesAndChars.end2) {
     176                // Reached end of string2 before end of string1.
     177                return i;
     178            }
     179            if (upcaseIfNeeded(indicesAndChars.array1[i],
     180                               indicesAndChars.convertCase)
     181                != upcaseIfNeeded(indicesAndChars.array2[j],
     182                                  indicesAndChars.convertCase))
     183                return i;
     184            ++i;
     185            ++j;
     186        }
     187    }
     188    // ### %string/=
     189    // Case sensitive.
     190    private static final Primitive _STRING_NOT_EQUAL = new pf__string_not_equal();
     191    private static final class pf__string_not_equal extends Primitive {
     192        pf__string_not_equal() {
     193            super("%string/=", PACKAGE_SYS, true);
     194        }
     195
     196        @Override
     197        public LispObject execute(LispObject string1, LispObject string2,
     198                                  LispObject start1, LispObject end1,
     199                                  LispObject start2, LispObject end2) {
     200            StringIndicesAndChars indicesAndChars =
     201                stringIndicesAndChars(string1, string2, start1, end1,
     202                                      start2, end2);
     203            int tmp = notEqual(indicesAndChars);
     204            return (tmp >= 0) ? Fixnum.getInstance(tmp) : NIL;
     205        }
     206    };
     207
     208    // ### %string-equal
     209    // Case insensitive.
     210    private static final Primitive _STRING_EQUAL_IGNORE_CASE = new pf__string_equal_ignore_case();
     211    private static final class pf__string_equal_ignore_case extends Primitive {
     212        pf__string_equal_ignore_case() {
     213            super("%string-equal", PACKAGE_SYS, true);
     214        }
     215
     216        @Override
     217        public LispObject execute(LispObject string1, LispObject string2,
     218                                  LispObject start1, LispObject end1,
     219                                  LispObject start2, LispObject end2)
     220
     221        {
     222            return (_STRING_NOT_EQUAL_IGNORE_CASE.execute(string1, string2,
     223                                                          start1, end1,
     224                                                          start2, end2)
     225                    == NIL) ? T : NIL;
     226        }
     227    };
     228
     229    // ### %string-not-equal
     230    // Case insensitive.
     231    private static final Primitive _STRING_NOT_EQUAL_IGNORE_CASE = new pf__string_not_equal_ignore_case();
     232    private static final class pf__string_not_equal_ignore_case extends Primitive {
     233        pf__string_not_equal_ignore_case() {
     234            super("%string-not-equal", PACKAGE_SYS, true);
     235        }
     236
     237        @Override
     238        public LispObject execute(LispObject string1, LispObject string2,
     239                                  LispObject start1, LispObject end1,
     240                                  LispObject start2, LispObject end2) {
     241            StringIndicesAndChars indicesAndChars =
     242                stringIndicesAndChars(string1, string2, start1, end1,
     243                                      start2, end2);
     244            indicesAndChars.convertCase = true;
     245            int tmp = notEqual(indicesAndChars);
     246            return (tmp >= 0) ? Fixnum.getInstance(tmp) : NIL;
     247        }
     248    };
     249
     250    private static final int lessThan(StringIndicesAndChars indicesAndChars) {
     251        int i = indicesAndChars.start1;
     252        int j = indicesAndChars.start2;
     253        while (true) {
     254            if (i == indicesAndChars.end1) {
     255                // Reached end of string1.
     256                if (j == indicesAndChars.end2)
     257                    return -1; // Strings are identical.
     258                return i;
     259            }
     260            if (j == indicesAndChars.end2) {
    275261                // Reached end of string2.
    276262                return -1;
     
    345331    };
    346332
    347     private static int lessThanOrEqual(StringIndicesAndChars indicesAndChars) {
     333    private static final int lessThanOrEqual(StringIndicesAndChars indicesAndChars) {
    348334        int i = indicesAndChars.start1;
    349335        int j = indicesAndChars.start2;
Note: See TracChangeset for help on using the changeset viewer.