Saturday, June 1, 2013

NSData rangeOfData options range example in Objective C (iOS).


NSData rangeOfData options range

Finds and returns the range of the first occurrence of the given data, within the given range, subject to given options.

- (NSRange)rangeOfData:(NSData *)dataToFind options:(NSDataSearchOptions)mask range:(NSRange)searchRange

Parameters of [NSData rangeOfData options range]
dataToFind
The data for which to search. This value must not be nil.
Important: Raises an NSInvalidArgumentException if dataToFind is nil.
mask
A mask specifying search options. The “NSDataSearchOptions” options may be specified singly or by combining them with the C bitwise OR operator.
searchRange
The range within the receiver in which to search for dataToFind. If this range is not within the receiver’s range of bytes, an NSRangeException raised.

Return Value of [NSData rangeOfData options range]
An NSRange structure giving the location and length of dataToFind within searchRange, modulo the options in mask. The range returned is relative to the start of the searched data, not the passed-in search range. Returns {NSNotFound, 0} if dataToFind is not found or is empty (@"").

NSData rangeOfData options range example.
//This is just mock up data to represent what would be passed into your method
unsigned char ch1[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x04, 0x01, 0x00, 0x0F };
NSData *data1 = [[NSData alloc] initWithBytes:ch1
                                       length:sizeof(ch1)];
//This is the data used for the comparison
NSData *data2 = [[NSData alloc] initWithBytes:(unsigned char[]){0x04, 0x01, 0x00}
                                       length:3];

NSRange range = [data1 rangeOfData:data2
                           options:0
                             range:NSMakeRange(0, [data1 length])];

if(range.location != NSNotFound)
{
     NSLog(@"Found pattern!");
}

Example of [NSData rangeOfData options range].
    NSString *testString = @"01K234567K";
    NSData *testData = [testString dataUsingEncoding:NSUTF8StringEncoding];
    NSString *testLetter = @"K";
    NSData *kData = [testLetter dataUsingEncoding:NSUTF8StringEncoding];
    NSRange whereisK = [testData rangeOfData:kData options:NSDataSearchBackwards      range:NSMakeRange(0, [testData length])];
    NSLog(@"loc: %d, len: %d",whereisK.location,whereisK.length);

NSData rangeOfData options range example.
const NSUInteger dataLength = 130;
unsigned char dataBytes[dataLength] = {
0x01,0x00,0x00,0x04,0x40,0x00,0x00,0x00,0x00,0x41,0x41,0x41,0x41,0x48,0x47,0x5A,
0x30,0x65,0x58,0x42,0x4E,0x4E,0x45,0x45,0x67,0x41,0x41,0x41,0x41,0x41,0x45,0x30,
0x30,0x51,0x53,0x42,0x74,0x63,0x44,0x51,0x79,0x61,0x58,0x4E,0x76,0x62,0x51,0x41,
0x41,0x41,0x2B,0x39,0x74,0x62,0x32,0x39,0x32,0x41,0x41,0x41,0x41,0x62,0x47,0x31,
0x32,0x61,0x47,0x51,0x41,0x41,0x41,0x41,0x41,0x71,0x67,0x59,0x35,0x79,0x36,0x6F,
0x47,0x4F,0x63,0x73,0x41,0x41,0x4B,0x78,0x45,0x41,0x41,0x43,0x34,0x41,0x41,0x41,
0x42,0x41,0x41,0x41,0x42,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
0x41,0x41,0x41,0x41,0x41,0x41,0x51,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
0x41,0x41};
NSData *data = [NSData dataWithBytes:dataBytes length:130];

const NSUInteger startBytesLength = 4;
unsigned char startBytes[startBytesLength] = {0x40, 0x00, 0x00, 0x00};
const NSUInteger stopBytesLength = 1;
unsigned char stopBytes[stopBytesLength] = {0x34};

NSData *startData = [NSData dataWithBytes:startBytes length:startBytesLength];
NSData *stopData  = [NSData dataWithBytes:stopBytes  length:stopBytesLength];
NSRange startRange = [data rangeOfData:startData options:0 range:NSMakeRange(0, dataLength)];
NSUInteger start = startRange.location;

NSRange stopRange  = [data rangeOfData:stopData  options:0 range:NSMakeRange(start+startBytesLength, dataLength-(start+startBytesLength))];
NSUInteger length = stopRange.location - start;
NSUInteger stopLocation = stopRange.location+stopBytesLength;

NSData *extractData = [data subdataWithRange:NSMakeRange(start, length)];
NSLog(@"extractData: %@", extractData);

NSMutableData *newData = [NSMutableData data];
[newData appendData:[data subdataWithRange:NSMakeRange(0, start)]];
[newData appendData:[data subdataWithRange:NSMakeRange(stopLocation, dataLength-stopLocation)]];
NSLog(@"newData: %@", newData);

End of NSData rangeOfData options range example article.