PStringUtils::IsInt: accept an optional leading sign
IsInt now recognises (possibly signed) integers such as "-5" or "+42", making it slightly more permissive than TString::IsDigit(). A lone sign, a double sign, or a sign following a digit are still rejected. strToNum test expectations updated accordingly. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -65,23 +65,34 @@ std::vector<std::string> PStringUtils::Split(const std::string &str, const std::
|
||||
// IsInt (static)
|
||||
//--------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Returns true if the string is a non-empty sequence of decimal digits
|
||||
* only. Mirrors the semantics of TString::IsDigit().
|
||||
* <p>Returns true if the string is an integer literal, i.e. a non-empty
|
||||
* sequence of decimal digits with an optional single leading sign (+/-).
|
||||
* This is slightly more permissive than TString::IsDigit(), which rejects a
|
||||
* sign, so that negative/positive integers such as "-5" or "+42" are also
|
||||
* recognised.
|
||||
*
|
||||
* \param str string to be checked
|
||||
* \return true if str consists of digits only
|
||||
* \return true if str is a (possibly signed) integer
|
||||
*/
|
||||
bool PStringUtils::IsInt(const std::string &str)
|
||||
{
|
||||
// mirror TString::IsDigit(): all characters must be digits or whitespace,
|
||||
// and there must be at least one digit (surrounding/embedded whitespace is
|
||||
// tolerated, e.g. for tokens split on ',' or ';' only).
|
||||
// all characters must be digits or whitespace, with an optional single
|
||||
// leading sign (+/-) preceding the digits, and there must be at least one
|
||||
// digit (surrounding/embedded whitespace is tolerated, e.g. for tokens
|
||||
// split on ',' or ';' only).
|
||||
bool hasDigit = false;
|
||||
bool hasSign = false;
|
||||
for (char c : str) {
|
||||
if (std::isdigit(static_cast<unsigned char>(c)))
|
||||
if (std::isdigit(static_cast<unsigned char>(c))) {
|
||||
hasDigit = true;
|
||||
else if (!std::isspace(static_cast<unsigned char>(c)))
|
||||
} else if (c == '+' || c == '-') {
|
||||
// a sign is only valid before any digit and may appear at most once
|
||||
if (hasDigit || hasSign)
|
||||
return false;
|
||||
hasSign = true;
|
||||
} else if (!std::isspace(static_cast<unsigned char>(c))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return hasDigit;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user