Re: Question about an NSScanner loop
Re: Question about an NSScanner loop
- Subject: Re: Question about an NSScanner loop
- From: Guy English <email@hidden>
- Date: Wed, 12 Jan 2005 17:24:06 -0500
Well the major problem is that you're allocating an NSScanner for
every character in the input string. You're also making an
autoreleased character set and that's gonna end up adding up.
NSScanner is stateful - it'll remember where in the input string it is
so you don't need make one all the time. See the docs for
scanLocation, etc.
Here's what I'd try ( written in mail, etc )
- (NSArray*) parseCSV: (NSString*) inputString
{
NSMutableArray *cells;
NSScanner *scanner;
NSCharacterSet *stopCharacters;
NSCharacterSet *allowedCharacters;
cells = [[[NSMutableArray alloc] init] autorelease];
scanner = [NSScanner scannerWithString: inputString];
stopCharacters = [NSCharacterSet
characterSetWithCharactersInString: @",\n"];
allowedCharacters = [NSCharacterSet alphanumericCharacterSet];
while ( [scanner isAtEnd] == NO )
{
NSString *thisCell;
// read in a string ...
[scanner scanCharactersInSet: allowedCharacters intoString: &thisCell];
// ... add it to the array ...
[cells addObject: thisCell];
// ... and scan past the delimiters ( scanning into nil just ignores )
[scanner scanCharactersInSet: stopCharacters intoString: nil];
}
return cells;
}
On Wed, 12 Jan 2005 21:48:50 +0000, Matt Crocker <email@hidden> wrote:
> 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
>
>
_______________________________________________
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