/// <summary> /// Sets an existing iterator to point to a new piece of text. /// </summary> /// <param name="text">New text</param> public override void SetText(string text) { if (text == null) { throw new ArgumentNullException("text"); } _text = text; if (string.IsNullOrEmpty(Text)) { _textBoundaries = new TextBoundary[0]; _currentIndex = 0; return; } if (_breakIterator == IntPtr.Zero) { InitializeBreakIterator(); } else { ErrorCode err; NativeMethods.ubrk_setText(_breakIterator, Text, Text.Length, out err); if (err.IsFailure()) { throw new Exception("BreakIterator.ubrk_setText() failed with code " + err); } } List <TextBoundary> textBoundaries = new List <TextBoundary>(); // Start at the the beginning of the text and iterate until all // of the boundaries are consumed. int cur = NativeMethods.ubrk_first(_breakIterator); TextBoundary textBoundary; if (!TryGetTextBoundaryFromOffset(cur, out textBoundary)) { return; } textBoundaries.Add(textBoundary); while (cur != DONE) { int next = NativeMethods.ubrk_next(_breakIterator); if (!TryGetTextBoundaryFromOffset(next, out textBoundary)) { break; } textBoundaries.Add(textBoundary); cur = next; } _textBoundaries = textBoundaries.ToArray(); _currentIndex = 0; }