检查不得使用 C++11 功能或其他库,例如Boost、Regex 等。我想出了以下解决方案。不漂亮但工作。它会从这里变得更优雅和/或更快吗?
bool isJsonNumber(const std::string& text)
{
if(text.empty()) return false;
bool foundE = false;
bool foundESign = false;
bool leadingZero = false;
bool lastIsDigit = false;
bool foundDot = false;
for(uint32_t i=0; i < text.length(); ++i)
{
const unsigned char c = text[i];
lastIsDigit = false;
const bool currIsNoDigit = (c < '0' || c > '9');
if(i == 0)
{
if(currIsNoDigit && c != '-' ) return false;
if(c == '0') leadingZero = true;
if(c != '-') lastIsDigit = true;
}
else
{
if(leadingZero)
{
leadingZero = false;
if(c != '.') return false;
foundDot = true;
}
else if(c == '.')
{
if(foundDot) return false;
foundDot = true;
}
else if(c == 'e' || c == 'E')
{
if(foundE) return false;
foundE = true;
}
else if(foundE && !foundESign)
{
if(currIsNoDigit && c != '-' && c != '+') return false;
if(c == '+' || c == '-')
{
foundESign = true;
}
else
{
lastIsDigit = true;
}
}
else
{
foundESign = false;
if(currIsNoDigit) return false;
lastIsDigit = true;
}
}
}
if(lastIsDigit == false) return false;
return true;
}
用例是一个小型嵌入式服务器,它接收巨大的 CSV 文件,并用 JSON 格式回答客户端。
最佳答案
使用 std::stod
可能会更容易:
size_t endpos;
std::stod(text, &endpos);
if (endpos != text.length())
{
// Not a number
}
else
{
// A number
}
如果你没有 std::stod
,因为它是 C++11 的特性,你可以用 std::strtod
做类似的事情.
如果您想禁止 INFINITY
或 NAN
或十六进制浮点值,只需检查字符串中的第二个或第三个字符不是字母即可:
if ((text.length() > 2 && std::isalpha(text[1])) ||
(text.length() > 3 && std::isalpha(text[2])))
{
// Not a number
}
对于“更大”的数字,总是有 std::stold
或 std::strtold
。但是,如果您想要任意大小的数字,那么要么像现在一样做,要么使用诸如 GMP 之类的库。 (mpf_set_str
似乎是一个很好的函数)。
关于c++ - 快速确定字符串是否为 JSON 数字的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15566804/