From 2351663f3aa8d5526a3c5d59277c9a751370e84b Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Wed, 9 Jan 2013 19:31:23 +0000 Subject: [PATCH] LUCENE-4671: Fix CharsRef.subSequence method git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1431019 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 2 ++ .../java/org/apache/lucene/util/CharsRef.java | 2 +- .../org/apache/lucene/util/TestCharsRef.java | 19 ++++++++++++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 770155943df..5b32d7d4ec5 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -324,6 +324,8 @@ Bug Fixes * LUCENE-4662: Add missing elided articles and prepositions to FrenchAnalyzer's DEFAULT_ARTICLES list passed to ElisionFilter. (David Leunen via Steve Rowe) + +* LUCENE-4671: Fix CharsRef.subSequence method. (Tim Smith via Robert Muir) Changes in Runtime Behavior diff --git a/lucene/core/src/java/org/apache/lucene/util/CharsRef.java b/lucene/core/src/java/org/apache/lucene/util/CharsRef.java index 60537bfdeb9..30e95439c76 100644 --- a/lucene/core/src/java/org/apache/lucene/util/CharsRef.java +++ b/lucene/core/src/java/org/apache/lucene/util/CharsRef.java @@ -218,7 +218,7 @@ public final class CharsRef implements Comparable, CharSequence, Clone if (start < 0 || end > length || start > end) { throw new IndexOutOfBoundsException(); } - return new CharsRef(chars, offset + start, offset + end); + return new CharsRef(chars, offset + start, end - start); } /** @deprecated This comparator is only a transition mechanism */ diff --git a/lucene/core/src/test/org/apache/lucene/util/TestCharsRef.java b/lucene/core/src/test/org/apache/lucene/util/TestCharsRef.java index 4237a6d0478..1997bcc4296 100644 --- a/lucene/core/src/test/org/apache/lucene/util/TestCharsRef.java +++ b/lucene/core/src/test/org/apache/lucene/util/TestCharsRef.java @@ -116,11 +116,28 @@ public class TestCharsRef extends LuceneTestCase { } // LUCENE-3590: fix off-by-one in subsequence, and fully obey interface + // LUCENE-4671: fix subSequence public void testCharSequenceSubSequence() { - CharSequence c = new CharsRef("abc"); + CharSequence sequences[] = { + new CharsRef("abc"), + new CharsRef("0abc".toCharArray(), 1, 3), + new CharsRef("abc0".toCharArray(), 0, 3), + new CharsRef("0abc0".toCharArray(), 1, 3) + }; + + for (CharSequence c : sequences) { + doTestSequence(c); + } + } + + private void doTestSequence(CharSequence c) { // slice assertEquals("a", c.subSequence(0, 1).toString()); + // mid subsequence + assertEquals("b", c.subSequence(1, 2).toString()); + // end subsequence + assertEquals("bc", c.subSequence(1, 3).toString()); // empty subsequence assertEquals("", c.subSequence(0, 0).toString());