SetText() public method

Set a new region of text to be examined by this iterator
public SetText ( char array, int start, int length ) : void
array char text buffer to examine
start int offset into buffer
length int maximum length to examine
return void
コード例 #1
0
 public override void Reset()
 {
     base.Reset();
     wrapper.SetText(m_buffer, 0, 0);
     iterator.SetText(new string(wrapper.Text, wrapper.Start, wrapper.Length));
     length = usableLength = m_offset = 0;
 }
コード例 #2
0
 public override void Reset()
 {
     base.Reset();
     wrapper.SetText(buffer, 0, 0);
     iterator.SetText(new string(buffer, 0, 0));
     length = usableLength = offset = 0;
 }
コード例 #3
0
        /// <summary>
        /// Refill the buffer, accumulating the offset and setting usableLength to the
        /// last unambiguous break position
        /// </summary>
        private void Refill()
        {
            offset += usableLength;
            int leftover = length - usableLength;

            Array.Copy(buffer, usableLength, buffer, 0, leftover);
            int requested = buffer.Length - leftover;
            int returned  = read(input, buffer, leftover, requested);

            length = returned < 0 ? leftover : returned + leftover;
            if (returned < requested)     // reader has been emptied, process the rest
            {
                usableLength = length;
            }
            else     // still more data to be read, find a safe-stopping place
            {
                usableLength = FindSafeEnd();
                if (usableLength < 0)
                {
                    usableLength = length;     /*
                                                * }
                                                * more than IOBUFFER of text without breaks,
                                                * gonna possibly truncate tokens
                                                */
                }

                wrapper.SetText(buffer, 0, Math.Max(0, usableLength));
                iterator.Text = wrapper;
            }
        }
コード例 #4
0
        /* run this to test if your JRE is buggy
         * public void testSentenceInstanceJREBUG() {
         * // we use the default locale, as its randomized by LuceneTestCase
         * BreakIterator bi = BreakIterator.getSentenceInstance(Locale.getDefault());
         * Segment ci = new Segment();
         * for (int i = 0; i < 10000; i++) {
         *  char text[] = TestUtil.randomUnicodeString(random).toCharArray();
         *  ci.array = text;
         *  ci.offset = 0;
         *  ci.count = text.length;
         *  consume(bi, ci);
         * }
         * }
         */

        private void DoTests(CharArrayIterator ci)
        {
            // basics
            ci.SetText("testing".ToCharArray(), 0, "testing".Length);
            assertEquals(0, ci.BeginIndex);
            assertEquals(7, ci.EndIndex);
            assertEquals(0, ci.Index);
            assertEquals('t', ci.Current);
            assertEquals('e', ci.Next());
            assertEquals('g', ci.Last());
            assertEquals('n', ci.Previous());
            assertEquals('t', ci.First());
            assertEquals(CharacterIterator.Done, ci.Previous());

            // first()
            ci.SetText("testing".ToCharArray(), 0, "testing".Length);
            ci.Next();
            // Sets the position to getBeginIndex() and returns the character at that position.
            assertEquals('t', ci.First());
            assertEquals(ci.BeginIndex, ci.Index);
            // or DONE if the text is empty
            ci.SetText(new char[] { }, 0, 0);
            assertEquals(CharacterIterator.Done, ci.First());

            // last()
            ci.SetText("testing".ToCharArray(), 0, "testing".Length);
            // Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
            // and returns the character at that position.
            assertEquals('g', ci.Last());
            assertEquals(ci.Index, ci.EndIndex - 1);
            // or DONE if the text is empty
            ci.SetText(new char[] { }, 0, 0);
            assertEquals(CharacterIterator.Done, ci.Last());
            assertEquals(ci.EndIndex, ci.Index);

            // current()
            // Gets the character at the current position (as returned by getIndex()).
            ci.SetText("testing".ToCharArray(), 0, "testing".Length);
            assertEquals('t', ci.Current);
            ci.Last();
            ci.Next();
            // or DONE if the current position is off the end of the text.
            assertEquals(CharacterIterator.Done, ci.Current);

            // next()
            ci.SetText("te".ToCharArray(), 0, 2);
            // Increments the iterator's index by one and returns the character at the new index.
            assertEquals('e', ci.Next());
            assertEquals(1, ci.Index);
            // or DONE if the new position is off the end of the text range.
            assertEquals(CharacterIterator.Done, ci.Next());
            assertEquals(ci.EndIndex, ci.Index);

            // setIndex()
            ci.SetText("test".ToCharArray(), 0, "test".Length);
            try
            {
                ci.SetIndex(5);
                fail();
            }
            catch (Exception e)
            {
                assertTrue(e is System.ArgumentException);
            }

            // clone()
            var text = "testing".ToCharArray();

            ci.SetText(text, 0, text.Length);
            ci.Next();
            var ci2 = ci.Clone() as CharArrayIterator;

            assertEquals(ci.Index, ci2.Index);
            assertEquals(ci.Next(), ci2.Next());
            assertEquals(ci.Last(), ci2.Last());
        }
コード例 #5
0
        /* run this to test if your JRE is buggy
        public void testSentenceInstanceJREBUG() {
          // we use the default locale, as its randomized by LuceneTestCase
          BreakIterator bi = BreakIterator.getSentenceInstance(Locale.getDefault());
          Segment ci = new Segment();
          for (int i = 0; i < 10000; i++) {
            char text[] = TestUtil.randomUnicodeString(random).toCharArray();
            ci.array = text;
            ci.offset = 0;
            ci.count = text.length;
            consume(bi, ci);
          }
        }
        */
        private void DoTests(CharArrayIterator ci)
        {
            // basics
            ci.SetText("testing".ToCharArray(), 0, "testing".Length);
            assertEquals(0, ci.BeginIndex);
            assertEquals(7, ci.EndIndex);
            assertEquals(0, ci.Index);
            assertEquals('t', ci.Current());
            assertEquals('e', ci.Next());
            assertEquals('g', ci.Last());
            assertEquals('n', ci.Previous());
            assertEquals('t', ci.First());
            assertEquals(CharacterIterator.DONE, ci.Previous());

            // first()
            ci.SetText("testing".ToCharArray(), 0, "testing".Length);
            ci.Next();
            // Sets the position to getBeginIndex() and returns the character at that position.
            assertEquals('t', ci.First());
            assertEquals(ci.BeginIndex, ci.Index);
            // or DONE if the text is empty
            ci.SetText(new char[] { }, 0, 0);
            assertEquals(CharacterIterator.DONE, ci.First());

            // last()
            ci.SetText("testing".ToCharArray(), 0, "testing".Length);
            // Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
            // and returns the character at that position.
            assertEquals('g', ci.Last());
            assertEquals(ci.Index, ci.EndIndex - 1);
            // or DONE if the text is empty
            ci.SetText(new char[] { }, 0, 0);
            assertEquals(CharacterIterator.DONE, ci.Last());
            assertEquals(ci.EndIndex, ci.Index);

            // current()
            // Gets the character at the current position (as returned by getIndex()).
            ci.SetText("testing".ToCharArray(), 0, "testing".Length);
            assertEquals('t', ci.Current());
            ci.Last();
            ci.Next();
            // or DONE if the current position is off the end of the text.
            assertEquals(CharacterIterator.DONE, ci.Current());

            // next()
            ci.SetText("te".ToCharArray(), 0, 2);
            // Increments the iterator's index by one and returns the character at the new index.
            assertEquals('e', ci.Next());
            assertEquals(1, ci.Index);
            // or DONE if the new position is off the end of the text range.
            assertEquals(CharacterIterator.DONE, ci.Next());
            assertEquals(ci.EndIndex, ci.Index);

            // setIndex()
            ci.SetText("test".ToCharArray(), 0, "test".Length);
            try
            {
                ci.SetIndex(5);
                fail();
            }
            catch (Exception e)
            {
                assertTrue(e is System.ArgumentException);
            }

            // clone()
            var text = "testing".ToCharArray();
            ci.SetText(text, 0, text.Length);
            ci.Next();
            var ci2 = ci.Clone() as CharArrayIterator;
            assertEquals(ci.Index, ci2.Index);
            assertEquals(ci.Next(), ci2.Next());
            assertEquals(ci.Last(), ci2.Last());
        }