473,795 Members | 2,826 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 4050
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
2542
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
4291
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
9672
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
10438
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
10001
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
9042
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...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
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
5437
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
4113
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
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.