473,804 Members | 3,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string::npos + 1 ???

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_ty pe not_white;

// trim leading whitespace
not_white = s.find_first_no t_of(ws);
s.erase(0, not_white);

// trim trailing space
not_white = s.find_last_not _of(ws);
s.erase(not_whi te+1); /*** Is this safe? ***/
// ^^^^^^^^^^^

My question is about the underlined not_white+1. What if
the find_last_not_o f call returns string::npos, as would
happen if s was a blank string. It seems to work with
all of my compilers, but it seems dubious to increment
string::npos.
Jul 22 '05 #1
1 4051
Derek wrote:
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_ty pe not_white;

// trim leading whitespace
not_white = s.find_first_no t_of(ws);
s.erase(0, not_white);

// trim trailing space
not_white = s.find_last_not _of(ws);
s.erase(not_whi te+1); /*** Is this safe? ***/
// ^^^^^^^^^^^

My question is about the underlined not_white+1. What if
the find_last_not_o f call returns string::npos, as would
happen if s was a blank string. It seems to work with
all of my compilers, but it seems dubious to increment
string::npos.


npos is defined as -1 converted to string::size_ty pe, which is an
unsigned integral type. As per the rules of unsigned types, -1 becomes
the largest representable value. Adding 1 causes this value to wrap
around to 0. Therefore, the result will be equivalent to

s.erase(0);

As far as I can tell, there's nothing wrong with this. It should make
's' empty (if it isn't already -- in your example it should already be).
This seems to give the expected result in your code, but it may be a bit
confusing. It may not be immediately obvious to someone reading the code
that it is correctly handling this case, and therefore it might be
worthwhile to make handling of it explicit. But that's your call.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #2

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

Similar topics

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);
8
2543
by: Marcus Kwok | last post by:
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...
11
4293
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
4272
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
9584
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
10583
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
10337
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
9160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6854
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();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4301
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
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2995
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.