String trimming throwing an exception

Discussion in 'C++' started by Adrian, Apr 10, 2007.

  1. Adrian

    Adrian Guest

    Can this code ever throw out_of_range or should the commented out code
    always be used?

    Is there any chance that std::string::npos+1 is bigger then
    std::string::npos

    Take str to be any allowable string value.

    #include <iostream>
    #include <string>

    int main(int argc, char *argv[])
    {
    std::string str;

    str.erase(0, str.find_first_not_of(" \t"));
    str.erase(str.find_last_not_of(" \t")+1, std::string::npos);

    /*
    str.erase(0, str.find_first_not_of(" \t"));
    std::string::size_type x=str.find_last_not_of(" \t");
    if(x!=std::string::npos)
    {
    str.erase(x+1, std::string::npos);
    }
    */
    return 0;
    }
     
    Adrian, Apr 10, 2007
    #1
    1. Advertising

  2. On 10 Apr, 21:09, "Adrian" <> wrote:
    > Can this code ever throw out_of_range or should the commented out code
    > always be used?
    >
    > Is there any chance that std::string::npos+1 is bigger then
    > std::string::npos


    npos is of type size_type and have the value -1, from what I can
    determine size_type is an unsigned type meaning that npos is the
    largest possible value that a variable of size_type can have. This
    means that npos + 1 can never be larger than npos.

    > Take str to be any allowable string value.
    >
    > #include <iostream>
    > #include <string>
    >
    > int main(int argc, char *argv[])
    > {
    > std::string str;
    >
    > str.erase(0, str.find_first_not_of(" \t"));
    > str.erase(str.find_last_not_of(" \t")+1, std::string::npos);


    So this will remove all but the first character if there are no tabs,
    you should probably use the code below.

    > /*
    > str.erase(0, str.find_first_not_of(" \t"));
    > std::string::size_type x=str.find_last_not_of(" \t");
    > if(x!=std::string::npos)
    > {
    > str.erase(x+1, std::string::npos);
    > }
    > */
    > return 0;
    >
    > }


    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Apr 11, 2007
    #2
    1. Advertising

  3. Adrian

    Adrian Guest

    On Apr 11, 12:30 am, "Erik Wikström" <>
    wrote:
    > npos is of type size_type and have the value -1, from what I can
    > determine size_type is an unsigned type meaning that npos is the
    > largest possible value that a variable of size_type can have. This
    > means that npos + 1 can never be larger than npos.


    So on a standard implementaion it will never throw

    >
    > > str.erase(0, str.find_first_not_of(" \t"));
    > > str.erase(str.find_last_not_of(" \t")+1, std::string::npos);

    >
    > So this will remove all but the first character if there are no tabs,
    > you should probably use the code below.

    The both remove the same about of white space :) check again.

    >
    > > /*
    > > str.erase(0, str.find_first_not_of(" \t"));
    > > std::string::size_type x=str.find_last_not_of(" \t");
    > > if(x!=std::string::npos)
    > > {
    > > str.erase(x+1, std::string::npos);
    > > }
    > > */
    > > return 0;
     
    Adrian, Apr 11, 2007
    #3
  4. Adrian

    James Kanze Guest

    On Apr 10, 9:09 pm, "Adrian" <> wrote:
    > Can this code ever throw out_of_range or should the commented out code
    > always be used?


    > Is there any chance that std::string::npos+1 is bigger then
    > std::string::npos


    Technically, I think so, but only if size_t is smaller than an
    int (and integral promotion means that the expression has a type
    larger than size_t). So I can't imagine an implementation where
    it would happen in practice. And of course, if the npos+1 is
    assigned to a size_t, it is guaranteed to be 0.

    > Take str to be any allowable string value.


    > #include <iostream>
    > #include <string>


    > int main(int argc, char *argv[])
    > {
    > std::string str;


    > str.erase(0, str.find_first_not_of(" \t"));
    > str.erase(str.find_last_not_of(" \t")+1, std::string::npos);


    Since the parameter type of erase, here, is size_t, you should
    be OK in every case.

    --
    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, Apr 12, 2007
    #4
  5. Adrian

    James Kanze Guest

    On Apr 11, 4:25 pm, "Adrian" <> wrote:
    > On Apr 11, 12:30 am, "Erik Wikström" <>
    > wrote:


    > > npos is of type size_type and have the value -1, from what I can
    > > determine size_type is an unsigned type meaning that npos is the
    > > largest possible value that a variable of size_type can have. This
    > > means that npos + 1 can never be larger than npos.


    > So on a standard implementaion it will never throw


    That's an interesting question, since the standard allows a lot
    of variance in implementations. I think that it could result in
    undefined behavior (and thus, anything, including throwing), but
    it would take a very, very strange implementation, which I don't
    think we'll ever see.

    Basically, the only time it could cause trouble is if INT_MAX ==
    USHRT_MAX and size_t were a typedef for unsigned short. Both
    conditions are, I think, permitted, but I can't imagine ever
    seeing them.

    Quite frankly, I wouldn't worry about this possibility.

    --
    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, Apr 12, 2007
    #5
  6. Adrian

    Adrian Guest

    On Apr 12, 2:56 am, "James Kanze" <> wrote:
    > That's an interesting question, since the standard allows a lot
    > of variance in implementations. I think that it could result in
    > undefined behavior (and thus, anything, including throwing), but
    > it would take a very, very strange implementation, which I don't
    > think we'll ever see.
    >
    > Basically, the only time it could cause trouble is if INT_MAX ==
    > USHRT_MAX and size_t were a typedef for unsigned short. Both
    > conditions are, I think, permitted, but I can't imagine ever
    > seeing them.
    >
    > Quite frankly, I wouldn't worry about this possibility.

    Thanks James, that makes sense to me.

    I never did worry about this :) until someone else questioned it so
    thought I should check.

    Adrian
     
    Adrian, Apr 12, 2007
    #6
    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. Steve C. Orr, MCSD

    Re: Trimming Blank Spaces in String

    Steve C. Orr, MCSD, Aug 9, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    473
  2. Scott Brady Drummonds

    std::string Destructor Throwing Exception

    Scott Brady Drummonds, Mar 23, 2005, in forum: C++
    Replies:
    3
    Views:
    3,422
    Andrey Tarasevich
    Mar 23, 2005
  3. Double Dumbass on You

    Trimming a string

    Double Dumbass on You, Aug 2, 2004, in forum: Python
    Replies:
    7
    Views:
    469
    Christopher T King
    Aug 6, 2004
  4. Generic Usenet Account

    Trimming leading and trailing spaces from a string

    Generic Usenet Account, Jul 8, 2005, in forum: C++
    Replies:
    6
    Views:
    390
    Paul Groke
    Jul 13, 2005
  5. Mike Copeland

    Trimming basic::string

    Mike Copeland, Apr 18, 2010, in forum: C++
    Replies:
    7
    Views:
    567
    Mike Copeland
    Apr 20, 2010
Loading...

Share This Page