• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Looking for bytes in NSData
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Looking for bytes in NSData


  • Subject: Re: Looking for bytes in NSData
  • From: David Remahl <email@hidden>
  • Date: Sun, 27 Oct 2002 18:40:33 +0100

>> At 7:03 AM -0600 10/27/02, Chris Vincent wrote:
>>> Is there a way to look for a sequence of bytes in NSData? I'm looking
>>> for something similar to NSString's -rangeOfString. Is this possible?
>>
>> I've been using NSData's -bytes method combined with the C function
>> strstr(). Do a "man strstr" for details and variants on strstr().
>
> You should avoid strstr() because it won't handle NUL bytes in either value
> correctly from your point of view.
>
>> If anyone knows a smarter way, please let us both know!
>
> Having said that, I haven't found any obvious method (or CFData function) to
> do what you want.
>
> Cheers,
>
> Chris

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;
}

/ Regards, David
_______________________________________________
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.

  • Follow-Ups:
    • Re: Looking for bytes in NSData
      • From: Julian Barkway <email@hidden>
References: 
 >Re: Looking for bytes in NSData (From: Chris Ridd <email@hidden>)

  • Prev by Date: Re: Looking for bytes in NSData
  • Next by Date: Project Builder Question
  • Previous by thread: Re: Looking for bytes in NSData
  • Next by thread: Re: Looking for bytes in NSData
  • Index(es):
    • Date
    • Thread