通过规则处理让源路径成为最短等价路径
处理规则如下
1.使用单斜线取代多斜线
2.取消每个包含.名称的路径
3.取消内部包含..的路径
4.取消/..根路径的元素,使用/替换
这个过程是循环执行的,直到路径符合所有规则
示例
QStringList ls;
ls << "a/c" << "a//c" << "a/c/." << "a/c/b/.." << "/../a/c" << "/../a/b/../././/c" << "";
for (auto& t : ls)
{
QString k = QDir::cleanPath(t);
qDebug() << k << "\r\n";
}
输出如下:

而/../a/c其实就是/a/c, Qt官方帮助手册中提到如下:
Returns path with directory separators normalized (that is, platform-native separators converted to "/") and redundant ones removed, and "."s and ".."s resolved (as far as possible).
Symbolic links are kept. This function does not return the canonical path, but rather the simplest version

本文介绍了一种处理文件路径的算法,通过规范化目录分隔符、移除冗余符号及解析.和..来简化路径。例如,./local变为local,local/../bin变为bin,/local/usr/../bin变为/local/bin。
最低0.47元/天 解锁文章
1464

被折叠的 条评论
为什么被折叠?



