Á
Ángel José Riesgo
Hi,
I'm writing some code that parses a string and tries to find some
tokens and extract some data from the string. The problem is simple
and the code I've just written works fine. However, I need some ugly
casts to get rid of a signed/unsigned mismatch warning, and I was
wondering if there may be a more elegant way of doing this. A dumbed
down version of my code follows:
#include <string>
const std::string kToken = "TOKEN";
void FindToken(std::string::const_iterator& position,
std::string::const_iterator end)
{
std::string::size_type tokenLength = kToken.length();
if(tokenLength <= (end - position)) // <- Signed - unsigned
conversion warning here.
{
std::string expectedToken(position, position + tokenLength);
if(expectedToken == kToken)
position += tokenLength; // The token has been found and the
iterator is advanced before returning.
}
}
Basically, I've moved the bit of code I'm interested in to the above
function FindToken, which tries to find a certain token ("TOKEN"), and
advances the "position" iterator by the token's length if it is found.
Otherwise, the function returns quietly leaving the iterator
unchanged. In the actual code, I can assume that the two iterators
come from the same string object and that position <= end.
Now the problem with the above code (building it with MSVC 10) is that
I get a warning because of the conversion between the signed type
returned by the (end - position) iterator subtraction and the
std::string::size_type unsigned integer type. I can
static_cast<std::string::size_type> the warning away, of course, but
it's a bit ugly, so I was wondering if anyone knows of a way of doing
this sort of thing without either warnings or casts.
Thanks in advance,
Ángel José Riesgo
I'm writing some code that parses a string and tries to find some
tokens and extract some data from the string. The problem is simple
and the code I've just written works fine. However, I need some ugly
casts to get rid of a signed/unsigned mismatch warning, and I was
wondering if there may be a more elegant way of doing this. A dumbed
down version of my code follows:
#include <string>
const std::string kToken = "TOKEN";
void FindToken(std::string::const_iterator& position,
std::string::const_iterator end)
{
std::string::size_type tokenLength = kToken.length();
if(tokenLength <= (end - position)) // <- Signed - unsigned
conversion warning here.
{
std::string expectedToken(position, position + tokenLength);
if(expectedToken == kToken)
position += tokenLength; // The token has been found and the
iterator is advanced before returning.
}
}
Basically, I've moved the bit of code I'm interested in to the above
function FindToken, which tries to find a certain token ("TOKEN"), and
advances the "position" iterator by the token's length if it is found.
Otherwise, the function returns quietly leaving the iterator
unchanged. In the actual code, I can assume that the two iterators
come from the same string object and that position <= end.
Now the problem with the above code (building it with MSVC 10) is that
I get a warning because of the conversion between the signed type
returned by the (end - position) iterator subtraction and the
std::string::size_type unsigned integer type. I can
static_cast<std::string::size_type> the warning away, of course, but
it's a bit ugly, so I was wondering if anyone knows of a way of doing
this sort of thing without either warnings or casts.
Thanks in advance,
Ángel José Riesgo