繁体   English   中英

快速字符串解析器 C++

[英]fast string parser C++

我有一个制作超快速命令解析器的想法。

我有 100 多对命令-函数,有些命令具有相同的前缀。

下面是我的想法的例子。 我可以制作一个程序来生成本例中的 C++ 代码,但我认为这可以通过模板来实现。

我不擅长模板。 可能有人可以帮忙吗?

static const string_view s1{"hello"};
void f1() { cout << "f1" << endl; }
static const string_view s2{"helly"};
void f2() { cout << "f2" << endl; }
static const string_view s3{"hi jo"};
void f3() { cout << "f3" << endl; }
static const string_view s4{"hoyMo"};
void f4() { cout << "f4" << endl; }

void sw(string_view& hw){
    switch(hw.size()){
        case 5: {
            switch(hw[0]){
                case 'h': {
                    switch(hw[1]){
                        case 'e': {
                            switch(hw[2]){
                                case 'l': {
                                    switch(hw[3]){
                                        case 'l': {
                                            switch(hw[4]){
                                                case 'o': {
                                                    f1();
                                                    break;
                                                }
                                                case 'y': {
                                                    f2();
                                                    break;
                                                }
                                                default: cout << "command not found" << endl; break;
                                            }
                                            break;
                                        }
                                        default: cout << "command not found" << endl; break;
                                    }
                                    break;
                                }
                                default: cout << "command not found" << endl; break;
                            }
                            break;
                        }
                        case 'i': {
                            if(hw.substr(2) == s3.substr(2)){
                                f3();
                            }
                            break;
                        }
                        case 'o': {
                            if(hw.substr(2) == s4.substr(2)){
                                f4();
                            }
                            break;
                        }
                        default: cout << "command not found" << endl; break;
                    }
                    break;
                }
                default: cout << "command not found" << endl; break;
            }
            break;
        }
        case 6: {
            //...
            break;
        }
        default: cout << "command not found" << endl; break;
    }
}

int main(){
    string_view myCommand("hi jo");
    sw(myCommand);
    string_view myCommand2("hoyMo");
    sw(myCommand2);
    string_view myCommand3("ha ha");
    sw(myCommand3);
}

您可能应该使用解析器库,例如Boost.Spirit 这将允许您编写简单的代码,例如

  string("hello")
| string("helly")
| string("hi jo")
| string("hoyMo")

并为您完成所有繁重的工作,以生成一个可能比您自己编写的解析器更快的解析器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM