Re: NSScanner problem...
Re: NSScanner problem...
- Subject: Re: NSScanner problem...
- From: Bill Cheeseman <email@hidden>
- Date: Sun, 21 Apr 2002 17:00:46 -0400
on 02-04-21 3:39 PM, Onar Vikingstad at email@hidden wrote:
>
I'm having a wierd problem I can't figure out with NSScanner. When I use
>
setCharactersToBeSkipped in the second last line (as shown in my code),
>
only the first " gets skipped, not the second one at the end of the
>
string.
<snip>
>
Is this the way it's supposed to work? How can I get it to skip also the
>
last " ?
When you want an answer to such a question ("Is this the way it's supposed
to work?"), it is generally considered good form to read the documentation
before resorting to the mailing list. In this case, the documentation for
NSScanner's setCharactersToBeSkipped: method says this:
"Sets the receiver to ignore the characters in skipSet when scanning its
string for a value representation. For example, if a scanner ignores spaces
and you send it a scanInt: message, it skips spaces until it finds a decimal
digit or other character. While an element is being scanned, however, no
characters are skipped."
The first two sentences are worded very precisely. There is meaning in what
they don't say, and the third sentence drives the point home. In other
words, while it's looking for characters in the search set, a scanner will
skip characters in the skipSet. But once it's found a character in the
search set, it will stop skipping characters in the skipSet while it's
scanning the potential match. Then when you call the scanner again, it will
stop skipping characters in the skipSet again.
If you think about it, you'll see why this behavior is appropriate. If
you're looking for the string "I love my Mac" but you're concerned that the
author might have hit the space bar several times before starting to type,
it's quite nice to be able to skip spaces until you start to see a potential
match. But it would be useless to continue to skip spaces while you're
examining the potential match, itself, because the search pattern includes
spaces.
Also, notice this line in the documentation: "The default set of characters
to skip is the whitespace and newline character set." So, when you redesign
your code, watch out while you're scanning for the newline character, as you
do in your last line, or you will run into a second problem, described
nicely in this passage in the documentation: "If you scan for something made
of characters in the set to be skipped (for example, using scanInt: when the
set of characters to be skipped is the decimal digits), the result is
undefined."
As the examples show, NSScanner is best used repeatedly in a loop or a
succession of separate calls searching for different patterns. Designing
such a loop or succession of calls is usually pretty straightforward, once
you understand the skip behavior. In your case, you've already figured out
how to skip quotation marks at the beginning of the string you're searching
for. In some cases, once you've found the characters you're searching for,
you might want to search for them again in the remaining characters (i.e.,
the substring not yet searched), and it will skip quotation marks again
until it finds another character in the search set or the end of the text.
Or, in other cases, you might want to search for the closing quotation mark
explicitly and do whatever is appropriate with the accumulated string once
you find it. The latter sounds like the approach you're looking for. That
is, scan until you find either @"\"'" or newline. This could be combined as
@"\"'\n". However, if you do this, you would want to be sure to set the
scanner's skip set to something other than the default whitespace and
newline; for example, the empty set.
As an aside, in my opinion it was a mistake for Cocoa to define whitespace
and newline as the default skipSet. It's a trap for the unwary. Very few
people will remember from the documentation (if they read it) that new
scanners are set up this way by default. There will inevitably come a day
when they write a seemingly perfect bit of code that nevertheless doesn't
work, and they will waste hours or days trying to figure out why, until they
finally go back and look at the documentation again and at last appreciate
the significance of this one little sentence: "The default set of characters
to skip is the whitespace and newline character set." I speak from
experience.
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
The AppleScript Sourcebook -
http://www.AppleScriptSourcebook.com
Vermont Recipes -
http://www.stepwise.com/Articles/VermontRecipes
Croquet Club of Vermont -
http://members.valley.net/croquetvermont
_______________________________________________
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.