Re: wordRangeForRange implememtation
Re: wordRangeForRange implememtation
- Subject: Re: wordRangeForRange implememtation
- From: "Adam R. Maxwell" <email@hidden>
- Date: Sun, 11 May 2008 15:39:33 -0700
On May 11, 2008, at 2:45 PM, Jérome Laurens wrote:
Le 11 mai 08 à 04:42, Ali Ozer a écrit :
To extract a word range from a string, it may be easier to use
something like NSScanner:
// Defining the words
NSCharacterSet *validWordSet = [NSCharacterSet
alphanumericCharacterSet]; // Or however you want to define words
NSCharacterSet *wordBreakSet = [validWordSet invertedSet];
// Set up a scanner
NSScanner *scanner = [NSScanner scannerWithString:yourString];
[scanner setCharactersToBeSkipped:nil]; // We will manage
skipping ourselves (since we need to find the beginning of the word)
A long time ago, I played seriously with NSScanner to implement some
syntax coloring.
It was unbearably slow, and I ended with the conclusion that
[NSScanner scannerWithString:]; was making a copy of the given string.
So I reimplemented my own NSScanner and it was way faster.
Is it true that NSScanner duplicates the string?
Interesting question...I wrote a quick test program and set a
breakpoint on -[NSCFString copyWithZone:], and NSScanner indeed copied
it. For immutable strings, that's probably a retain, but it could be
pretty expensive for mutable strings or the NSTextStorage proxy.
The main performance problem I've had with NSScanner is that -
[NSScanner scanCharactersFromSet:] creates an autoreleased inverted
NSCharacterSet, which can blow up an autorelease pool pretty quickly
(rdar://problem/4652388). My conclusion is that it's not great for
parsing large strings or in a loop.
--
Adam_______________________________________________
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