본문으로 건너뛰기
토파즈6.0 · 출시 전
다운로드 →

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    라이브러리 · std.path

    Path.parent

    Returns the optional parent of this path.

    parent() -> Option<Path>
    수신자
    Path
    최소 인자 수
    0
    최대 인자 수
    0

    Declaration and parameters

    링크

    사용 조건

    기본 포함 · 미정

    Import std.path in language mode 5.21.

    The current compiler selects std/path.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    수신자

    Path

    매개변수 제약

    A Path is immutable normalized relative text. Every member evaluates its receiver and arguments once in ordinary call order. Members perform no host access and leave the receiver unchanged on success and failure.

    toString returns the exact normalized representation. Separators are forward slashes and the relative root is a dot. Constructing a Path from that text returns the same representation.

    join accepts nonempty relative child text. It rejects NUL, a leading separator, and a colon immediately after one ASCII character before combining the child with the receiver. Both slash forms are separators. Parent components remove preceding components from the combined path and return Err when none remain to remove.

    Joining a child may reach the relative root but may not cross it. A dot child preserves the receiver. Invalid child text returns Err containing an error string without producing a Path. Error text identifies Path.join and the invalid condition.

    parent removes the final component. For a one component path it returns Some containing the dot root. The relative root has no parent and returns None().

    fileName returns the final component without any preceding separator. The relative root returns None(). These results describe text and do not establish whether the path identifies a file or directory.

    extension returns the text after the final dot in the file name, provided that dot is not its first character. A leading dot alone does not introduce an extension. A final dot returns Some(""). A name without such a dot and the relative root return None().

    withExtension replaces the final extension or appends one when it is absent. The argument supplies the suffix without a leading dot. An empty argument removes an existing suffix and its separating dot. A leading dot, either separator, or NUL in the argument returns Err.

    withExtension preserves a leading dot that belongs to the file name. It returns Err when the receiver is the relative root because that path has no file name. A successful replacement is normalized with the same rules as Path.from.

    Path validation describes lexical text. Host operations may reject an otherwise valid Path because of platform naming rules, permissions, or capability restrictions. A Path does not establish filesystem existence or prevent a symbolic link from resolving elsewhere.

    결과

    The declared result type is Option<Path>.

    효과

    실패

    Result and effects

    링크

    Return the path without its final component. A one component path has the dot root as its parent, and the dot root has no parent.

    Related entries

    링크

    std.path →

    Inspect and transform normalized paths

    링크
    규칙topaz.stdlib.contract.path-operations.rule

    Inspect and transform normalized paths

    A Path is immutable normalized relative text. Every member evaluates its receiver and arguments once in ordinary call order. Members perform no host access and leave the receiver unchanged on success and failure.

    toString returns the exact normalized representation. Separators are forward slashes and the relative root is a dot. Constructing a Path from that text returns the same representation.

    join accepts nonempty relative child text. It rejects NUL, a leading separator, and a colon immediately after one ASCII character before combining the child with the receiver. Both slash forms are separators. Parent components remove preceding components from the combined path and return Err when none remain to remove.

    Joining a child may reach the relative root but may not cross it. A dot child preserves the receiver. Invalid child text returns Err containing an error string without producing a Path. Error text identifies Path.join and the invalid condition.

    parent removes the final component. For a one component path it returns Some containing the dot root. The relative root has no parent and returns None().

    fileName returns the final component without any preceding separator. The relative root returns None(). These results describe text and do not establish whether the path identifies a file or directory.

    extension returns the text after the final dot in the file name, provided that dot is not its first character. A leading dot alone does not introduce an extension. A final dot returns Some(""). A name without such a dot and the relative root return None().

    withExtension replaces the final extension or appends one when it is absent. The argument supplies the suffix without a leading dot. An empty argument removes an existing suffix and its separating dot. A leading dot, either separator, or NUL in the argument returns Err.

    withExtension preserves a leading dot that belongs to the file name. It returns Err when the receiver is the relative root because that path has no file name. A successful replacement is normalized with the same rules as Path.from.

    Path validation describes lexical text. Host operations may reject an otherwise valid Path because of platform naming rules, permissions, or capability restrictions. A Path does not establish filesystem existence or prevent a symbolic link from resolving elsewhere.

    이유

    One normalized representation makes composition predictable. Parent and extension queries describe components without consulting host state. A suffix has one accepted spelling, and joining validates the child before its leading separator can disappear inside the combined text.

    Join relative components without changing the receiver

    링크
    main.tpzJoin relative components without changing the receiver플레이그라운드 열기 →
    1. import std.path { Path }
    2. import std.test
    3. import std.io
    4. let base = test.assertOk(Path.from("chapters/draft"))
    5. let child = test.assertOk(base.join("../final/./main.tpz"))
    6. io.writeLine(base.toString())
    7. io.writeLine(child.toString())
    8. io.writeLine(test.assertOk(base.join("../..")).toString())
    9. io.writeLine(test.assertOk(base.join(".")).toString())
    기대 출력종료 코드 0
    chapters/draft
    chapters/final/main.tpz
    .
    chapters/draft
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. chapters/draft
    2. chapters/final/main.tpz
    3. .
    4. chapters/draft

    Reject invalid child text before joining

    링크
    main.tpzReject invalid child text before joining플레이그라운드 열기 →
    1. import std.path { Path }
    2. import std.test
    3. import std.io
    4. let base = test.assertOk(Path.from("chapters"))
    5. for child in (["", "/outside", "C:/outside", "../../outside"]) {
    6. match (base.join(child)) {
    7. case Err(_) => io.writeLine("rejected")
    8. case Ok(_) => io.writeLine("accepted")
    9. }
    10. }
    11. io.writeLine(base.toString())
    기대 출력종료 코드 0
    rejected
    rejected
    rejected
    rejected
    chapters
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. rejected
    2. rejected
    3. rejected
    4. rejected
    5. chapters

    Inspect the final component and the relative root

    링크
    main.tpzInspect the final component and the relative root플레이그라운드 열기 →
    1. import std.path { Path }
    2. import std.test
    3. import std.io
    4. let file = test.assertOk(Path.from("chapters/main.tpz"))
    5. io.writeLine(test.assertSome(file.parent()).toString())
    6. io.writeLine(test.assertSome(file.fileName()))
    7. io.writeLine(test.assertSome(file.extension()))
    8. let root = test.assertOk(Path.from("."))
    9. test.assertNone(root.parent())
    10. test.assertNone(root.fileName())
    11. test.assertNone(root.extension())
    12. io.writeLine(root.toString())
    기대 출력종료 코드 0
    chapters
    main.tpz
    tpz
    .
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. chapters
    2. main.tpz
    3. tpz
    4. .

    Keep the relative root as the parent of one component

    링크
    main.tpzKeep the relative root as the parent of one component플레이그라운드 열기 →
    1. import std.path { Path }
    2. import std.test
    3. import std.io
    4. let file = test.assertOk(Path.from("main.tpz"))
    5. match (file.parent()) {
    6. case Some(parent) => io.writeLine(parent.toString())
    7. case None() => io.writeLine("missing")
    8. }
    기대 출력종료 코드 0
    .
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. .

    Distinguish an absent extension from an empty extension

    링크
    main.tpzDistinguish an absent extension from an empty extension플레이그라운드 열기 →
    1. import std.path { Path }
    2. import std.test
    3. import std.io
    4. for name in (["README", ".env", ".env.local", "archive.tar.gz", "trailing."]) {
    5. let file = test.assertOk(Path.from(name))
    6. match (file.extension()) {
    7. case Some(extension) => io.writeLine("present [{extension}]")
    8. case None() => io.writeLine("absent")
    9. }
    10. }
    기대 출력종료 코드 0
    absent
    absent
    present [local]
    present [gz]
    present []
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. absent
    2. absent
    3. present [local]
    4. present [gz]
    5. present []

    Replace and remove a final extension

    링크
    main.tpzReplace and remove a final extension플레이그라운드 열기 →
    1. import std.path { Path }
    2. import std.test
    3. import std.io
    4. let file = test.assertOk(Path.from("chapters/main.tpz"))
    5. io.writeLine(test.assertOk(file.withExtension("pdf")).toString())
    6. io.writeLine(test.assertOk(file.withExtension("")).toString())
    7. io.writeLine(file.toString())
    기대 출력종료 코드 0
    chapters/main.pdf
    chapters/main
    chapters/main.tpz
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. chapters/main.pdf
    2. chapters/main
    3. chapters/main.tpz

    Preserve the name of a file that starts with a dot

    링크
    main.tpzPreserve the name of a file that starts with a dot플레이그라운드 열기 →
    1. import std.path { Path }
    2. import std.test
    3. import std.io
    4. let file = test.assertOk(Path.from(".env"))
    5. io.writeLine(test.assertOk(file.withExtension("local")).toString())
    6. io.writeLine(test.assertOk(file.withExtension("")).toString())
    기대 출력종료 코드 0
    .env.local
    .env
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. .env.local
    2. .env

    Reject a second suffix spelling and a path without a file name

    링크
    main.tpzReject a second suffix spelling and a path without a file name플레이그라운드 열기 →
    1. import std.path { Path }
    2. import std.test
    3. import std.io
    4. let file = test.assertOk(Path.from("chapter.txt"))
    5. for extension in ([".pdf", "pdf/extra", "pdf\\extra"]) {
    6. match (file.withExtension(extension)) {
    7. case Err(_) => io.writeLine("rejected")
    8. case Ok(_) => io.writeLine("accepted")
    9. }
    10. }
    11. let root = test.assertOk(Path.from("."))
    12. match (root.withExtension("pdf")) {
    13. case Err(_) => io.writeLine("root rejected")
    14. case Ok(_) => io.writeLine("root accepted")
    15. }
    기대 출력종료 코드 0
    rejected
    rejected
    rejected
    root rejected
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. rejected
    2. rejected
    3. rejected
    4. root rejected

    메뉴

    다운로드 →