• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
RE: Question about an NSScanner loop
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Question about an NSScanner loop


  • Subject: RE: Question about an NSScanner loop
  • From: "Jonathan E. Jackel" <email@hidden>
  • Date: Wed, 12 Jan 2005 17:38:15 -0500
  • Importance: Normal

You would crash because you release cellscanner even though you don't own it -- if you ever got out of your endless loop, as joar points out.  +scannerWithString: returns a scanner that you should not release unless you retain it.
 
The typical NSScanner loop for a 2D array would have an outer and an inner loop.  The outer loop would read in a line of input.  The inner loop would divide the line into records.  Something like this:
 
NSMutableArray *theArray = [NSMutableArray array];
NSScanner *mainScanner = [NSScanner scannerWithString:input];
NSString *temp;
while(![mainScanner isAtEnd])
{
    NSMutableArray *aRecord = [NSMutableArray array];
    [mainScanner scanUpToString:@"\n" intoString:&temp];
    NSScanner *tempScanner = [NSScanner scannerWithString:temp];
    while(![tempScanner isAtEnd])
    {
        //process each field then
        [aRecord addObject:theField];
    }
    [theArray addObject:aRecord];
    [mainScanner scanString:@"\n" intoString:nil];
}
-----Original Message-----
From: cocoa-dev-bounces+jonathan=email@hidden [mailto:cocoa-dev-bounces+jonathan=email@hidden]On Behalf Of Matt Crocker
Sent: Wednesday, January 12, 2005 4:49 PM
To: email@hidden
Subject: Question about an NSScanner loop

Hi folks,

It's been ages since I've had to ask a question on here, so I must be making progress...

My problem is with a small method that parses a CSV file (already loaded in with "stringWithContentsOfFile") and outputs a 2D array (i.e array of array) of the file's contents as NSStrings. The reason I'm doing this rather than a simple "componentsSeparatedByStrings" is that the CSV file has a few complications such as quotes that need to be handled specially.

The code I've attached runs fine, but eats several hundred megabytes of memory, even when handling an input file of around 60kB. I've been scratching my head over this for days, which probably means it's something blindingly obvious (and you can all have a good laugh at me) but I can't spot it. Note that I've commented out the actions that add the data to the arrays just to demonstrate that the memory problem isn't in there.

That means it's the NSString or the NSScanner...?

Any help much appreciated!

-(NSMutableArray *)parseCSV:(NSString*)inputString
{

NSMutableArray *outputArray = [NSMutableArray array];
//NSMutableArray *currentLine = [NSMutableArray array];
NSString *cellString = nil;

int inputIndex = 0;

while (inputIndex < [inputString length])
{
NSScanner *cellScanner = [NSScanner scannerWithString:[inputString substringWithRange:NSMakeRange(inputIndex, [inputString length]-inputIndex)]];
[cellScanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@",\n"] intoString:&cellString];
[cellScanner release];
inputIndex = inputIndex + [cellString length] + 1;
if ([cellString characterAtIndex:[cellString length]-1] == '\n')
{
cellString = [cellString substringWithRange:NSMakeRange(0, [cellString length]-1)];
}

if([inputString characterAtIndex:inputIndex - 1] == ',')
{
//[currentLine addObject:cellString];
cellString = nil;
}
else if([inputString characterAtIndex:inputIndex - 1] == '\n')
{
//[currentLine addObject:cellString];
cellString = nil;
//[outputArray addObject:[[currentLine mutableCopy] autorelease]];
//[currentLine removeAllObjects];
}
}

return outputArray;
}
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Question about an NSScanner loop
      • From: j o a r <email@hidden>
References: 
 >Question about an NSScanner loop (From: Matt Crocker <email@hidden>)

  • Prev by Date: Smooth Reordering TableView
  • Next by Date: Re: NSTask
  • Previous by thread: Re: Question about an NSScanner loop
  • Next by thread: Re: Question about an NSScanner loop
  • Index(es):
    • Date
    • Thread