--- /dev/null
+// © 2017 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html#License
+package com.ibm.icu.impl;
+
+import java.text.CharacterIterator;
+
+/**
+ * Implement the Java CharacterIterator interface on a CharSequence.
+ * Intended for internal use by ICU only.
+ */
+public class CSCharacterIterator implements CharacterIterator {
+
+ private int index;
+ private CharSequence seq;
+
+
+ /**
+ * Constructor.
+ * @param text The CharSequence to iterate over.
+ */
+ public CSCharacterIterator(CharSequence text) {
+ if (text == null) {
+ throw new NullPointerException();
+ }
+ seq = text;
+ index = 0;
+ }
+
+ /** @{inheritDoc} */
+ @Override
+ public char first() {
+ index = 0;
+ return current();
+ }
+
+ /** @{inheritDoc} */
+ @Override
+ public char last() {
+ index = seq.length();
+ return previous();
+ }
+
+ /** @{inheritDoc} */
+ @Override
+ public char current() {
+ if (index == seq.length()) {
+ return DONE;
+ }
+ return seq.charAt(index);
+ }
+
+ /** @{inheritDoc} */
+ @Override
+ public char next() {
+ if (index < seq.length()) {
+ ++index;
+ }
+ return current();
+ }
+
+ /** @{inheritDoc} */
+ @Override
+ public char previous() {
+ if (index == 0) {
+ return DONE;
+ }
+ --index;
+ return current();
+ }
+
+ /** @{inheritDoc} */
+ @Override
+ public char setIndex(int position) {
+ if (position < 0 || position > seq.length()) {
+ throw new IllegalArgumentException();
+ }
+ index = position;
+ return current();
+ }
+
+ /** @{inheritDoc} */
+ @Override
+ public int getBeginIndex() {
+ return 0;
+ }
+
+ /** @{inheritDoc} */
+ @Override
+ public int getEndIndex() {
+ return seq.length();
+ }
+
+ /** @{inheritDoc} */
+ @Override
+ public int getIndex() {
+ return index;
+ }
+
+ /** @{inheritDoc} */
+ @Override
+ public Object clone() {
+ CSCharacterIterator copy = new CSCharacterIterator(seq);
+ copy.setIndex(index);
+ return copy;
+ }
+}
import java.util.Locale;
import java.util.MissingResourceException;
+import com.ibm.icu.impl.CSCharacterIterator;
import com.ibm.icu.impl.CacheValue;
import com.ibm.icu.impl.ICUDebug;
import com.ibm.icu.util.ICUCloneNotSupportedException;
setText(new StringCharacterIterator(newText));
}
+ /**
+ * Sets the iterator to analyze a new piece of text. The new
+ * piece of text is passed in as a CharSequence, and the current
+ * iteration position is reset to the beginning of the text.
+ * (The old text is dropped.)
+ * @param newText A CharSequence containing the text to analyze with
+ * this BreakIterator.
+ * @draft ICU 60
+ */
+ public void setText(CharSequence newText) {
+ setText(new CSCharacterIterator(newText));
+ }
+
/**
* Sets the iterator to analyze a new piece of text. The
* BreakIterator is passed a CharacterIterator through which
--- /dev/null
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html#License
+
+package com.ibm.icu.dev.test.impl;
+
+import java.text.CharacterIterator;
+import java.text.StringCharacterIterator;
+
+import org.junit.Test;
+
+import com.ibm.icu.dev.test.TestFmwk;
+import com.ibm.icu.impl.CSCharacterIterator;
+
+public class CSCharacterIteratorTest extends TestFmwk {
+ public CSCharacterIteratorTest() {};
+
+ @Test
+ public void TestAPI() {
+ String text = "Hello, World";
+
+ CharSequence cs = text;
+ CharacterIterator csci = new CSCharacterIterator(cs);
+ CharacterIterator sci = new StringCharacterIterator(text);
+
+ assertEquals("", sci.setIndex(6), csci.setIndex(6));
+ assertEquals("", sci.getIndex(), csci.getIndex());
+ assertEquals("", sci.current(), csci.current());
+ assertEquals("", sci.previous(), csci.previous());
+ assertEquals("", sci.next(), csci.next());
+ assertEquals("", sci.getBeginIndex(), csci.getBeginIndex());
+ assertEquals("", sci.getEndIndex(), csci.getEndIndex());
+ assertEquals("", sci.first(), csci.first());
+ assertEquals("", sci.last(), csci.last());
+
+ csci.setIndex(4);
+ sci.setIndex(4);
+ CharacterIterator clci = (CharacterIterator)csci.clone();
+ for (int i=0; i<50; ++i) {
+ assertEquals("", sci.next(), clci.next());
+ }
+ for (int i=0; i<50; ++i) {
+ assertEquals("", sci.previous(), clci.previous());
+ }
+ }
+}
import java.io.PrintStream;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Locale;
import org.junit.Test;
if (wordIter1.current() != 0)
errln("ERROR:2 setText did not reset the iteration position to the beginning of the text, it is"
+ wordIter1.current() + "\n");
- //ICU4J has remove the method adoptText
- /*
- charIter1.adoptText(text1Clone);
- if (wordIter1.getText() == charIter1.getText()
- || wordIter1.getText() != text2
- || charIter1.getText() != text1)
- errln((UnicodeString) "ERROR:2 error is getText or setText()");
- RuleBasedBreakIterator rb = (RuleBasedBreakIterator) wordIter1.clone();
- rb.adoptText(text1);
- if (rb.getText() != text1)
- errln((UnicodeString) "ERROR:1 error in adoptText ");
- rb.adoptText(text2);
- if (rb.getText() != text2)
- errln((UnicodeString) "ERROR:2 error in adoptText ");
- */
+ // Test the CharSequence overload of setText() for a simple case.
+ BreakIterator lineIter = BreakIterator.getLineInstance(Locale.ENGLISH);
+ CharSequence csText = "Hello, World. ";
+ // Expected Line Brks ^ ^ ^
+ // 0123456789012345
+ List<Integer> expected = new ArrayList<Integer>();
+ expected.add(0); expected.add(7); expected.add(14);
+ lineIter.setText(csText);
+ for (int pos = lineIter.first(); pos != BreakIterator.DONE; pos = lineIter.next()) {
+ assertTrue("", expected.contains(pos));
+ }
+ assertEquals("", csText.length(), lineIter.current());
}
/**
}
}
- /*Internal subroutine used for comparision of expected and acquired results */
+ /*Internal subroutine used for comparison of expected and acquired results */
private void doTest(String testString, int start, int gotoffset, int expectedOffset, String expectedString) {
String selected;
String expected = expectedString;