Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is std::string::npos portably incrementable?

Reply
Thread Tools

Is std::string::npos portably incrementable?

 
 
Marcus Kwok
Guest
Posts: n/a
 
      06-29-2007
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
 
Reply With Quote
 
 
 
 
Fei Liu
Guest
Posts: n/a
 
      06-29-2007
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
 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      06-29-2007
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
 
Reply With Quote
 
Obnoxious User
Guest
Posts: n/a
 
      06-29-2007
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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-29-2007
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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      06-29-2007
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
 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      06-29-2007
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
 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      06-29-2007
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-30-2007
On Jun 29, 5:14 pm, ricec...@gehennom.invalid (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

 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      07-02-2007
James Kanze <> wrote:
> On Jun 29, 5:14 pm, ricec...@gehennom.invalid (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
 
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
portably encrypting a file system's partition, directory and/or file news Computer Security 1 11-25-2005 11:10 PM
portably encrypting a file system's partition, directory and/or file news Java 0 11-25-2005 08:30 PM
beeping portably John J. Lee Python 4 03-22-2005 09:42 PM
Problem with blocking portably on sockets and Queue? Tero Saarni Python 2 08-07-2003 04:44 AM
Portably Selecting an Integer Type of a Specific Size? Simon G Best C++ 3 07-17-2003 07:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57