Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Cleverly extending the std::string class

Reply
Thread Tools

Re: Cleverly extending the std::string class

 
 
peter koch
Guest
Posts: n/a
 
      03-19-2009
On 19 Mar., 19:58, Giuliano Bertoletti <gbe32...@libero.it> wrote:
> Hello,
>
> just wondering if there's a clever way to extend the functionality of
> std::string (without using boost).
>
> Newer programming languages like python do have a lot of functionality
> associated with strings, which may be handy in C++.
>
> For example lowercasing and uppercasing the string.
> Extracting the last element by subscripting -1, or the last say four
> chars using [-4:-1] are also desirable.
>
> These functionalities could be accomodated by proper function calls.
>
> If I derive a class from std::string I have all the std::string member
> functions return the base class and not the derived class.
>
> So for example I would have to write:
>
> std::string p = "test";
> std::string q = ((MYSTRING)p).lower()
>
> while I would prefer to work only with MYSTRING.
>
> MYSTRING p;
> MYSTRING q = p.lower();
>
> but then if I call:
>
> q = p.substr(0,4)
>
> substr returns an std::string and not a MYSTRING.
>
> Is this the wrong place where to use inheritance ?
>
> Regards,
> Giuliano Bertoletti.


What is your problem with using free functions instead - e.g. void
tolower(std::string& string) or std::string substring(std:string
const& s, int begin, int end)?

/Peter
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      03-19-2009
On Mar 19, 8:25 pm, peter koch <peter.koch.lar...@gmail.com> wrote:
> On 19 Mar., 19:58, Giuliano Bertoletti <gbe32...@libero.it> wrote:


> What is your problem with using free functions instead - e.g.
> void tolower(std::string& string) or std::string
> substring(std:string const& s, int begin, int end)?


Just don't forget that these functions may require a locale as
argument as well.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      03-19-2009
On Mar 19, 9:37 pm, Giuliano Bertoletti <gbe32...@libero.it> wrote:
> peter koch ha scritto:
> > What is your problem with using free functions instead -
> > e.g. void tolower(std::string& string) or std::string
> > substring(std:string const& s, int begin, int end)?


> It tends to make the code less readable.


That depends.

> For example:


> std::vector<std::string> v = split( tolower( p ), "," )


> while I would prefer to write:


> std::vector<std::string> v = p.tolower().split(",");


> Notice that functions do not alter the object itself but
> return an altered object.


In other words, the object you have in mind is totally unrelated
to std::string. (I too find that a better design, and it
corresponds to how my pre-standard String class worked. But it
is NOT std::string.)

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      03-19-2009
On 19 Mar., 23:40, James Kanze <james.ka...@gmail.com> wrote:
> On Mar 19, 8:25 pm, peter koch <peter.koch.lar...@gmail.com> wrote:
>
> > On 19 Mar., 19:58, Giuliano Bertoletti <gbe32...@libero.it> wrote:
> > What is your problem with using free functions instead - e.g.
> > void tolower(std::string& string) or std::string
> > substring(std:string const& s, int begin, int end)?

>
> Just don't forget that these functions may require a locale as
> argument as well.


Probably. It really depends on what "tolower" is meant to do. If real
text-processing is involved, there is no doubt that it is required. On
the other hand, if - as is common - it is used with the purpose of
parsing simple ascii texts (compilers and initialisation files as an
example), there is no need for that. I agree that in that case,
calling the function for "lowercase" is an exaggeration.

/Peter
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-20-2009
On Mar 20, 12:04 am, peter koch <peter.koch.lar...@gmail.com> wrote:
> On 19 Mar., 23:40, James Kanze <james.ka...@gmail.com> wrote:


> > On Mar 19, 8:25 pm, peter koch <peter.koch.lar...@gmail.com> wrote:


> > > On 19 Mar., 19:58, Giuliano Bertoletti <gbe32...@libero.it> wrote:
> > > What is your problem with using free functions instead -
> > > e.g. void tolower(std::string& string) or std::string
> > > substring(std:string const& s, int begin, int end)?


> > Just don't forget that these functions may require a locale
> > as argument as well.


> Probably. It really depends on what "tolower" is meant to do.


That really is the question, isn't it. The answer is far from
trivial.

> If real text-processing is involved, there is no doubt that it
> is required. On the other hand, if - as is common - it is used
> with the purpose of parsing simple ascii texts (compilers and
> initialisation files as an example), there is no need for
> that.


It depends on what those files support. The C family of
languages (and Unix) punts on the question, by making case
significant---not necessarily a good idea for the user, but it
certainly avoids questions along the lines of whether ä compares
equal to A (or even Ae, in Swiss German).

> I agree that in that case, calling the function for
> "lowercase" is an exaggeration.


Either that, or calling the class String is wrong---it's
InitializationFileText, or something like that.

Practically, of course, it's almost certainly acceptable to just
use std::string, and to call the function toLower, provide it is
in a namespace which is designed to process such files; the
namespace indicates what we mean by the function.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      03-23-2009
On Thu, 19 Mar 2009 15:42:30 -0700 (PDT), James Kanze <> wrote:
> On Mar 19, 9:37 pm, Giuliano Bertoletti <gbe32...@libero.it> wrote:
>> peter koch ha scritto:
>> > What is your problem with using free functions instead -
>> > e.g. void tolower(std::string& string) or std::string
>> > substring(std:string const& s, int begin, int end)?

>
>> It tends to make the code less readable.

>
> That depends.


You could argue that free functions are more readable because readers
familiar with std::string know that the object itself represents and
can do. All they need to learn is what tolower() and substring() do,
and they can use them directly in their std::string-based code.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-24-2009
On Mar 23, 4:13 pm, Jorgen Grahn <grahn+n...@snipabacken.se> wrote:
> On Thu, 19 Mar 2009 15:42:30 -0700 (PDT), James Kanze
> <james.ka...@gmail.com> wrote:
> > On Mar 19, 9:37 pm, Giuliano Bertoletti <gbe32...@libero.it> wrote:
> >> peter koch ha scritto:
> >> > What is your problem with using free functions instead -
> >> > e.g. void tolower(std::string& string) or std::string
> >> > substring(std:string const& s, int begin, int end)?


> >> It tends to make the code less readable.


> > That depends.


> You could argue that free functions are more readable because
> readers familiar with std::string know that the object itself
> represents and can do. All they need to learn is what
> tolower() and substring() do, and they can use them directly
> in their std::string-based code.


One could. More generally, I don't think that there's a
fundamental difference in readability between the two syntaxes,
"a.f()" and "f( a )". Any difference is cultural or due to
habits. On the other hand, mixing the two, i.e. having, say
"a.tolower()" but "toupper( a )", would certainly doesn't help
readability. On the third hand, there are different "levels"; I
don't see any real problem with s.replace() but toupper( s ),
for example: functions which manipulate the underlying bytes,
without consideration of the encoding, are members; functions
which consider the semantics of those bytes, as characters, are
free functions.

One could also argue for a Text class, using std::string in its
implementation, but also containing information concerning the
encoding and the locale. This could lead to some interesting
questions: concatenating a Text with ISO 8859-1 encoding and one
with ISO 8859-5 encoding might result in a Text with UTF-8
encoding, but what happens when you concatenate a Text in French
locale with one in Swedish or Turkish. (In French, ä collates
with a, and the upper case of i is I. In Swedish, ä collates
immediately after z, and in Turkish, the upper case of i is
\u0130.)

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Cleverly extending the std::string class James Kanze C++ 1 03-20-2009 08:35 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Extending class to be a base class *properly* Matthias Kaeppler C++ 1 04-22-2005 06:05 AM
maps and class types: extending a class factory Simon Elliott C++ 0 01-11-2005 01:02 PM



Advertisments