我编写了一个函数,该函数可以解析文本文件中的一行,并用列表中的相应副本替换cerain关键字。
代码如下:
std::string __iec_parse_cl(const std::string &line){
std::string ret = line;
static const size_t num_tokens = 6;
static const char *tokens_search[6]{
filled ...
};
static const char *tokens_replace[6]{
filled ...
};
for(size_t x = 0; x < num_tokens; x++){
size_t yolo;
do{
yolo = ret.find(tokens_search[x];
if(yolo != std::string::npos){
ret.erase(yolo, strlen(tokens_search[x]));
ret.insert(yolo, tokens_replace[x]);
}
} while(yolo != std::string::npos);
}
return ret;
}
当我解析一个看起来像这样的令牌时:globalid
用这个替换它:get_global_id
一切都很好...
但是,当我尝试解析看起来像这样的令牌global_id
并尝试用它替换它时:get_global_id
程序停在函数中的某个位置:/
是什么原因造成的?
那是因为您的搜索需要在替换后开始。如果你每次更换后打印出你的字符串,你会看到它有get_global_id
那么get_get_global_id
再get_get_get_global_id
等等
您需要做的是告诉find
您在上一次更换后开始。
size_t yolo = 0;
do{
yolo = ret.find(tokens_search[x], yolo);
if (yolo != std::string::npos){
ret.erase(yolo, strlen(tokens_search[x]));
ret.insert(yolo, tokens_replace[x]);
yolo += strlen(tokens_replace[x]);
}
} while(yolo != std::string::npos);
如果令牌与其替换令牌之间存在其他重叠,则这样做也可以防止其他问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句