Boost logo

Boost :

Subject: Re: [boost] [RFC] string inserter/extractor "q u o t i n g"
From: Eric Niebler (eric_at_[hidden])
Date: 2010-06-23 11:01:42

On 6/23/2010 6:12 AM, Daniel James wrote:
> On 23 June 2010 03:49, Rob Stewart <robertstewart_at_[hidden]> wrote:
>> I quickly chose to throw std::logic_error from unquoted() when it fails to
>> find a closing delimiter. There may well be a better approach; feel free to
>> suggest alternatives.
> You should inherit from std::runtime_error, since that would be
> usually caused by bad input rather than programmer error.

Bad input can be a programmer error too. Here's how you decide:

- Decide if passing a malformed string is a violation of unquote's
- If it is, throw something derived from logic_error.
- If not, handle it gracefully or, if you cannot, throw something
derived from runtime_error.

Users can (in theory) check that the string is well-formed before
calling unquote, but to do so they would essentially have to implement
unquote themselves. So making it a precondition that the input is
well-formed seems onerous in this case. I would not make it a
precondition; instead, I would accept it as valid input and document that.

For valid input, I would prefer if unquote found a way to muddle through
and do something reasonable. Throwing an exception when a trailing quote
is missing seems like smacking someone's hand when they forgot to say
"mother may I?" Why not just accept it? And document that fact! If the
string is malformed in a way that you really can't muddle on, then throw
something derived from runtime_error.

> To be honest, I don't see the value of this. As this is the kind of
> thing which is handled well in other ways (e.g. using a parser or
> lexer generator, or a standard data format such as XML, JSON etc.).
> There tends to be odd differences in quoting, encoding and escaping
> styles making a generic function awkward. It's not as specific as a
> filename extractor and not as generic as a parser and it's not clear
> why there's a need for something in between.

Parser and lexer generators are complicated beasts, and I wouldn't send
a novice programmer in that direction if it could be helped. Most
quoting/unquoting is very straightforward. There's a quote character and
an escape character. For anything more complicated, yes, build your own.

Eric Niebler
BoostPro Computing

Boost list run by bdawes at, gregod at, cpdaniel at, john at