Is std::string::npos portably incrementable?



 DEVELOP > c-Plus-Plus > Is std::string::npos portably incrementable?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Marcus Kwok"
Date: 29 Jun 2007 10:14:34 AM
Object: Is std::string::npos portably incrementable?
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
.

User: "Fei Liu"

Title: Re: Is std::string::npos portably incrementable? 29 Jun 2007 10:18:28 AM
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
.
User: "Marcus Kwok"

Title: Re: Is std::string::npos portably incrementable? 29 Jun 2007 10:26:25 AM
Fei Liu <feiliu@aepnetworks.com> 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
.


User: "Victor Bazarov"

Title: Re: Is std::string::npos portably incrementable? 29 Jun 2007 10:36:07 AM
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
.
User: "Marcus Kwok"

Title: Re: Is std::string::npos portably incrementable? 29 Jun 2007 11:55:01 AM
Victor Bazarov <v.Abazarov@comacast.net> 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
.
User: "John Harrison"

Title: Re: Is std::string::npos portably incrementable? 29 Jun 2007 11:54:44 AM
Marcus Kwok wrote:

Victor Bazarov <v.Abazarov@comacast.net> 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
.
User: "Marcus Kwok"

Title: Re: Is std::string::npos portably incrementable? 29 Jun 2007 12:06:18 PM
John Harrison <john_andronicus@hotmail.com> 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
.




User: "Obnoxious User"

Title: Re: Is std::string::npos portably incrementable? 29 Jun 2007 10:26:49 AM
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
.

User: "James Kanze"

Title: Re: Is std::string::npos portably incrementable? 30 Jun 2007 03:33:29 AM
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=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
User: "Marcus Kwok"

Title: Re: Is std::string::npos portably incrementable? 02 Jul 2007 08:43:39 AM
James Kanze <james.kanze@gmail.com> 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
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER