|
Boost : |
From: Stewart, Robert (stewart_at_[hidden])
Date: 2002-02-14 13:09:44
From: John Maddock [mailto:John_Maddock_at_[hidden]]
> I'm going to go off on a slight tangent though because in
> several places we
> require ranges of strings (regex, tokeniser, and Darin Adler's string
> algorithms), there is as yet no standard way to represent them.
>
> regex uses something like a pair of iterators.
> tokeniser just copies the range into a std::string (but can easily be
> modified to use something else).
> DA's string algorithms use a kind of substring (but one
> that's inherently
> tied to std::basic_string).
>
> It seems to me that we've all been trying to avoid the issue
> as much as
> possible, so recently I've begun work on a substring class - one that
> represents any iterator range - but that "looks like" a string.
[snip]
> Roughly you can do anything with a substring that you can with a const
> std::string, except call c_str() or data().
For the cases you cite, a substring of this sort may be useful. However, in
general, a const substring will provide only a portion of the value of a .
To make substrings valuable, one should be able to modify the underlying
string. I used RWCString years ago, which provided a powerful RWCSubString
class. It allowed things like the following:
RWCString foo("The red fox jumped over the lazy dog");
foo(4,3) = "quick, brown";
std::cout << foo << endl;
The result is "The quick, brown fox...." Such a facility would be highly
useful. IIRC, RWCString used COW, so RWCSubString, the return type of the
function operator, could share the underlying string for const operations
and force a non-shared copy for non-const operations. That luxury is
clearly not available for std::basic_string and presents a design challenge.
However, I would be happy to live with the potential for the substring
outliving its string; that's no different than an iterator being
invalidated. You just have to know what's valid and what's undefined
behavior.
Rob
Susquehanna International Group, LLP
http://www.sig.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk