473,785 Members | 2,720 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is std::string::np os portably incrementable?

Is std::string::np os 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_t ype)-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::s tring& s)
{
using std::string;

string to_insert = "goes/";
string::size_ty pe 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
Jun 29 '07 #1
8 2540
Marcus Kwok wrote:
Is std::string::np os 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_t ype)-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::s tring& s)
{
using std::string;

string to_insert = "goes/";
string::size_ty pe 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_re place be a better choice in this case?

Fei
Jun 29 '07 #2
Marcus Kwok wrote:
Is std::string::np os 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_t ype)-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
Jun 29 '07 #3
On Fri, 29 Jun 2007 15:14:34 +0000, Marcus Kwok wrote:
Is std::string::np os 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_t ype)-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
Jun 29 '07 #4
Victor Bazarov <v.********@com acast.netwrote:
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
Jun 29 '07 #5
Marcus Kwok wrote:
Victor Bazarov <v.********@com acast.netwrote:
>Marcus Kwok wrote:
>>, so
incrementin g 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
Jun 29 '07 #6
John Harrison <jo************ *@hotmail.comwr ote:
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
Jun 29 '07 #7
On Jun 29, 5:14 pm, ricec...@gehenn om.invalid (Marcus Kwok) wrote:
Is std::string::np os 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: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 30 '07 #8
James Kanze <ja*********@gm ail.comwrote:
On Jun 29, 5:14 pm, ricec...@gehenn om.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
Jul 2 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3662
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while( (idx=str.find_first_of(',')) >= 0 ) { str.replace( idx, 1, "" ); str.insert( idx, " or " ); }
8
518
by: Derek | last post by:
A common technique for trimming leading and trailing spaces from std::string is the following: string s(" blah blah blah "); const char* ws= " \t\r"; string::size_type not_white; // trim leading whitespace not_white = s.find_first_not_of(ws);
18
2430
by: JKop | last post by:
Can some-one please point me to a nice site that gives an exhaustive list of all the memberfunctions, membervariables, operators, etc. of the std::string class, along with an informative description of how each works. I've been trying Google for the last 20 minutes but can't get anything decent. Thanks.
7
4871
by: Marcus Kwok | last post by:
std::string::npos is described in _TC++PL:SE_ (Section 20.3.4) as the "all characters" marker. I tried to use it this way, but my program crashes: #include <iostream> #include <string> int main() { std::string s = "hi/hello/now";
2
5514
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
11
4289
by: Christopher Pisz | last post by:
Is std::string::npos always going to be less than any std::string 's size()? I am trying to handle a replacement of all occurances of a substr, in which the replacement also contains the substr. Yick. All I could come up with is: #include <string> int main() { std::string text;
7
4269
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant expression") or Comeau Online ("constant value is not known"). If I replace
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.