bugGNUstep - Bugs: bug #3301, NSScanner can't scan ']' properly ?

Group
 
 

bug #3301: NSScanner can't scan ']' properly ?

Submitter:  -Deleted Account- <yjchen>
Submitted:  Tue 22 Apr 2003 12:52:34 PM UTC
   
 
Category:  Base/Foundation Severity:  3 - Normal
Item Group:  Bug Status:  Invalid
Privacy:  Public Assigned to:  None
Open/Closed:  Closed
* Mandatory Fields

Add a New Comment Rich Markup
   

Fri 25 Apr 2003 06:35:45 AM UTC, comment #2: 

Not a bug in NSScanner, which is performing correctly and as documented. I did commit a small change to the documentation in CVS in the hope of clarifying, but it was already quite clear.

Here is an example of how the code should have been written if the idea was to count the number of ocurrances of a a string in a file.

#include <Foundation/Foundation.h>

int main(int argc, const char *argv[])
{
  NSAutoreleasePool *pool = [NSAutoreleasePool new];

  NSString *string = [NSString stringWithContentsOfFile: @"NSMatrix.m"];
  NSScanner *scanner = [NSScanner scannerWithString: string];
  NSString *ch = @"[";
  unsigned total = 0;

  [scanner scanUpToString: ch intoString: NULL];
  while ([scanner scanString: ch intoString: NULL] == YES)
   {
     total++;
     [scanner scanUpToString: ch intoString: NULL];
   }

  NSLog(@"total %d", total);

  RELEASE(pool);
  return 0;
}

Richard Frith-Macdonald <CaS>
Group Member
Tue 22 Apr 2003 03:19:00 PM UTC, comment #1: 

Not a bug in NSScanner, which is performing correctly and as documented. I did commit a small change to the documentation in CVS in the hope of clarifying, but it was already quite clear.

Here is an example of how the code should have been written if the idea was to count the number of ocurrances of a a string in a file.

#include <Foundation/Foundation.h>

int main(int argc, const char *argv[])
{
  NSAutoreleasePool *pool = [NSAutoreleasePool new];

  NSString *string = [NSString stringWithContentsOfFile: @"NSMatrix.m"];
  NSScanner *scanner = [NSScanner scannerWithString: string];
  NSString *ch = @"[";
  unsigned total = 0;

  [scanner scanUpToString: ch intoString: NULL];
  while ([scanner scanString: ch intoString: NULL] == YES)
   {
     total++;
     [scanner scanUpToString: ch intoString: NULL];
   }

  NSLog(@"total %d", total);

  RELEASE(pool);
  return 0;
}

Richard Frith-Macdonald <CaS>
Group Member
Tue 22 Apr 2003 12:52:34 PM UTC, original submission:  

I try to use NSScanner to count the number of '{', '}', '[', ']', '\n', etc.,
and the result is unexpected.
It happens specially for ']' and ')'.
Below is a test code.
Did I do something wrong ?
I also try scanString rather than scanUpToString, and it got worse.
Is there any  limitation for NSScanner, such as special character ?

Thanx.

Yen-Ju

#include <AppKit/AppKit.h>

int main(int argc, const char *argv[])
{
  NSAutoreleasePool *pool = [NSAutoreleasePool new];

  NSString *string = [NSString stringWithContentsOfFile: @"NSMatrix.m"];
  NSScanner *scanner = [NSScanner scannerWithString: string];
  NSString *ch = @"[";
  BOOL result = YES;
  unsigned location = 0;
  unsigned total = 0;

  while(result)
   {
     [scanner setScanLocation: location];
     result = [scanner scanUpToString: ch intoString: NULL];
     location = [scanner scanLocation];
     if (location >= [string length])
       break;
     location++;
     total++
   }

  NSLog(@"total %d", total);

  RELEASE(pool);
  return 0;
}

-Deleted Account- <yjchen>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one

Digest:
   bug dependencies.

 

CC list is empty

 

There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

Only logged-in users can vote.

 

Follow 5 latest changes.

Date Changed by Updated Field Previous Value => Replaced by
2005-05-22 FredKiefer Open/ClosedDeclined Closed
2004-08-24 CaS Assigned toCaS None
2003-05-16 CaS StatusNone None
2003-04-22 CaS Assigned toNone None
    Open/ClosedOpen None

Back to the top

Powered by Savane 3.13-0329.
Corresponding source code