Is the NSData a substring of the another NSData?
I have two NSData
objects
N开发者_高级运维SData *toScan = /* initialized somehow with "Hello, this world." */;
NSData *toMatch = /* initialized somehow with "this" */;
What is the best way to know the toMatch
bytes are the subset of toScan
bytes?
I use C-functions for this purposes at this point: something like this
strstr([[toScan identifier] bytes], [[toMatch identifier] bytes]);
// returned bytes are null-terminated
but I think there is not the best way to use C-functions in object-oriented environment...
As of Snow Leopard and iOS 4.0, NSData has -rangeOfData:options:range:
which should do what you want.
It's also pretty darn fast.
In a different question, I wrote an answer containing an NSData
category with a rangeOfData:
method:
Elegant Algorithm for Parsing Data Stream Into Record
That'll do what you're looking for.
精彩评论