Can you make this parsing key value pairs snippet better

D

Diwa

I have created the following code snippet for parsing name value
pairs. Is this ideal?

-------------------------------------------------------------------------------------
typedef std::map < std::string, std::string > MapNameValT ;

void parseNameValPairs(const std::string & sInp, MapNameValT &
mapOut,
char cNameValDelim, char cFieldDelim)
{
std::istringstream iss(sInp);
std::string sName, sVal;
while( std::getline(iss, sName, cNameValDelim) )
{
std::getline(iss, sVal, cFieldDelim);
mapOut[sName] = sVal;
}
}
-------------------------------------------------------------------------------------

The above code passed all of the below tests:

("8=FIX.4.1,9=302,35=8,49=INET,56=WED2R8,11=,43=N,", '=', ',')); //
Tag 11 is empty
("8=FIX.4.1,9=302,35=8,49=INET,56=WED2R8,11=,43=N", '=', ',')); //
No delim at end
("8=FIX.4.1=9=302=35=8=49=INET=56=WED2R8=11==43=N", '=', '=')); //
Same delim for field and for name/value
("tname^ANO^Aoid^A7.a^Aouser^Adiwakar^Atif^ADAY^A", '\001',
'\001')); // \001 is delim

The ^A in last test case is '\001'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top