|
Boost : |
From: Darin Adler (darin_at_[hidden])
Date: 2002-02-11 02:18:15
On 2/10/02 5:12 AM, "John Maddock" <John_Maddock_at_[hidden]> wrote:
> 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.
Seems like a nice thing to do. But I worry that a substring that knows how
to act as a read-only string is a bit fragile because it's easy to forget
that it becomes invalid if the string it's based on is deleted, and there's
no necessary connection between the lifetime of these objects.
In the string algorithms work I was doing (apologies for not yet finishing
this tiny library and getting it reviewed), I had decided to keep the
substring_location concept as an internal implementation detail.
There was no "treat the substring as a string" in the work that I did. The
main reason the substring_location class exists is to single out parts of a
string for modification, without requiring lots of functions with multiple
return values. What matters for the string algorithms is being able to
construct a substring_location easily given the kind of string position
values returned by functions such as std::string::find, and being able to
extract the string position values needed to call functions like
std::string::replace and std::string::erase.
On the other hand, my usage did require the separate state you propose
(whether you call it "bad", "invalid", "null", "nsubstring", or "npos") to
represent "not found" so that find functions don't need to return both a
boolean and a substring.
-- Darin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk