/// <summary> /// - "c:/dir1" + "dir2/file.txt" => "c:/dir1/dir2/file.txt" /// - "c:/dir1" + "/from-root/file.txt" => "c:/from-root/file.txt" /// - "c:/dir1" + "d:dir2/file.txt" (drive and relative path) => "d:/dir1/dir2/file.txt" /// - "c:/dir1" + "\\.\server\share-name\dir2\file.txt" => "\\.\server\share-name\dir2\file.txt" /// </summary> /// <param name="aOther"></param> /// <returns>new Filepath instance.</returns> public Filepath Combine(string aOther) { if (string.IsNullOrEmpty(aOther)) { return(this); } return(Combine(Filepath.Parse(aOther))); }
/// <summary> /// Traditional DOS path /// Parse(@"C:\dir\file.txt"); /// Parse(@"relative\dir\and\file.txt"); /// Parse(@"\"); // root /// Parse(@"C:\"); // drive and root /// Parse(@"C:"); // specifies a drive, but relative /// Parse(@"D:drive-and-relative\dir\file"); /// /// DOS Device path /// Parse(@"\\?\volume/dir/more-dir/.git"); /// /// UNC path /// Parse(@"\\server\share-name\dir\file"); /// /// UNIX /// Parse("/usr/local/bin"); /// Parse("/"); // root /// </summary> /// <param name="aInput"></param> /// <returns></returns> public static Filepath Parse(string?aInput) { if (string.IsNullOrEmpty(aInput)) { return(Empty); } var scan = new FilepathScanner(aInput ?? ""); var self = new Filepath(); self.Prefix = _ParsePrefix(scan); self.Absolute = scan.Skip(SEPARATING_PATTERN); self.Items = _ParsePath(scan).ToArray(); return(self); }
public Filepath Combine(Filepath aOtherPath) { if (aOtherPath == null) { return(this); } return(new Filepath { Prefix = _SelectPrefix(aOtherPath.Prefix, this.Prefix), Absolute = this.Absolute || aOtherPath.Absolute, Items = aOtherPath.Absolute ? aOtherPath.Items : this.Items.Concat(aOtherPath.Items).ToArray() }); }
/// <summary> /// 1. マイナスのstartは末尾からの距離と考え、1ステップだけ補正する /// 2. マイナスのcountは、末尾からいくつ削るかの指定とみなす /// 3. 大きなcountは補正する /// 4. 補正後のstartが範囲外の場合はパスを空にする /// </summary> /// <param name="aStart">マイナス指定も可</param> /// <param name="aCount">マイナス指定も可</param> /// <returns></returns> public Filepath Slice(int aStart, int aCount = int.MaxValue) { var rv = new Filepath { Prefix = this.Prefix, }; var fixedHead = _FixHeadIndex(aStart); var fixedTail = _FixTailIndex(fixedHead, aCount); var fixedCount = fixedTail - fixedHead; // 開始位置が 0 であれば、this.Absolute を踏襲。そうでなければ常に相対パスとなる rv.Absolute = (fixedHead == 0) ? this.Absolute : false; // countが1以上の場合のみ、Itemsを切り出して取得 rv.Items = (0 <= fixedHead) && (fixedHead < Items.Length) && (1 <= fixedCount) ? this.Items.Skip(fixedHead).Take(fixedCount).ToArray() : _EMPTY; return(rv); }