Skip to content
Snippets Groups Projects
Commit 31ebc601 authored by George Nachman's avatar George Nachman
Browse files

Ensure search results are sorted by start position. Fixes issues with find next doing crazy things.

parent c654c8ac
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -11,6 +11,7 @@
 
+ (instancetype)searchResultFromX:(int)x y:(long long)y toX:(int)endX y:(long long)endY;
- (BOOL)isEqualToSearchResult:(SearchResult *)other;
- (NSComparisonResult)compare:(SearchResult *)other;
 
@end
 
Loading
Loading
#import "SearchResult.h"
#import "VT100GridTypes.h"
 
@implementation SearchResult
 
Loading
Loading
@@ -35,4 +36,12 @@
return ((((((_startX * 33) ^ _endX) * 33) ^ _absStartY) * 33) ^ _absEndY);
}
 
- (NSComparisonResult)compare:(SearchResult *)other {
if (!other) {
return NSOrderedDescending;
}
return VT100GridAbsCoordOrder(VT100GridAbsCoordMake(_startX, _absStartY),
VT100GridAbsCoordMake(other->_startX, other->_absStartY));
}
@end
Loading
Loading
@@ -224,6 +224,23 @@ NS_INLINE NSComparisonResult VT100GridCoordOrder(VT100GridCoord a, VT100GridCoor
return NSOrderedSame;
}
 
NS_INLINE NSComparisonResult VT100GridAbsCoordOrder(VT100GridAbsCoord a, VT100GridAbsCoord b) {
if (a.y < b.y) {
return NSOrderedAscending;
}
if (a.y > b.y) {
return NSOrderedDescending;
}
if (a.x < b.x) {
return NSOrderedAscending;
}
if (a.x > b.x) {
return NSOrderedDescending;
}
return NSOrderedSame;
}
NS_INLINE VT100GridRun VT100GridRunMake(int x, int y, int length) {
VT100GridRun run;
run.origin.x = x;
Loading
Loading
Loading
Loading
@@ -208,7 +208,21 @@
return;
}
 
[_searchResults insertObject:searchResult atIndex:0];
NSInteger insertionIndex = [_searchResults indexOfObject:searchResult
inSortedRange:NSMakeRange(0, _searchResults.count)
options:NSBinarySearchingInsertionIndex
usingComparator:^NSComparisonResult(SearchResult * _Nonnull obj1, SearchResult * _Nonnull obj2) {
NSComparisonResult result = [obj1 compare:obj2];
switch (result) {
case NSOrderedAscending:
return NSOrderedDescending;
case NSOrderedDescending:
return NSOrderedAscending;
default:
return result;
}
}];
[_searchResults insertObject:searchResult atIndex:insertionIndex];
 
// Update highlights.
for (long long y = searchResult.absStartY; y <= searchResult.absEndY; y++) {
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment