NSString – components separated by case insensitive string

The latest version of MailRaider greatly improves its handling of base64 encoded or quoted printable strings – the previous version could get confused if, for example, it encountered ‘=?UTF-8?B?’ instead of ‘=?utf-8?b?’. The problem is that NSString only has componentsSeparatedByString – which isn’t much use if you don’t know what case to expect. Hmm.

To solve this problem, I wrote my own componentsSeparatedByCaseInsensitiveString – it works in exactly the same way as componentsSeparatedByString, and it returns the same result, with the exception that it’s case insensitive. Hence the name.

Feel free to use it in your own code – no attribution necessary. All I ask is that, if you can improve this code (perhaps by making it faster and more efficient) then you share your improvements in the comments below.

The latest version of MailRaider greatly improves its handling of base64 encoded or quoted printable strings – the previous version could get confused if, for example, it encountered ‘=?UTF-8?B?’ instead of ‘=?utf-8?b?’. The problem is that NSString only has componentsSeparatedByString – which isn’t much use if you don’t know what case to expect. Hmm.

To solve this problem, I wrote my own componentsSeparatedByCaseInsensitiveString – it works in exactly the same way as componentsSeparatedByString, and it returns the same result, with the exception that it’s case insensitive. Hence the name.

Feel free to use it in your own code – no attribution necessary. All I ask is that, if you can improve this code (perhaps by making it faster and more efficient) then you share your improvements in the comments below.

@implementation NSString (String_Wangers)

- (NSArray<NSString *> *)componentsSeparatedByCaseInsensitiveString:(NSString *)separator {
    NSMutableArray* returnArray = NSMutableArray.new;
    NSRange searchRange = NSMakeRange(0,self.length);
    NSRange iRange;
    while (true) {
       searchRange.length = self.length - searchRange.location;
       iRange = [self rangeOfString:separator options:NSCaseInsensitiveSearch range:searchRange];
       if (iRange.location != NSNotFound) {
          searchRange.location = iRange.location+iRange.length;
          NSString* subString = [self substringWithRange:NSMakeRange(searchRange.location,self.length - searchRange.location)];
          NSRange jRange = [subString rangeOfString:separator options:NSCaseInsensitiveSearch];
          [returnArray addObject:(jRange.location != NSNotFound)?[subString substringWithRange:NSMakeRange(0,jRange.location)]:subString];
       } else {
          iRange = [self rangeOfString:separator options:NSCaseInsensitiveSearch];
          [returnArray insertObject:(iRange.location != NSNotFound)?[self substringWithRange:NSMakeRange(0,iRange.location)]:self atIndex:0];
          break;
       }
    }
    return returnArray;
}

@end

Leave a Reply

Your email address will not be published. Required fields are marked *