/// <summary> /// Checks if the offset is valid, gets the RuleStatus and /// RuleStatusVector for the offset and returns that TextBoundary. /// </summary> /// <param name="offset">Offset to check in text.</param> /// <param name="textBoundary">TextBoundary for the given offset.</param> /// <returns>true if the offset is valid; false otherwise.</returns> private bool TryGetTextBoundaryFromOffset(int offset, out TextBoundary textBoundary) { textBoundary = default(TextBoundary); if (offset == DONE) { return(false); } const int length = 128; int[] vector = new int[length]; ErrorCode errorCode; int actualLen = NativeMethods.ubrk_getRuleStatusVec(_breakIterator, vector, length, out errorCode); if (errorCode.IsFailure()) { throw new Exception("BreakIterator.GetRuleStatusVector failed! " + errorCode); } if (actualLen > length) { vector = new int[actualLen]; NativeMethods.ubrk_getRuleStatusVec(_breakIterator, vector, vector.Length, out errorCode); if (errorCode.IsFailure()) { throw new Exception("BreakIterator.GetRuleStatusVector failed! " + errorCode); } } int[] ruleStatuses; // Constrain the size of the array to actual number of elements // that were returned. if (actualLen < vector.Length) { ruleStatuses = new int[actualLen]; Array.Copy(vector, ruleStatuses, actualLen); } else { ruleStatuses = vector; } textBoundary = new TextBoundary(offset, ruleStatuses); return(true); }