Esempio n. 1
0
        /// <summary>
        /// Works backwards from the caret position and finds the index of the previous word,
        /// and also returns the size of the word
        /// </summary>
        /// <param name="inputString">input text string</param>
        /// <param name="caretPos">cursor position</param>
        /// <param name="offset">Offset of the previous word (0 if beginning reached)</param>
        /// <param name="count">size of the previous word</param>
        /// <returns></returns>
        public static bool GetPrevWordOffset(String inputString, int caretPos, out int offset, out int count)
        {
            try
            {
                count  = 0;
                offset = caretPos;

                if (String.IsNullOrEmpty(inputString))
                {
                    return(true);
                }

                caretPos--;
                int len = inputString.Length;
                if (caretPos >= len)
                {
                    caretPos = len - 1;
                }
                else if (caretPos < 0)
                {
                    return(false);
                }

                int index = caretPos;
                offset = caretPos;
                int endPos = caretPos;

                while (index >= 0 && Char.IsWhiteSpace(inputString[index]))
                {
                    index--;
                }

                while (index >= 0 && IsTerminator(inputString[index]))
                {
                    index--;
                }

                if (index < 0)
                {
                    index = 0;
                }
                else
                {
                    while (index >= 0 && !Char.IsWhiteSpace(inputString[index]) && !TextUtils.IsSentenceTerminator(inputString[index]))
                    {
                        index--;
                    }

                    index++;
                }

                offset = index;
                count  = endPos - offset + 1;

                return(true);
            }
            catch (Exception ex)
            {
                Log.Debug(ex.ToString());
                offset = 0;
                count  = 0;
                return(false);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Useful to replace a word in the input string with a new word.  Makes a
        /// determination whether the cursor is within a word or past the word.  If
        /// it is within the word, it means the word has to be replaced with the new word.
        /// If it is past the word (there is no current word), the new word has to be
        /// inserted at the cursor position
        /// ap
        /// </summary>
        /// <param name="inputString">Input text string</param>
        /// <param name="caretPos">Cursor position</param>
        /// <param name="insertOrReplaceOffset">Returns the offset at which to insert/replace</param>
        /// <param name="wordToReplace">Returns the word to replace (null if none)</param>
        /// <returns>true if the word has to be inserted</returns>
        public static bool CheckInsertOrReplaceWord(String inputString, int caretPos, out int insertOrReplaceOffset, out String wordToReplace)
        {
            try
            {
                wordToReplace         = String.Empty;
                insertOrReplaceOffset = caretPos;

                if (String.IsNullOrEmpty(inputString))
                {
                    Log.Debug("NULL string. return true " + insertOrReplaceOffset);
                    return(true);
                }

                caretPos--;
                if (caretPos < 0)
                {
                    caretPos = 0;
                }

                int len = inputString.Length;
                if (caretPos >= len)
                {
                    caretPos = len - 1;
                }

                int index = caretPos;
                insertOrReplaceOffset = caretPos;
                Log.Debug("index: " + index + ", inputString[index] is [" + inputString[index] + "]");
                // cursor is not within a word
                if (Char.IsWhiteSpace(inputString[index]))
                {
                    insertOrReplaceOffset = caretPos + 1;
                    Log.Debug("iswhiespace. return true " + insertOrReplaceOffset);
                    return(true);
                }

                // cursor is at a sentence terminator.  needs
                // insertion
                if (TextUtils.IsSentenceTerminator(inputString[index]))
                {
                    insertOrReplaceOffset = caretPos + 1;
                    Log.Debug("is sentence terminator. return true " + insertOrReplaceOffset);
                    return(true);
                }

                // cursor is at a word.  Needs to be replaced
                if (TextUtils.IsWordElement(inputString[index]))
                {
                    insertOrReplaceOffset = TextUtils.getWordToReplace(inputString, caretPos, out wordToReplace);
                    Log.Debug("iswordelement is true.  return false " + insertOrReplaceOffset);
                    return(false);
                }
                else
                {
                    insertOrReplaceOffset = caretPos + 1;
                    Log.Debug("iswordelement is false.  return true " + insertOrReplaceOffset);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Log.Debug(ex.ToString());
                insertOrReplaceOffset = 0;
                wordToReplace         = String.Empty;
                return(true);
            }
        }