Browse Source

Fix whole-word search in wildcard search mode.

http://community.sharpdevelop.net/forums/t/13167.aspx
4.0
Daniel Grunwald 15 years ago
parent
commit
35e5a72359
  1. 9
      src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceUtilities.cs
  2. 10
      src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/BruteForceSearchStrategy.cs

9
src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceUtilities.cs

@ -30,10 +30,15 @@ namespace SearchAndReplace
} }
} }
static bool IsWordPart(char c)
{
return char.IsLetterOrDigit(c) || c == '_';
}
public static bool IsWholeWordAt(IDocument document, int offset, int length) public static bool IsWholeWordAt(IDocument document, int offset, int length)
{ {
return (offset - 1 < 0 || Char.IsWhiteSpace(document.GetCharAt(offset - 1))) && return (offset - 1 < 0 || !IsWordPart(document.GetCharAt(offset - 1))) &&
(offset + length + 1 >= document.TextLength || Char.IsWhiteSpace(document.GetCharAt(offset + length))); (offset + length + 1 >= document.TextLength || !IsWordPart(document.GetCharAt(offset + length)));
} }
public static ISearchStrategy CreateSearchStrategy(SearchStrategyType type) public static ISearchStrategy CreateSearchStrategy(SearchStrategyType type)

10
src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/BruteForceSearchStrategy.cs

@ -34,17 +34,11 @@ namespace SearchAndReplace
return true; return true;
} }
bool IsWholeWordAt(IDocument document, int offset, int length)
{
return (offset - 1 < 0 || !Char.IsLetterOrDigit(document.GetCharAt(offset - 1))) &&
(offset + length + 1 >= document.TextLength || !Char.IsLetterOrDigit(document.GetCharAt(offset + length)));
}
int InternalFindNext(ITextIterator textIterator) int InternalFindNext(ITextIterator textIterator)
{ {
while (textIterator.MoveAhead(1)) { while (textIterator.MoveAhead(1)) {
if (SearchOptions.MatchCase ? MatchCaseSensitive(textIterator.Document, textIterator.Position, searchPattern) : MatchCaseInsensitive(textIterator.Document, textIterator.Position, searchPattern)) { if (SearchOptions.MatchCase ? MatchCaseSensitive(textIterator.Document, textIterator.Position, searchPattern) : MatchCaseInsensitive(textIterator.Document, textIterator.Position, searchPattern)) {
if (!SearchOptions.MatchWholeWord || IsWholeWordAt(textIterator.Document, textIterator.Position, searchPattern.Length)) { if (!SearchOptions.MatchWholeWord || SearchReplaceUtilities.IsWholeWordAt(textIterator.Document, textIterator.Position, searchPattern.Length)) {
return textIterator.Position; return textIterator.Position;
} }
} }
@ -59,7 +53,7 @@ namespace SearchAndReplace
textIterator.Position = offset; textIterator.Position = offset;
} }
if (SearchOptions.MatchCase ? MatchCaseSensitive(textIterator.Document, textIterator.Position, searchPattern) : MatchCaseInsensitive(textIterator.Document, textIterator.Position, searchPattern)) { if (SearchOptions.MatchCase ? MatchCaseSensitive(textIterator.Document, textIterator.Position, searchPattern) : MatchCaseInsensitive(textIterator.Document, textIterator.Position, searchPattern)) {
if (!SearchOptions.MatchWholeWord || IsWholeWordAt(textIterator.Document, textIterator.Position, searchPattern.Length)) { if (!SearchOptions.MatchWholeWord || SearchReplaceUtilities.IsWholeWordAt(textIterator.Document, textIterator.Position, searchPattern.Length)) {
if (TextSelection.IsInsideRange(textIterator.Position + searchPattern.Length - 1, offset, length)) { if (TextSelection.IsInsideRange(textIterator.Position + searchPattern.Length - 1, offset, length)) {
return textIterator.Position; return textIterator.Position;
} else { } else {

Loading…
Cancel
Save