std::filesystem::path::lexically_normal, std::filesystem::path::lexically_relative, std::filesystem::path::lexically_proximate
提供: cppreference.com
< cpp | filesystem | path
path lexically_normal() const; |
(1) | (C++17以上) |
path lexically_relative(const path& base) const; |
(2) | (C++17以上) |
path lexically_proximate(const path& base) const; |
(3) | (C++17以上) |
2)
base
に対して相対化した *this
を返します。
- まず、 root_name() != base.root_name() || is_absolute() != base.is_absolute() || (!has_root_directory() && base.has_root_directory()) であれば、デフォルト構築されたパスを返します。
- そうでなければ、まず、 auto [a, b] = mismatch(begin(), end(), base.begin(), base.end()) によって行われたかのように、
*this
とbase
の最初の一致しない要素を探します。 それから、
- a == end() かつ b == base.end() であれば、 path(".") を返します。
- そうでなければ、 [b, base.end()) 内のドットでもドット-ドットでもない空でないファイル名要素の数からドット-ドットファイル名要素の数を引いた値を N としたとき、 N < 0 であれば、デフォルト構築されたパスを返します。
- そうでなく、 N = 0 かつ a == end() || a->empty() であれば、 path(".") を返します。
- そうでなければ、以下のように構成されるオブジェクトを返します。
- デフォルト構築された path() と、それに続く、
- operator/=(path("..")) の N ���の適用と、それに続く、
- 半開区間
[a, end())
内の各要素に対する operator/= の適用。
3) lexically_relative(base) の値が空のパスでなければ、それを返します。 そうでなければ、
*this
を返します。目次 |
[編集] 引数
(なし)
[編集] 戻り値
1) パスの正規形。
2) パスの相対形。
3) パスの近接形。
[編集] 例外
(なし)
[編集] ノート
これらの変換は純粋に字句的に行われます。 パスが存在するかどうかを調べず、シンボリックリンクも辿らず、ファイルシステムに一切アクセスしません。 lexically_relative
および lexically_proximate
のシンボリックリンクを辿るバージョンについては、 relative および proximate を参照してください。
Windows では、返されるパスはバックスラッシュ (推奨区切り文字) を持ちます。
[編集] 例
Run this code
#include <iostream> #include <filesystem> #include <cassert> namespace fs = std::filesystem; int main() { assert(fs::path("foo/./bar/..").lexically_normal() == "foo/"); assert(fs::path("foo/.///bar/../").lexically_normal() == "foo/"); assert(fs::path("/a/d").lexically_relative("/a/b/c") == "../../d"); assert(fs::path("/a/b/c").lexically_relative("/a/d") == "../b/c"); assert(fs::path("a/b/c").lexically_relative("a") == "b/c"); assert(fs::path("a/b/c").lexically_relative("a/b/c/x/y") == "../.."); assert(fs::path("a/b/c").lexically_relative("a/b/c") == "."); assert(fs::path("a/b").lexically_relative("c/d") == "../../a/b"); }
[編集] 欠陥報告
以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。
DR | 適用先 | 発行時の動作 | 正しい動作 |
---|---|---|---|
LWG 3096 | C++17 | trailing "/" and "/." are handled incorrectly | corrected |
[編集] 関連項目
(C++17) |
相対パスを組み立てます (関数) |