c++ - 快速确定字符串是否为 JSON 数字的方法

标签 c++ json

检查不得使用 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 做类似的事情.


如果您想禁止 INFINITYNAN 或十六进制浮点值,只需检查字符串中的第二个或第三个字符不是字母即可:

if ((text.length() > 2 && std::isalpha(text[1])) ||
    (text.length() > 3 && std::isalpha(text[2])))
{
    // Not a number
}

对于“更大”的数字,总是有 std::stoldstd::strtold。但是,如果您想要任意大小的数字,那么要么像现在一样做,要么使用诸如 GMP 之类的库。 (mpf_set_str 似乎是一个很好的函数)。

关于c++ - 快速确定字符串是否为 JSON 数字的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15566804/

相关文章:

json - 在 Coldfusion 中的 api Web 服务中以 json 格式发送纯文本

c++ - C++中初始化程序和默认初始化程序列表之间的区别

ios - 从 json 获取数据后将数据加载到 UILabels 和 UITextFields

javascript - 为什么我会收到 Uncaught SyntaxError : Unexpected token ILLEGAL

c++ - 向模板特化添加更多类型

ios - NSCFString 0x2749a0 valueForUndefinedKey 此类不符合键数据的键值编码

json - Grunt 不会从 grunt-json-server 运行 json_server 任务

c++ - 是否可以使用互斥锁只锁定数据结构的一个元素?

c++ - 何时使用预处理器指令在 C++ 中定义函数?

c++ - 如何确定传递给模板函数的 vector 的类型