Boost logo

Boost :

From: Alexei Novakov (alexei_novakov_at_[hidden])
Date: 2002-11-20 10:31:41


"Vincent Finn" <vincent_finn_at_[hidden]> wrote in message
news:3DDB6C2E.9090809_at_agilent.com...
> Forwarded to main Boost list - that's the more appropriate venue for
> discussions of possible additions.
>
> -- Jim Hyslop boost-users moderator.
>
> > -----Original Message-----
> > From: alexei_novakov [mailto:alexei_novakov_at_[hidden]]
> > Sent: Monday, November 18, 2002 7:05 PM
> > To: Boost-Users_at_[hidden]
> > Subject: [Boost-Users] Possible boost addition: sub string and const
> > string.
> >
> >
> > Hello everyone.
> >
> > I have two classes which I found pretty handy: sub_string (behaves as
> > a mirror of the portion of master basic_string) and const_string (C-
> > string wrapper). Nice thing about these two is they implemented as
> > template specialization of basic_string which has advantages:
> > a) familiar interface;
> > b) possibility to reuse the code written for basic_string (like
> > string streams, lexical casts, etc).
> >
> > Any interest?
> >
> > Regards.
> >
> > Alexei Novakov
>
> Sounds interesting, like slice on a valarray
>
> When I have to do a lot of manipulation of sub string and can't afford
> to copy back and forward I normally resort to using vector<char>; this
> sounds a lot handier
>
> How does it work ?
>
> Vin
>

The idea is simple. Sub string is declared as basic_string template
specialization:

template <typename CHAR, typename TRAITS, typename ALLOCATOR>
class basic_string<CHAR, TRAITS, basic_string<CHAR, TRAITS, ALLOCATOR> >

The interfase is the same as for basic_string (except constructors). Sub
string instances contain reference to master string and boundaries (start
and size). One can use it like this:

// Start
typedef basic_string<string::value_type, string::traits_type, string>
sub_string;

string str("1234567890");
sub_string ss(str, 2, 5); // master string, start position, size

assert(lexical_cast<int>(ss) == 34567);

ss = "$$";

assert(str == "12$$890");
// End

All the basic_string operators (like +, ==, !=, <, >, <=, >=) are overloaded
to be used for both strings and sub strings.

Similar approach is used for const_string (C string wrapper), but only const
methods of basic_string are implemented.

Alexei.


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