Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2004-01-24 19:20:36


On 1/21/04 8:53 PM, "Luke Stebbing" <ls_at_[hidden]> wrote:

> On Wed, 21 Jan 2004, "Daryle Walker" <darylew_at_[hidden]> wrote:
>> Are Boost.Regex objects and strings semantically equivalent?
>> ...
>> From what little I know of Regex, I think the answer is "no". (So keep
>> the constructor explicit.)
>
> I agree that from an interface perspective, that's the primary question, but
> I think the answer is "yes". Take a look at a string literal (char const*
> const literal, technically):
>
> "Hello world"
>
> Conceptually, this is a string. On the other hand:
>
> "He(\\w)+ world"
>
> Conceptually, this can be considered as either a string or a regular
> expression, depending on the context, and if a set of programmers were
> selected and shown that literal, I believe the overwhelming response would
> be "that's a regular expression".
>
> A careful reader will note that "Hello world" is also a regular expression,
> but a degenerate one (literal matching, no special characters) that we
> usually just call a string. The primary attribute that distinguishes regular
> expressions from stripped down const strings is where they are /used/, not
> what they are. This is emphasized by the fact that aside from
> locale/allocator information (which is associated with basic_string as well,
> but in a different way) and mark_count (which is an inspector function that
> gives information about parens grouping for a given regex), regex member
> functions form a proper subset of string member functions and are
> semantically identical.
>
> Context gives a regular expression meaning, and if the danger of expensive,
> accidental conversions is avoided (and I believe this is the case), implicit
> conversions should be allowed.

But the context of whether or not a string is a plain-old-string or really a
regular expression is determined by us. The compiler has no way of
determining that. This means that we either use strict discipline to
prevent mixing the contexts or use compiler facilities, like "explicit", to
help us. Unless you're the perfect programmer, you should use the compiler
to help when it can.

Also, I wouldn't consider a list of characters to be equivalent to a pattern
matcher, even if the latter is implemented in terms of the former. (Can a
regex be defined by other ways than a string? Using "regex atom" objects
composed together could cut out the mandatory parsing engine the string
method requires.)

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT hotmail DOT com

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