[英]fast string parser C++
I have an idea to make super fast command parser.我有一个制作超快速命令解析器的想法。
I have more than 100 pairs of command - function, and some commands have same prefixes.我有 100 多对命令-函数,有些命令具有相同的前缀。
Down below there is example of my idea.下面是我的想法的例子。 I can make a program that will generate C++ code like in this example, but i think this can be realized with templates.
我可以制作一个程序来生成本例中的 C++ 代码,但我认为这可以通过模板来实现。
I'm not strong in templates.我不擅长模板。 May be some one can help with it?
可能有人可以帮忙吗?
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);
}
You should probably be using a parser library, such as Boost.Spirit .您可能应该使用解析器库,例如Boost.Spirit 。 This wil allow you to write simple code, like
这将允许您编写简单的代码,例如
string("hello")
| string("helly")
| string("hi jo")
| string("hoyMo")
and do all the heavy lifting for you to generate a parser that will probably be faster than something you would write yourself.并为您完成所有繁重的工作,以生成一个可能比您自己编写的解析器更快的解析器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.