RE: Question about an NSScanner loop
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];
}
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];/color>
NSString *cellString = nil/color>;
int/color>
inputIndex = 0/color>;
while/color>
(inputIndex < [inputString length])
{
NSScanner *cellScanner =
[NSScanner scannerWithString:[inputString
substringWithRange:NSMakeRange(inputIndex, [inputString
length]-inputIndex)]];
[cellScanner
scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@",\n"/color>]
intoString:&cellString];
[cellScanner release];
inputIndex =
inputIndex + [cellString length] +
1/color>;
if/color>
([cellString characterAtIndex:[cellString length]-1/color>] == '\n'/color>)
{
cellString = [cellString
substringWithRange:NSMakeRange(0/color>,
[cellString length]-1/color>)];
}
if/color>([inputString
characterAtIndex:inputIndex - 1/color>] == ','/color>)
{
//[currentLine
addObject:cellString];/color>
cellString = nil/color>;
}
else/color>
if/color>([inputString
characterAtIndex:inputIndex - 1/color>] == '\n'/color>)
{
//[currentLine
addObject:cellString];/color>
cellString = nil/color>;
//[outputArray
addObject:[[currentLine mutableCopy] autorelease]];/color>
//[currentLine
removeAllObjects];/color>
}
}
return/color>
outputArray;
}
/bigger>
_______________________________________________
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