C++-在搜索带下划线的字符串时,行分析器会暂停
C++ - Line parser stalls when searching for string with underscore
我编写了一个函数,该函数解析文本文件中的一行,并将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);
如果您的代币和它们的替代品之间存在其他重叠,那么这样做也可以防止其他问题。
相关文章:
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 如何在程序执行时查看Valgrind Massif输出(或其他堆分析器)?
- 如何暂停插孔音频客户端
- 在 vscode 中运行时无法暂停或设置断点
- C++ 如何暂停控制台输出,以便用户可以在继续之前阅读信息
- 如何将循环暂停几秒钟?
- 为什么我的静态分析器找不到潜在的未初始化读取?
- #pragma(*诊断)当将Clang分析器与GCC编译器混合时
- Clang 分析器潜在内存泄漏 - 误报
- 如何在 for 循环内暂停
- 在 C++ 中暂停 For 循环 1 秒钟
- 使用暂停按钮ARDUINO创建灯光序列时出现问题
- 如何暂停进程中的所有线程(停止世界)
- Flex:尝试使用 Flex 生成C++词法分析器; "unrecognized rule"错误
- 暂停功能,它永远循环
- 为什么 XAudio2 仅在系统暂停时播放.wav文件?
- 使用 gperftools 的堆分析器来分析 libc malloc
- 避免在暂停时破坏声音
- Visual Studio 项目设置,用于检测英特尔跟踪分析器
- C++-在搜索带下划线的字符串时,行分析器会暂停