Re: Parsing string to key=>val pairs
Re: Parsing string to key=>val pairs
- Subject: Re: Parsing string to key=>val pairs
- From: Ricky Sharp <email@hidden>
- Date: Thu, 30 Jun 2005 18:47:26 -0500
On Jun 30, 2005, at 6:19 PM, Michael Carter wrote:
I've been trying to find the best way to parse a large string into
a dictionary with key=>value pairs taken from colon-delimited items
in the string. I've been messing with NSString's methods and
looking at NSScanner, however it seems like quite a lot of work
that way. The input is an x509 certificate, so there are about 12
key:value pairings that have various amounts of whitespace in-between.
Should I continue coaxing the string with NSString/NSScanner, or is
there a better way? The bare minimum I need to extract is shown in
bold, however I'd like to read in some other properties.
sample:
Certificate:
Data:
Version: 1 (0x0)
Serial Number: 160 (0xa0)
Signature Algorithm: md5WithRSAEncryption
Issuer: C=US, ST=California, L=Cupertino, O=Some Computer
Company, OU=Some Org unit, CN=A Root Cert/
emailAddress=email@hidden
Validity
Not Before: Jun 7 23:48:00 2005 GMT
Not After : Jun 7 23:48:00 2007 GMT
Subject: C=US, ST=California, L=Cupertino, O=Some Computer
Company, OU=Some Org unit, CN=server.somecompany.com
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public Key: (1024 bit)
Modulus (1024 bit):
Assuming that an atomic value cannot contain eolns, and that the eoln
character used is always the same (e.g. 0x0A) you could do this:
NSArray* theKeyValues = [theCertificateString
componentsSeparatedByString:@"\u000A"];
Then, iterate over the items in the array. For each item, do another
componentsSeparatedByString, but use the colon character. You'll
probably then want to strip whitespace on the resultant two strings
(key and value). Finally, put that key and value pair in your
dictionary.
It looks like you may have to special case 'Certificate' and 'Data'
as they don't have an atomic value assigned to them.
Whether this is the best method is an exercise for the reader.
Have you also looked into the possibility of converting this to XML?
Or do you need the data to end up in a dictionary?
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
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