Boost logo

Boost :

Subject: Re: [boost] [xpressive] Is there a way to test for an empty regex?
From: Michael Goldshteyn (mgoldshteyn_at_[hidden])
Date: 2008-11-20 10:14:10


"Eric Niebler" <eric_at_[hidden]> wrote in message
news:4925187A.6080402_at_boost-consulting.com...
> 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???

Yes, the behavior of the last assert is surprising, so I do see your point.
"empty" is certainly not the right name for such a function, at least in the
context of standard container functionality.

>
> 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();
> }

empty() may not a good name for this function, but
agree with me that encapsulating the functionality of testing whether an re
object actually holds a regular-expression should be more intuitive than:

0 == rex.regex_id(); // Test if rex actually contains a regular expression

Perhaps something like unfilled(), bare(), or as you suggested is_invalid()
should be added to the actual implementation, instead of user code? I would
further argue that something so trivial and tightly coupled to the
implementation
(i.e., regex_id() being zero) should be a member function and not stand
alone.

Opinions on this topic from others would also be greatly appreciated!

Thanks,

Michael Goldshteyn


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk