Re: Cleverly extending the std::string class

Discussion in 'C++' started by peter koch, Mar 19, 2009.

  1. peter koch

    peter koch Guest

    On 19 Mar., 19:58, Giuliano Bertoletti <> 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
    peter koch, Mar 19, 2009
    #1
    1. Advertising

  2. peter koch

    James Kanze Guest

    On Mar 19, 8:25 pm, peter koch <> wrote:
    > On 19 Mar., 19:58, Giuliano Bertoletti <> 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
    James Kanze, Mar 19, 2009
    #2
    1. Advertising

  3. peter koch

    James Kanze Guest

    On Mar 19, 9:37 pm, Giuliano Bertoletti <> 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
    James Kanze, Mar 19, 2009
    #3
  4. peter koch

    peter koch Guest

    On 19 Mar., 23:40, James Kanze <> wrote:
    > On Mar 19, 8:25 pm, peter koch <> wrote:
    >
    > > On 19 Mar., 19:58, Giuliano Bertoletti <> 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
    peter koch, Mar 19, 2009
    #4
  5. peter koch

    James Kanze Guest

    On Mar 20, 12:04 am, peter koch <> wrote:
    > On 19 Mar., 23:40, James Kanze <> wrote:


    > > On Mar 19, 8:25 pm, peter koch <> wrote:


    > > > On 19 Mar., 19:58, Giuliano Bertoletti <> 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
    James Kanze, Mar 20, 2009
    #5
  6. peter koch

    Jorgen Grahn Guest

    On Thu, 19 Mar 2009 15:42:30 -0700 (PDT), James Kanze <> wrote:
    > On Mar 19, 9:37 pm, Giuliano Bertoletti <> 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!
    Jorgen Grahn, Mar 23, 2009
    #6
  7. peter koch

    James Kanze Guest

    On Mar 23, 4:13 pm, Jorgen Grahn <> wrote:
    > On Thu, 19 Mar 2009 15:42:30 -0700 (PDT), James Kanze
    > <> wrote:
    > > On Mar 19, 9:37 pm, Giuliano Bertoletti <> 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
    James Kanze, Mar 24, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,261
    Ivan Vecerina
    Mar 17, 2005
  2. Fei Liu
    Replies:
    9
    Views:
    431
  3. Jeffrey Walton
    Replies:
    10
    Views:
    927
    Mathias Gaunard
    Nov 26, 2006
  4. James Kanze
    Replies:
    1
    Views:
    344
    James Kanze
    Mar 20, 2009
  5. Lars Schouw

    extending std::string

    Lars Schouw, Apr 7, 2010, in forum: C++
    Replies:
    9
    Views:
    1,429
    Lars Schouw
    Apr 11, 2010
Loading...

Share This Page