-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow regex search #9228
Closed
Closed
Allow regex search #9228
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dfa8164
initial
PankajBhojwani 65fba1d
wrap around
PankajBhojwani f7c47a0
find previous
PankajBhojwani 71b83da
comment
PankajBhojwani 08f5843
update comment
PankajBhojwani 09fa066
take into account glyph width
PankajBhojwani 12b5f65
const
PankajBhojwani 839af9f
move increment to inside the loop
PankajBhojwani 324a6a3
fix test hopefully?
PankajBhojwani 0d6631e
Migrate spelling-0.0.19 changes from main
DHowett 4a45e05
Migrate spelling-0.0.21 changes from main
DHowett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,11 +25,14 @@ using namespace Microsoft::Console::Types; | |
Search::Search(IUiaData& uiaData, | ||
const std::wstring& str, | ||
const Direction direction, | ||
const Sensitivity sensitivity) : | ||
const Sensitivity sensitivity, | ||
bool regex) : | ||
_direction(direction), | ||
_sensitivity(sensitivity), | ||
_needle(s_CreateNeedleFromString(str)), | ||
_uiaData(uiaData), | ||
_regex(regex), | ||
_inputString(str), | ||
_coordAnchor(s_GetInitialAnchor(uiaData, direction)) | ||
{ | ||
_coordNext = _coordAnchor; | ||
|
@@ -50,11 +53,14 @@ Search::Search(IUiaData& uiaData, | |
const std::wstring& str, | ||
const Direction direction, | ||
const Sensitivity sensitivity, | ||
const COORD anchor) : | ||
const COORD anchor, | ||
bool regex) : | ||
_direction(direction), | ||
_sensitivity(sensitivity), | ||
_needle(s_CreateNeedleFromString(str)), | ||
_coordAnchor(anchor), | ||
_regex(regex), | ||
_inputString(str), | ||
_uiaData(uiaData) | ||
{ | ||
_coordNext = _coordAnchor; | ||
|
@@ -75,20 +81,31 @@ bool Search::FindNext() | |
return false; | ||
} | ||
|
||
do | ||
if (_regex) | ||
{ | ||
if (_FindNeedleInHaystackAt(_coordNext, _coordSelStart, _coordSelEnd)) | ||
{ | ||
_UpdateNextPosition(); | ||
_reachedEnd = _coordNext == _coordAnchor; | ||
return true; | ||
} | ||
else | ||
auto start = _direction == Direction::Forward ? _coordNext : COORD{ 0 }; | ||
auto end = _direction == Direction::Forward ? _uiaData.GetTextBufferEndPosition() : _coordNext; | ||
auto wrapStart = _direction == Direction::Forward ? COORD{ 0 } : _coordNext; | ||
auto wrapEnd = _direction == Direction::Forward ? _coordNext : _uiaData.GetTextBufferEndPosition(); | ||
return _RegexHelper(start, end) || _RegexHelper(wrapStart, wrapEnd); | ||
} | ||
else | ||
{ | ||
do | ||
{ | ||
_UpdateNextPosition(); | ||
} | ||
|
||
} while (_coordNext != _coordAnchor); | ||
if (_FindNeedleInHaystackAt(_coordNext, _coordSelStart, _coordSelEnd)) | ||
{ | ||
_UpdateNextPosition(); | ||
_reachedEnd = _coordNext == _coordAnchor; | ||
return true; | ||
} | ||
else | ||
{ | ||
_UpdateNextPosition(); | ||
} | ||
|
||
} while (_coordNext != _coordAnchor); | ||
} | ||
|
||
return false; | ||
} | ||
|
@@ -344,3 +361,57 @@ std::vector<std::vector<wchar_t>> Search::s_CreateNeedleFromString(const std::ws | |
} | ||
return cells; | ||
} | ||
|
||
bool Search::_RegexHelper(COORD start, COORD end) | ||
{ | ||
std::wstring concatAll; | ||
auto begin = start; | ||
std::wsmatch match; | ||
|
||
// To deal with text that spans multiple lines, we will first concatenate | ||
// all the text into one string and find the regex in that string | ||
while (begin != end) | ||
{ | ||
concatAll += *_uiaData.GetTextBuffer().GetTextDataAt(begin); | ||
_IncrementCoord(begin); | ||
} | ||
|
||
// Create the regex object and iterator | ||
std::wregex regexObj{ _inputString }; | ||
auto matches_begin = std::wsregex_iterator(concatAll.begin(), concatAll.end(), regexObj); | ||
auto matches_end = std::wsregex_iterator(); | ||
|
||
// If we found a match, get its position and length to update the selection coordinates | ||
if (matches_begin != matches_end) | ||
{ | ||
std::wsregex_iterator desired; | ||
if (_direction == Direction::Forward) | ||
{ | ||
desired = matches_begin; | ||
} | ||
else | ||
{ | ||
// Regex iterators are not bidirectional, so we cannot create a reverse iterator out of one | ||
// So, if we want to find the previous match, we need to manually progress the iterator | ||
while (matches_begin != matches_end) | ||
{ | ||
desired = matches_begin; | ||
++matches_begin; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There really should be a better way to do this |
||
} | ||
const auto pos = desired->position(); | ||
for (auto i = 0; i < pos; ++i) | ||
{ | ||
_IncrementCoord(start); | ||
} | ||
PankajBhojwani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_coordSelStart = start; | ||
const auto len = desired->length(); | ||
for (auto i = 0; i < (len - 1); ++i) | ||
{ | ||
_IncrementCoord(start); | ||
} | ||
_coordSelEnd = start; | ||
return true; | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the user accidentally types a pathological regex and the search takes too long, is there any way to abort it? It doesn't look like std::regex_iterator supports any kind of cancellation token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if cancellation could be implemented by throwing an exception from operators of a custom iterator type. Would there be a risk that such an exception can skip some cleanup in the regexp algorithm, or that the algorithm can copy part of the input to an internal buffer and then spend excessive time examining that buffer, without using the original iterators?