473,383 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

String trimming throwing an exception

Can this code ever throw out_of_range or should the commented out code
always be used?

Is there any chance that std::string::npos+1 is bigger then
std::string::npos

Take str to be any allowable string value.

#include <iostream>
#include <string>

int main(int argc, char *argv[])
{
std::string str;

str.erase(0, str.find_first_not_of(" \t"));
str.erase(str.find_last_not_of(" \t")+1, std::string::npos);

/*
str.erase(0, str.find_first_not_of(" \t"));
std::string::size_type x=str.find_last_not_of(" \t");
if(x!=std::string::npos)
{
str.erase(x+1, std::string::npos);
}
*/
return 0;
}

Apr 10 '07 #1
5 2026
On 10 Apr, 21:09, "Adrian" <n...@bluedreamer.comwrote:
Can this code ever throw out_of_range or should the commented out code
always be used?

Is there any chance that std::string::npos+1 is bigger then
std::string::npos
npos is of type size_type and have the value -1, from what I can
determine size_type is an unsigned type meaning that npos is the
largest possible value that a variable of size_type can have. This
means that npos + 1 can never be larger than npos.
Take str to be any allowable string value.

#include <iostream>
#include <string>

int main(int argc, char *argv[])
{
std::string str;

str.erase(0, str.find_first_not_of(" \t"));
str.erase(str.find_last_not_of(" \t")+1, std::string::npos);
So this will remove all but the first character if there are no tabs,
you should probably use the code below.
/*
str.erase(0, str.find_first_not_of(" \t"));
std::string::size_type x=str.find_last_not_of(" \t");
if(x!=std::string::npos)
{
str.erase(x+1, std::string::npos);
}
*/
return 0;

}
--
Erik Wikström

Apr 11 '07 #2
On Apr 11, 12:30 am, "Erik Wikström" <eri...@student.chalmers.se>
wrote:
npos is of type size_type and have the value -1, from what I can
determine size_type is an unsigned type meaning that npos is the
largest possible value that a variable of size_type can have. This
means that npos + 1 can never be larger than npos.
So on a standard implementaion it will never throw
>
str.erase(0, str.find_first_not_of(" \t"));
str.erase(str.find_last_not_of(" \t")+1, std::string::npos);

So this will remove all but the first character if there are no tabs,
you should probably use the code below.
The both remove the same about of white space :-) check again.
>
/*
str.erase(0, str.find_first_not_of(" \t"));
std::string::size_type x=str.find_last_not_of(" \t");
if(x!=std::string::npos)
{
str.erase(x+1, std::string::npos);
}
*/
return 0;
Apr 11 '07 #3
On Apr 10, 9:09 pm, "Adrian" <n...@bluedreamer.comwrote:
Can this code ever throw out_of_range or should the commented out code
always be used?
Is there any chance that std::string::npos+1 is bigger then
std::string::npos
Technically, I think so, but only if size_t is smaller than an
int (and integral promotion means that the expression has a type
larger than size_t). So I can't imagine an implementation where
it would happen in practice. And of course, if the npos+1 is
assigned to a size_t, it is guaranteed to be 0.
Take str to be any allowable string value.
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
std::string str;
str.erase(0, str.find_first_not_of(" \t"));
str.erase(str.find_last_not_of(" \t")+1, std::string::npos);
Since the parameter type of erase, here, is size_t, you should
be OK in every case.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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

Apr 12 '07 #4
On Apr 11, 4:25 pm, "Adrian" <n...@bluedreamer.comwrote:
On Apr 11, 12:30 am, "Erik Wikström" <eri...@student.chalmers.se>
wrote:
npos is of type size_type and have the value -1, from what I can
determine size_type is an unsigned type meaning that npos is the
largest possible value that a variable of size_type can have. This
means that npos + 1 can never be larger than npos.
So on a standard implementaion it will never throw
That's an interesting question, since the standard allows a lot
of variance in implementations. I think that it could result in
undefined behavior (and thus, anything, including throwing), but
it would take a very, very strange implementation, which I don't
think we'll ever see.

Basically, the only time it could cause trouble is if INT_MAX ==
USHRT_MAX and size_t were a typedef for unsigned short. Both
conditions are, I think, permitted, but I can't imagine ever
seeing them.

Quite frankly, I wouldn't worry about this possibility.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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

Apr 12 '07 #5
On Apr 12, 2:56 am, "James Kanze" <james.ka...@gmail.comwrote:
That's an interesting question, since the standard allows a lot
of variance in implementations. I think that it could result in
undefined behavior (and thus, anything, including throwing), but
it would take a very, very strange implementation, which I don't
think we'll ever see.

Basically, the only time it could cause trouble is if INT_MAX ==
USHRT_MAX and size_t were a typedef for unsigned short. Both
conditions are, I think, permitted, but I can't imagine ever
seeing them.

Quite frankly, I wouldn't worry about this possibility.
Thanks James, that makes sense to me.

I never did worry about this :-) until someone else questioned it so
thought I should check.

Adrian

Apr 12 '07 #6

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

Similar topics

3
by: Scott Brady Drummonds | last post by:
Hi, all, I've a fairly small piece of code that is causing me problems. I'm using std::string and am building a string of several dozen characters using several of std::string's functions: a...
1
by: Farooq Khan | last post by:
i'm writing a class library having following code snippet. i want this class to report an error (by throwing an Exception) to the application using this class library. the problem is that within...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
7
by: ORC | last post by:
Hi, How to transfer strings between a C# .NET application and a native DLL that uses Unicode like: #ifndef UNICODE #define UNICODE #endif The function header in the DLL: BOOL...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
2
by: Bob | last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure out what I am doing wrong. Given the following setup code: string str = { "one", "two", "three", "four" }; double...
5
by: CarlWSummers | last post by:
I'm doing something wrong. I'm parsing a string into rational numbers (int/int) by looking for spaces in the string, assinging a substring of the original string to a temporary variable, and...
1
by: usenet | last post by:
I wrote some sample code (see below) for nested exception throwing i.e. my catch blocks are throwing exceptions of their own (for simplicity I used standard exceptions). I am getting some...
4
by: Sin Jeong-hun | last post by:
Hello. It seems like the default behavior of DrawString is trying to keep the whole word if possible. I'd like the string to be drawn in multilines, but filling the entire line width. For example...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.