开发者

Is there a way to get an NSArray of NSRanges for an occurrence of an NSString inside of another NSString?

开发者 https://www.devze.com 2023-01-15 02:25 出处:网络
I have been recently working on an easy to use Syntax Highlighting system for a personal project. So far, I have everything working properly by finding the range of specific strings and highlighting t

I have been recently working on an easy to use Syntax Highlighting system for a personal project. So far, I have everything working properly by finding the range of specific strings and highlighting that range in the NSTextView. Everything works until you try to type a highlighted word twice, which causes the error demonstrated below:

开发者_开发知识库
<div class="container">
    <div> <!-- This <div> would not get highlighted -->
        Hello World
    </div>
</div> <!-- this </div> would not get highlighted -->

I believe this is occurring because I am only getting the first occurrence of an NSString using the rangeOfString method, and for that reason, only the first highlighted item is being highlighted.

Sorry for the long explanation! Anyway, I was wondering if there was an rangesOfString method or something similar that can give me an NSArray of NSRanges for each occurrence of an NSString inside of another NSString. Below is the line of code I am using to get my range currently:

NSString *textViewString = [textView string];
NSString *textToHighlight = @"<div>";
NSRange area;

area.location = 0;
area.length = [textViewString length];

NSRange range = [textViewString rangeOfString: textToHighlight 
                                      options: NSCaseInsensitiveSearch 
                                        range: area];

What I would like is something like this:

NSString *textViewString = [textView string];
NSString *textToHighlight = @"<div>";
NSRange area;

area.location = 0;
area.length = [textViewString length];

NSArray *ranges = [textViewString rangesOfString: textToHighlight
                                         options: NSCaseInsensitiveSearch
                                           range: area];

int i;
int total = [ranges count];
for (i = 0; i < total; i++) {
    NSRange occurrence = [ranges objectAtIndex: i];
    // Apply color highlighting here
}

If this is not possible, could someone please point me in the right direction or suggest an alternative way of doing syntax highlighting? Thank you!


There is no such method. You can use rangeOfSubstring:options:range: to get each match, and narrow down the range as you go: After each match, you change the search range's location to the character after (location + length of) the match range and reduce the search range's length by the difference.


In the modern block-based world, it's not that hard to write an “enumerate substrings that match…” method that does the above as its implementation. Here's what that looks like:

- (NSUInteger) enumerateSubstringsMatchingString_PRH:(NSString *)stringToFind
    options:(NSStringCompareOptions)mask
    usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block
{
    NSUInteger count = 0;

    NSUInteger selfLength = self.length;
    NSRange searchRange = { 0, selfLength };
    NSRange foundRange;
    BOOL stop = NO;
    while (( ! stop ) && (foundRange = [self rangeOfString:stringToFind options:mask range:searchRange]).location != NSNotFound) {
        ++count;
        if (block != NULL) {
            @autoreleasepool {
                NSString *substring = [self substringWithRange:foundRange];
                block(substring, foundRange, /*TODO include terminators and separators in enclosingRange as described in enumerateSubstringsWithOptions:: docs*/ foundRange, &stop);
            }
        }
        searchRange.location = NSMaxRange(foundRange);
        if (searchRange.location >= self.length)
            break;
        searchRange.length = selfLength - searchRange.location;
    }

    return count;
}

The Gist I linked above gives a few obvious test cases.


NSRange is not an object, so you need to put it in one. Look at NSValues +valueWithRange: class method.

0

精彩评论

暂无评论...
验证码 换一张
取 消