• 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: Splitting strings....
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Splitting strings....


  • Subject: Re: Splitting strings....
  • From: Allan Odgaard <email@hidden>
  • Date: Sat, 28 Feb 2004 17:32:32 +0100

On 28. Feb 2004, at 2:18, Jerry LeVan wrote:

I have suddenly developed a need to be able to split a NSString on a ";"
The rub comes in that the semicolon does not count if it is *in* a quoted string.

Also a string can contain a quote character if it is doubled...

Did you make up this latter rule yourself? It complicates things, because when you meet a quote inside a string, you will need to look at the next character to learn how to interpret it, but you might be at the end of the buffer.

Imagine we instead use escape (as most others), then the code would be (typed into Mail, so there might be oversights):

unichar* skip_string (unichar* first, unichar* last)
{
bool escape = false;
for(++first; first != last; ++first)
{
if(!escape && *first == '\'')
break;
escape = !escape && *first == '\\';
}
}

NSArray* split_string (NSString* str)
{
NSMutableArray* res = [NSMutableArray array];

unsigned len = [str length];
unichar* buf = malloc(sizeof(unichar) * len);
unichar* first = buf;
unichar* last = buf + len;

unichar* bos = first;
while(first != last)
{
if(*first == '\'')
first = skip_string(first, last);
else if(*first == ';')
{
[res addObject:[NSString stringWithCharacters:bos length:first - bos]];
bos = ++first;
}
else
++first;
}

if(bos != last)
[res addObject:[NSString stringWithCharacters:bos length:last - bos]];

free(buf);
return array;
}

Shameless plug: if you want to simplify things, check out CocoaSTL, it allows to iterate a string like this:

foreach(it, beginof(str), endof(str))
{
if(*it == ';')
...
}

And in fact, it does the same for NSArray, NSData, NSIndexSet, NSDictionary and NSSet (and also allows assignment to these through insert iterators) -- allowing you to write generic code, i.e. in the above you would remove the explicit calls to NSString, replace 'unichar*' with T (as a template parameter), and you have a function which can split C strings, NSData contents, NSStrings, std::string or whatever :)

http://www.top-house.dk/~aae0030/cocoastl/
_______________________________________________
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.


  • Follow-Ups:
    • Re: Splitting strings....
      • From: Allan Odgaard <email@hidden>
References: 
 >Splitting strings.... (From: Jerry LeVan <email@hidden>)

  • Prev by Date: Re: Splitting strings....
  • Next by Date: Re: US / British English Localisations
  • Previous by thread: Re: Splitting strings....
  • Next by thread: Re: Splitting strings....
  • Index(es):
    • Date
    • Thread