NSScanner Oddities
NSScanner Oddities
- Subject: NSScanner Oddities
- From: Brock Brandenberg <email@hidden>
- Date: Mon, 18 Mar 2002 21:39:46 -0600
I am trying to trim leading and trailing whitespace from some text
objects, but I'm having difficulty understanding why NSScanner skips
some of the characters it does.
The initial concept was to leave the default whitespace and newline skip
set and tell the scanner to find the first character not in this set. I
use the inverted set so as not to lose any characters that I might leave
out if unioning together letter, numbers, etc. (This appears to be
what's causing the problem...)
unsigned int firstChar, lastChar;
NSCharacterSet *whitespace;
NSScanner *scanner;
whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
scanner = [NSScanner scannerWithString:[self string]];
if( [scanner isAtEnd] == NO )
{
[scanner scanUpToCharactersFromSet:[whitespace invertedSet]
intoString:nil];
firstChar = [scanner scanLocation];
...
}
This should give me the position of the first non-whitespace character,
but NSScanner seems to stop immediately on any leading whitespace as
though the inverted set is not correct. To solve this, I am having to
set the skip set to nil, then reset it after the first match so that the
scanner makes subsequent hops correctly.
whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
scanner = [NSScanner scannerWithString:[self string]];
if( [scanner isAtEnd] == NO )
{
[scanner setCharactersToBeSkipped:nil];
[scanner scanUpToCharactersFromSet:[whitespace invertedSet]
intoString:nil]; // skip past leading whitespace
[scanner setCharactersToBeSkipped:whitespace]; // reset the skip chars
firstChar = [scanner scanLocation];
lastChar = firstChar;
while( [scanner isAtEnd] == NO )
{
if( [scanner scanUpToCharactersFromSet:whitespace intoString:nil] )
{
lastChar = [scanner scanLocation]; // if we got one, save
the ending location
}
}
[self setString:[[self string]
substringWithRange:NSMakeRange(firstChar,(lastChar-firstChar))]];
}
I can set the scanUpToCharactersFromSet: to letters or numbers and have
it find the first occurrence, but I want to insure that I get the full
set of unicode, non-whitespace characters. From the docs, I'm unsure how
to get this set with any other method.
As for general feedback, the initial skipping of characters is not well
documented at all. I'm sure other developers, like myself, miss the old
"Inside Mac" series where concepts were well illustrated and had good
examples instead of just having an API reference. For example, if you
use scanUpToCharactersFromSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet] on a string such as @" first second
third", it will not stop at the beginning of the string as would be
expected. It stops between "first and second". Since the behavior is not
consistent with the way standard c lib functions behave, lack of good
documentation for this kind of behavior costs countless hours of
frustration. Is there a way that user feedback can be incorporated and
linked into the docs, kind of like user tech notes?
Thanks,
Brock Brandenberg
----- industrial design @ bergdesign ------
web site: www.bergdesign.com
email: email@hidden
--------------------------------------------
_______________________________________________
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.