Hi --

I'm getting some really odd behavior out of NSScanner; it seems to be choking on character sets that only have whitespace in them.

This doesn't work:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        
                NSString *test=@"   ooxxdk";
NSCharacterSet *whiteSpaceCharSet=[NSCharacterSet characterSetWithCharactersInString:@"\t\r\n "];
                
                NSScanner *scanner=[NSScanner scannerWithString:test];
                
                
                NSLog(@"location before: %d", [scanner scanLocation]);
BOOL result=[scanner scanCharactersFromSet:whiteSpaceCharSet intoString:nil];
                NSLog(@"location after: %d", [scanner scanLocation]);
                NSLog(@"result returned: %d",result);
        
    [pool drain];
    return 0;
}

... it logs this:
2008-09-21 22:58:28.399 foo[804:10b] location before: 0
2008-09-21 22:58:28.401 foo[804:10b] location after: 0
2008-09-21 22:58:28.401 foo[804:10b] result returned: 0

I expected it to log something like this:
2008-09-21 23:01:31.357 foo[844:10b] location before: 0
2008-09-21 23:01:31.359 foo[844:10b] location after: 3
2008-09-21 23:01:31.360 foo[844:10b] result returned: 1


But, changing it like so:

                NSString *test=@"   aooxxdk";
NSCharacterSet *whiteSpaceCharSet=[[NSCharacterSet characterSetWithCharactersInString:@"\t\r\n a"] retain];

does work... logging this:

2008-09-21 23:01:31.357 foo[844:10b] location before: 0
2008-09-21 23:01:31.359 foo[844:10b] location after: 4
2008-09-21 23:01:31.360 foo[844:10b] result returned: 1

I get similar results swapping tabs and/or cr's for the leading spaces on the test string; as well as using the standard [NSCharacterSet whitespaceAndNewlineCharacterSet].

Anybody have any ideas why this isn't working?


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to