📄 bruteforcesearchstrategy.cs
字号:
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
// <version>$Revision: 1261 $</version>
// </file>
using System;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Internal.Undo;
using ICSharpCode.TextEditor.Document;
namespace SearchAndReplace
{
/// <summary>
/// Only for fallback purposes.
/// </summary>
public class BruteForceSearchStrategy : ISearchStrategy
{
string searchPattern;
bool MatchCaseSensitive(ITextBufferStrategy document, int offset, string pattern)
{
for (int i = 0; i < pattern.Length; ++i) {
if (offset + i >= document.Length || document.GetCharAt(offset + i) != pattern[i]) {
return false;
}
}
return true;
}
bool MatchCaseInsensitive(ITextBufferStrategy document, int offset, string pattern)
{
for (int i = 0; i < pattern.Length; ++i) {
if (offset + i >= document.Length || Char.ToUpper(document.GetCharAt(offset + i)) != pattern[i]) {
return false;
}
}
return true;
}
bool IsWholeWordAt(ITextBufferStrategy document, int offset, int length)
{
return (offset - 1 < 0 || !Char.IsLetterOrDigit(document.GetCharAt(offset - 1))) &&
(offset + length + 1 >= document.Length || !Char.IsLetterOrDigit(document.GetCharAt(offset + length)));
}
int InternalFindNext(ITextIterator textIterator)
{
while (textIterator.MoveAhead(1)) {
if (SearchOptions.MatchCase ? MatchCaseSensitive(textIterator.TextBuffer, textIterator.Position, searchPattern) : MatchCaseInsensitive(textIterator.TextBuffer, textIterator.Position, searchPattern)) {
if (!SearchOptions.MatchWholeWord || IsWholeWordAt(textIterator.TextBuffer, textIterator.Position, searchPattern.Length)) {
return textIterator.Position;
}
}
}
return -1;
}
int InternalFindNext(ITextIterator textIterator, int offset, int length)
{
while (textIterator.MoveAhead(1) && TextSelection.IsInsideRange(textIterator.Position, offset, length)) {
if (SearchOptions.MatchCase ? MatchCaseSensitive(textIterator.TextBuffer, textIterator.Position, searchPattern) : MatchCaseInsensitive(textIterator.TextBuffer, textIterator.Position, searchPattern)) {
if (!SearchOptions.MatchWholeWord || IsWholeWordAt(textIterator.TextBuffer, textIterator.Position, searchPattern.Length)) {
if (TextSelection.IsInsideRange(textIterator.Position + searchPattern.Length - 1, offset, length)) {
return textIterator.Position;
} else {
return -1;
}
}
}
}
return -1;
}
public bool CompilePattern()
{
searchPattern = SearchOptions.MatchCase ? SearchOptions.FindPattern : SearchOptions.FindPattern.ToUpper();
return true;
}
public SearchResult FindNext(ITextIterator textIterator)
{
int offset = InternalFindNext(textIterator);
return GetSearchResult(offset);
}
public SearchResult FindNext(ITextIterator textIterator, int offset, int length)
{
int foundOffset = InternalFindNext(textIterator, offset, length);
return GetSearchResult(foundOffset);
}
SearchResult GetSearchResult(int offset)
{
return offset >= 0 ? new SearchResult(offset, searchPattern.Length) : null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -