Re: Looking for bytes in NSData
Re: Looking for bytes in NSData
- Subject: Re: Looking for bytes in NSData
- From: Julian Barkway <email@hidden>
- Date: Sun, 27 Oct 2002 18:58:37 +0100
On Sunday, October 27, 2002, at 06:40 pm, David Remahl wrote:
It has always been a bit of a mystery to me why memmem() isn't a
standard
member of the C library...
You can adapt this method from OmniFoundation to return the position
of the
found bytestring (it could be quite heavily optimized):
- (BOOL)containsData:(NSData *)data;
{
unsigned const char *selfPtr, *selfEnd, *selfRestart, *ptr,
*ptrRestart,
*end;
ptrRestart = [data bytes];
end = ptrRestart + [data length];
selfRestart = [self bytes];
selfEnd = selfRestart + [self length] - [data length];
while(selfRestart <= selfEnd) {
selfPtr = selfRestart;
ptr = ptrRestart;
while(ptr < end) {
if (*ptr++ != *selfPtr++)
break;
}
if (ptr == end)
return YES;
selfRestart++;
}
return NO;
}
Ugggh! If anyone's interested, I have coded up a rangeOfCString: NSData
category method which is a wrapper for a much more efficient
Boyer-Moore search. Depends if you need to grind through large amounts
of data quickly, I guess....
--
Julian Barkway, Zurich, Switzerland.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.