|
Boost : |
Subject: Re: [boost] [xpressive] Is there a way to test for an empty regex?
From: Eric Niebler (eric_at_[hidden])
Date: 2008-11-20 02:57:46
Michael Goldshteyn wrote:
> "Eric Niebler" <eric_at_[hidden]> wrote in message
>>
>> if( 0 != re.regex_id() )
>>
>
> Thanks for the speedy reply. I did notice the postcondition on the
> constructor and the fact that the regex_id() will return 0 for a
> non-initialized regex, but was hoping that there was a more intuitive way to
> go about this test, since readers of the code may get confused without a
> comment. How hard would it be to add an empty() function with a signature
> that is similar to the one in boost::regex before Boost 1.38.0 comes out,
> since this functionality, at least in my humble opinion is very useful.
>
> The signature for the function in boost::regex is:
>
> bool empty() const;
boost::regex goes to some length to present itself as a souped up
container of characters. You can assign a character range to it, get
begin() and end() iterators for stepping through the characters with
which the regex was initialized, etc. Given that, see if you can answer
this without checking the docs:
assert(std::string().empty()); // OK
assert(std::string("").empty()); // OK
assert(boost::regex().empty()); // Is this true???
assert(boost::regex("").empty()); // How about this???
Unsurprisingly, regex::empty() is not part of the standard regex
interface in C++0x. I don't like regex::empty() and I'm not inclined to
add it. Sorry. You already have a way to get the information you're
interested in. If you would like to give it a pretty name, by all means...
template<class Iter>
bool is_invalid(xpressive::basic_regex<Iter> const &rex)
{
return 0 == rex.regex_id();
}
-- Eric Niebler BoostPro Computing http://www.boostpro.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk