通过规则处理让源路径成为最短等价路径
处理规则如下
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 of the input. For example, "./local" becomes "local", "local/../bin" becomes "bin" and "/local/usr/../bin" becomes "/local/bin".
红色的表示:在不影响正确的情况,在返回的路径中,尽量保留 . 和 ..
这也就是输出/../a/c而不是/a/c的原因。