Beispiel #1
0
 /// <summary>
 /// Translates the magic values (End, Beginning, Word) into actual values
 /// based on the given count.
 /// </summary>
 /// <param name="text">The text used for normalizing.</param>
 /// <param name="searchPosition">The character position to start searching for words.</param>
 /// <param name="direction">The direction to search for Word positions.</param>
 /// <returns>
 /// The normalized index.
 /// </returns>
 /// <exception cref="System.IndexOutOfRangeException">Encountered an invalid index:  + Index</exception>
 public int GetCharacterIndex(
     string text,
     CharacterPosition searchPosition,
     WordSearchDirection direction)
 {
     return(GetCharacterIndex(
                text, searchPosition, direction, DefaultWordTokenizer));
 }
Beispiel #2
0
        /// <summary>
        /// Translates the magic values (End, Beginning, Word) into actual values
        /// based on the given count.
        /// </summary>
        /// <param name="text">The text used for normalizing.</param>
        /// <param name="searchPosition">The character position to start searching for words.</param>
        /// <param name="direction">The direction to search for Word positions.</param>
        /// <param name="wordTokenizer">The word tokenizer to use for word positions.</param>
        /// <returns>
        /// The normalized index.
        /// </returns>
        /// <exception cref="System.IndexOutOfRangeException">Encountered an invalid index:  + Index</exception>
        public int GetCharacterIndex(
            string text,
            CharacterPosition searchPosition,
            WordSearchDirection direction,
            IWordTokenizer wordTokenizer)
        {
            // Make sure we have a sane state before we start.
            if (wordTokenizer == null)
            {
                throw new ArgumentNullException("wordTokenizer");
            }

            // All the magic values are negative, so if we don't have one, there is
            // nothing to do.
            if (Index >= 0)
            {
                return(Index);
            }

            // If we have the end magic number, then the index is equal to the end
            // of the text line.
            if (Index == EndIndex)
            {
                return(text.Length);
            }

            // If we have the word, then we go either left or right.
            if (Index == WordIndex)
            {
                // Perform some additional checks on the range and index.
                if (searchPosition.Index == 0 &&
                    direction == WordSearchDirection.Left)
                {
                    throw new IndexOutOfRangeException(
                              "Cannot find Word position at beginning of string.");
                }

                if (searchPosition.Index == text.Length &&
                    direction == WordSearchDirection.Right)
                {
                    throw new IndexOutOfRangeException(
                              "Cannot find Word position at end of string.");
                }

                // Depending on the direction, we use the word tokenizer in
                // the appropriate direction. We also have to resolve the
                // position because the word tokenizer doesn't know how to handle
                // the symbolic constants.
                int searchIndex = searchPosition.GetCharacterIndex(text);

                if (direction == WordSearchDirection.Right)
                {
                    int index = wordTokenizer.GetNextWordBoundary(text, searchIndex);
                    return(index);
                }
                else
                {
                    int index = wordTokenizer.GetPreviousWordBoundary(text, searchIndex);
                    return(index);
                }
            }

            // If we got this far, we don't know how to process this.
            throw new IndexOutOfRangeException("Encountered an invalid index: " + Index);
        }