• 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: Reading in dictionary from txt file: options for speed
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading in dictionary from txt file: options for speed


  • Subject: Re: Reading in dictionary from txt file: options for speed
  • From: Marcel Weiher <email@hidden>
  • Date: Thu, 16 Apr 2009 17:52:17 -0700


On Apr 16, 2009, at 12:01 , Miles wrote:


It looks like I have the search working like this, but I have to double-space the dictionary file to have a leading \n.

No, you just need one initial extra newline at the start, the newline from the end of the last string matches up with the starting newline of the modified search string.



NSString *searchStr = @"\njoy\n";
NSData *strData = [searchStr dataUsingEncoding:NSUTF8StringEncoding];
const char *strBytes = [strData bytes];
const char *fileBytes = [stringFileContents bytes];
char *ptr = strstr(fileBytes, strBytes);


Is that what you were thinking?

Roughly.

-(BOOL)containsString:(NSString*)searchString
{
int stringLen=[searchString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
if ( stringLength < 200 ) {
char buffer[ stringLen +4 ];
char *ptr;
buffer[0]='\n';
[searchString getBytes:buffer+1 maxLength:stringLen usedLength:NULL encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0,stringLen) remainingRange:NULL];
buffer[stringLen+1]='\n';
buffer[stringLen+2]=0;
ptr = strnstr([stringFileContents bytes], buffer, [stringFileContents length] );
return ptr != NULL;
}
}



1. I use a local buffer and copy the string into that, and assume you don't have words with more than 200 characters.
2. I copy the newlines into that buffer and null terminate it.


-> Your method is probably just as good, except that you need to null terminate your strings, not sure that happens.

3. I use strnstr() instead of strstr() because the dict isn't null terminated either.


Cheers,

Marcel

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Reading in dictionary from txt file: options for speed (From: Miles <email@hidden>)
 >Re: Reading in dictionary from txt file: options for speed (From: Marcel Weiher <email@hidden>)
 >Re: Reading in dictionary from txt file: options for speed (From: WT <email@hidden>)
 >Re: Reading in dictionary from txt file: options for speed (From: Miles <email@hidden>)
 >Re: Reading in dictionary from txt file: options for speed (From: Marcel Weiher <email@hidden>)
 >Re: Reading in dictionary from txt file: options for speed (From: WT <email@hidden>)
 >Re: Reading in dictionary from txt file: options for speed (From: Miles <email@hidden>)

  • Prev by Date: Re: Simulating a click in NSStepper
  • Next by Date: Re: Reading in dictionary from txt file: options for speed
  • Previous by thread: Re: Reading in dictionary from txt file: options for speed
  • Next by thread: Re: Reading in dictionary from txt file: options for speed
  • Index(es):
    • Date
    • Thread