Is std::string::npos portably incrementable?

Discussion in 'C++' started by Marcus Kwok, Jun 29, 2007.

  1. Marcus Kwok

    Marcus Kwok Guest

    Is std::string::npos portably able to be incremented?

    For example, I want to insert some text into a string. If a certain
    character is found, I want to insert this text immediately after this
    character; otherwise I insert at the beginning of the string. On my
    implementation string::npos has the value of (string::size_type)-1, so
    incrementing it will make it 0, but can I rely on it?

    Also, say that the character occurs at the end of the string. Is it
    valid to specify the insert position as one greater than this value?

    Example usage of what I want to do is shown in fix_path(). Everything
    behaves as I would expect on my implementation (VS 2005).


    #include <iostream>
    #include <string>

    void fix_path(std::string& s)
    {
    using std::string;

    string to_insert = "goes/";
    string::size_type pos = s.rfind('/');
    s.insert(pos + 1, to_insert);
    }

    void test(const std::string& s)
    {
    using std::cout;
    using std::string;

    string t(s);
    cout << t << '\n';
    fix_path(t);
    cout << t << "\n\n";
    }

    int main()
    {
    using std::cout;
    using std::string;

    test("my/stuff/here");
    test("here");
    test("another/");
    test("/beginning");
    }


    /*
    Output:
    my/stuff/here
    my/stuff/goes/here

    here
    goes/here

    another/
    another/goes/

    /beginning
    /goes/beginning
    */

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Jun 29, 2007
    #1
    1. Advertising

  2. Marcus Kwok

    Fei Liu Guest

    Marcus Kwok wrote:
    > Is std::string::npos portably able to be incremented?
    >
    > For example, I want to insert some text into a string. If a certain
    > character is found, I want to insert this text immediately after this
    > character; otherwise I insert at the beginning of the string. On my
    > implementation string::npos has the value of (string::size_type)-1, so
    > incrementing it will make it 0, but can I rely on it?
    >
    > Also, say that the character occurs at the end of the string. Is it
    > valid to specify the insert position as one greater than this value?
    >
    > Example usage of what I want to do is shown in fix_path(). Everything
    > behaves as I would expect on my implementation (VS 2005).
    >
    >
    > #include <iostream>
    > #include <string>
    >
    > void fix_path(std::string& s)
    > {
    > using std::string;
    >
    > string to_insert = "goes/";
    > string::size_type pos = s.rfind('/');
    > s.insert(pos + 1, to_insert);
    > }
    >
    > void test(const std::string& s)
    > {
    > using std::cout;
    > using std::string;
    >
    > string t(s);
    > cout << t << '\n';
    > fix_path(t);
    > cout << t << "\n\n";
    > }
    >
    > int main()
    > {
    > using std::cout;
    > using std::string;
    >
    > test("my/stuff/here");
    > test("here");
    > test("another/");
    > test("/beginning");
    > }
    >
    >
    > /*
    > Output:
    > my/stuff/here
    > my/stuff/goes/here
    >
    > here
    > goes/here
    >
    > another/
    > another/goes/
    >
    > /beginning
    > /goes/beginning
    > */
    >

    Would boost::regex_replace be a better choice in this case?

    Fei
    Fei Liu, Jun 29, 2007
    #2
    1. Advertising

  3. Marcus Kwok

    Marcus Kwok Guest

    Fei Liu <> wrote:
    > Marcus Kwok wrote:
    >> void fix_path(std::string& s)
    >> {
    >> using std::string;
    >>
    >> string to_insert = "goes/";
    >> string::size_type pos = s.rfind('/');
    >> s.insert(pos + 1, to_insert);
    >> }

    >
    > Would boost::regex_replace be a better choice in this case?


    Looking at the documentation for it, it seems it only gives you the
    option to replace all occurrences or only the first occurrence; however
    I am not replacing, but inserting, and I am inserting at the last
    occurrence.

    Also, I can not guarantee that Boost will necessarily be available for
    the people using this, especially since the Standard Library covers this
    usage.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Jun 29, 2007
    #3
  4. On Fri, 29 Jun 2007 15:14:34 +0000, Marcus Kwok wrote:

    > Is std::string::npos portably able to be incremented?
    >
    > For example, I want to insert some text into a string. If a certain
    > character is found, I want to insert this text immediately after this
    > character; otherwise I insert at the beginning of the string. On my
    > implementation string::npos has the value of (string::size_type)-1, so
    > incrementing it will make it 0, but can I rely on it?
    >


    Most likely yes, since the standard defines 'npos' as:
    static const size_type npos = -1;

    And 'size_type' is:
    typedef typename Allocator::size_type size_type;

    --
    Obnoxious User
    Obnoxious User, Jun 29, 2007
    #4
  5. Marcus Kwok wrote:
    > Is std::string::npos portably able to be incremented?


    By definition, npos is 'size_type' and has the value -1. See 21.3/6.

    > For example, I want to insert some text into a string. If a certain
    > character is found, I want to insert this text immediately after this
    > character; otherwise I insert at the beginning of the string. On my
    > implementation string::npos has the value of (string::size_type)-1


    It is required to be that on all implementations.

    >, so
    > incrementing it will make it 0, but can I rely on it?


    I believe so.

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 29, 2007
    #5
  6. Marcus Kwok wrote:
    > Victor Bazarov <> wrote:
    >> Marcus Kwok wrote:
    >>> , so
    >>> incrementing it will make it 0, but can I rely on it?

    >> I believe so.

    >
    > Thanks. Do know the answer to my other question (whether or not it is
    > well-defined to specify s.size() as the insert position for string)?
    >


    It's well defined.

    john
    John Harrison, Jun 29, 2007
    #6
  7. Marcus Kwok

    Marcus Kwok Guest

    Victor Bazarov <> wrote:
    > Marcus Kwok wrote:
    >>, so
    >> incrementing it will make it 0, but can I rely on it?

    >
    > I believe so.


    Thanks. Do know the answer to my other question (whether or not it is
    well-defined to specify s.size() as the insert position for string)?

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Jun 29, 2007
    #7
  8. Marcus Kwok

    Marcus Kwok Guest

    John Harrison <> wrote:
    > Marcus Kwok wrote:
    >> Thanks. Do know the answer to my other question (whether or not it is
    >> well-defined to specify s.size() as the insert position for string)?

    >
    > It's well defined.


    Great, thanks.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Jun 29, 2007
    #8
  9. Marcus Kwok

    James Kanze Guest

    On Jun 29, 5:14 pm, (Marcus Kwok) wrote:
    > Is std::string::npos portably able to be incremented?


    The obvious answer is that it's a constant, and you can't
    increment a constant. But from the rest of your post, I gather
    that what you really want to know is whether npos + 1 is
    guaranteed to be zero.

    Technically, the answer is no. npos is guaranteed to have the
    value (size_t)(-1), and size_t is guaranteed to be an unsigned
    type, so the value would be guaranteed to be 0, unless integral
    promotion occurs. However:

    -- I've never heard of an implementation where size_t was
    smaller than an unsigned int, so integral promotion won't
    occur, and

    -- even if integral promotion occurs, if you immediately
    reconvert the results back to a size_t, you're guaranteed to
    end up with 0.

    So in practice, I think you can count on it.

    --
    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, Jun 30, 2007
    #9
  10. Marcus Kwok

    Marcus Kwok Guest

    James Kanze <> wrote:
    > On Jun 29, 5:14 pm, (Marcus Kwok) wrote:
    > -- even if integral promotion occurs, if you immediately
    > reconvert the results back to a size_t, you're guaranteed to
    > end up with 0.
    >
    > So in practice, I think you can count on it.


    Thanks for the confirmation. I am just using it as the parameter for
    string::insert(), which is of size_type, so I should be fine.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Jul 2, 2007
    #10
    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. Replies:
    8
    Views:
    3,104
  2. Marcus Kwok
    Replies:
    7
    Views:
    415
    Duane Hebert
    Jan 27, 2006
  3. Adrian
    Replies:
    7
    Views:
    428
    Adrian
    Jul 12, 2007
  4. Christopher Pisz
    Replies:
    11
    Views:
    528
    James Kanze
    Jan 13, 2008
  5. Hendrik Schober
    Replies:
    11
    Views:
    870
    Hendrik Schober
    Sep 24, 2008
Loading...

Share This Page