Re: Splitting strings....
Re: Splitting strings....
- Subject: Re: Splitting strings....
- From: Jerry LeVan <email@hidden>
- Date: Sat, 28 Feb 2004 11:31:24 -0500
Well, I am trying to keep things simple, I have replaced:
listOfStatements = [sql componentsSeparatedByString:@";"];
with:
listOfStatements = [self parseInput:sql];
where
-(NSArray *) parseInput: (NSString *) statementString
{
NSString *sep = @"\001";
int length,i;
BOOL inQuote; // true when in a quoted string
NSMutableString *workingString;
NSRange replace;
workingString = [NSMutableString stringWithString:statementString];
length = [workingString length];
inQuote = NO;
for(i=0; i< length; i++)
{
if([workingString characterAtIndex:i] == '\'') {
inQuote = (inQuote)?NO:YES;
continue;
}
if( [workingString characterAtIndex:i] == ';' && !inQuote) {
replace = NSMakeRange(i,1);
[workingString replaceCharactersInRange:replace withString: sep];
}
}
//NSLog(@"Parse Results: %@", [workingString
componentsSeparatedByString:sep]);
return [workingString componentsSeparatedByString:sep] ;
}
Seems to be working....
--Jerry
On Feb 28, 2004, at 11:12 AM, Allan Odgaard wrote:
On 28. Feb 2004, at 15:46, Public Look wrote:
Scanning and parsing strings are two of the most mature disciplines
in Computer Science and there is no reason to ever write this type of
code the hard way again IMHO.
Although parsing is a mature theoretic discipline, I can think of a
few practical reasons why the OP would not want to do as you suggest:
flex generate switch/case-code rather than parser-tables, leading to
larger executables.
flex is for ASCII, not unicode, so at best the NSString should be
converted to UTF8.
flex, by default, reads from a FILE* (stdin), the OP needs to work on
a string (although flex actually use a macro to get the next byte,
which can be re-defined to something else).
flex generate code rather than data, so only one grammar can be used
pr. program (unless pre-processing the flex source to add
namespace-prefixes or similar).
flex generate code which use global variables, so the program would
not be re-entrant.
flex generate a parser for regular languages, so the OP would also
need to use bison to solve his task -- now writing a grammar for both
flex and bison and setting it up to solve his task starts to be more
complicated than the task itself.
etc.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.