]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/engine/Index.java
Try to find an exact, non-normalized match.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / Index.java
index fd69bb857e4bd53c08152211c91bccf4115e3d10..0c07472dabf207f82c1e206785d2a5471d67ce84 100644 (file)
@@ -136,10 +136,11 @@ public final class Index {
                 stoplist.add(raf.readUTF());
             }
         } else if (dict.dictFileVersion >= 4) {
-            raf.readInt(); // length
-            raf.skipBytes(52);
             stoplist = new HashSet<>();
-            byte b;
+            raf.readInt(); // length
+            raf.skipBytes(18);
+            byte b = raf.readByte();
+            raf.skipBytes(b == 'L' ? 71 : 33);
             while ((b = raf.readByte()) == 0x74) {
                 stoplist.add(raf.readUTF());
             }
@@ -311,6 +312,7 @@ public final class Index {
     }
 
     private int findInsertionPointIndex(String token, final AtomicBoolean interrupted) {
+        String orig_token = token;
         token = normalizeToken(token);
 
         int start = 0;
@@ -328,7 +330,8 @@ public final class Index {
             if (comp == 0)
                 comp = sortCollator.compare(token, midEntry.normalizedToken());
             if (comp == 0) {
-                return windBackCase(token, mid, interrupted);
+                start = end = mid;
+                break;
             } else if (comp < 0) {
                 // System.out.println("Upper bound: " + midEntry + ", norm=" +
                 // midEntry.normalizedToken() + ", mid=" + mid);
@@ -365,6 +368,23 @@ public final class Index {
                 start--;
         }
 
+        // If the search term was normalized, try to find an exact match first
+        if (!orig_token.equalsIgnoreCase(token)) {
+            int matchLen = findMatchLen(sortCollator, token, sortedIndexEntries.get(start).normalizedToken());
+            int scan = start;
+            while (scan >= 0 && scan < sortedIndexEntries.size()) {
+                IndexEntry e = sortedIndexEntries.get(scan);
+                if (e.token.equalsIgnoreCase(orig_token))
+                {
+                    return scan;
+                }
+                if (matchLen > findMatchLen(sortCollator, token, e.normalizedToken()))
+                    break;
+                if (interrupted.get()) return start;
+                scan++;
+            }
+        }
+
         // If we search for a substring of a string that's in there, return
         // that.
         int result = Math.min(start, sortedIndexEntries.size() - 1);