ev-mold-string-prefix-suffix-glob-002¶
| 字段 | 值 |
|---|---|
| 项目 | rui314/mold |
| 固定 commit | 27162691b71a |
| 路径 | lib/multi-glob.cc |
| 行段 | L80-L93 |
| 特性 | 字符串与范围小工具 |
| 许可证 | MIT |
| 代码来源 | 项目源码 |
| 级别 | manual |
| 审核 | zyuhao7 @ 2026-10-24 |
| 测试代码 | 否 |
| 发现方式 | regex |
用法¶
同时使用 starts_with 与 ends_with 处理通配符
为什么典型¶
前后缀检查成对出现,直接表达“以 * 开头/结尾”,比手写索引与长度运算更难出错。
摘录¶
static std::string handle_stars(std::string_view pat) {
std::string str(pat);
// Convert "foo" -> "\0foo\0", "*foo" -> "foo\0", "foo*" -> "\0foo"
// and "*foo*" -> "foo". Aho-Corasick can do only substring matching,
// so we use \0 as beginning/end-of-string markers.
if (str.starts_with('*') && str.ends_with('*'))
return str.substr(1, str.size() - 2);
if (str.starts_with('*'))
return str.substr(1) + "\0"s;
if (str.ends_with('*'))
return "\0"s + str.substr(0, str.size() - 1);
return "\0"s + str + "\0"s;
}
备注¶
生产路径;摘录覆盖完整的通配符前缀/后缀分支。