user663896
user663896

Reputation:

Is the NSData a substring of the another NSData?

I have two NSData objects

NSData *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...

Upvotes: 0

Views: 390

Answers (2)

kperryua
kperryua

Reputation: 10534

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.

Upvotes: 2

Dave DeLong
Dave DeLong

Reputation: 243156

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.

Upvotes: 0

Related Questions