From a14d79366f97ffde61b56aee2e2d9123ccadc8a7 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Thu, 19 Jan 2017 11:27:24 +0100 Subject: [PATCH] LUCENE-7645: Use JDK's Arrays.binarySearch in BaseCharFilter. --- .../analysis/charfilter/BaseCharFilter.java | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/charfilter/BaseCharFilter.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/charfilter/BaseCharFilter.java index 48ffa48eefd..4fba9fe5b30 100644 --- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/charfilter/BaseCharFilter.java +++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/charfilter/BaseCharFilter.java @@ -41,31 +41,17 @@ public abstract class BaseCharFilter extends CharFilter { /** Retrieve the corrected offset. */ @Override protected int correct(int currentOff) { - if (offsets == null || currentOff < offsets[0]) { + if (offsets == null) { return currentOff; } - - int hi = size - 1; - if(currentOff >= offsets[hi]) - return currentOff + diffs[hi]; - int lo = 0; - int mid = -1; - - while (hi >= lo) { - mid = (lo + hi) >>> 1; - if (currentOff < offsets[mid]) - hi = mid - 1; - else if (currentOff > offsets[mid]) - lo = mid + 1; - else - return currentOff + diffs[mid]; + int index = Arrays.binarySearch(offsets, 0, size, currentOff); + if (index < -1) { + index = -2 - index; } - if (currentOff < offsets[mid]) - return mid == 0 ? currentOff : currentOff + diffs[mid-1]; - else - return currentOff + diffs[mid]; + final int diff = index < 0 ? 0 : diffs[index]; + return currentOff + diff; } protected int getLastCumulativeDiff() {