Question about an NSScanner loop
Question about an NSScanner loop
- Subject: Question about an NSScanner loop
- From: Matt Crocker <email@hidden>
- Date: Wed, 12 Jan 2005 21:48:50 +0000
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