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
Priority:  5 - Normal Item Group:  Bug
Status:  Invalid Privacy:  Public
Assigned to:  None Open/Closed:  Closed
* Mandatory Fields

Post a Comment

Add a New Comment Rich Markup
   

Discussion

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>

 

Attached Files

This item currently has no attached files.

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

Attach Files:
   
   
Comment:
   

 

Dependencies

This item does not depend on any other items.

Items that depend on this one

Digest:
   bug dependencies.

 

Mail Notification Carbon-Copy List

 

Votes

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.

 

History

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.16-ed84.
Corresponding source code