From: Doug Coleman Date: Fri, 17 Feb 2023 02:55:48 +0000 (-0600) Subject: pathnames: support pathnames more places like append-path and recursive-directory... X-Git-Tag: 0.99~553 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=fa9e13476dee02203ed8e0b2fd3bf5549dd8ec5a pathnames: support pathnames more places like append-path and recursive-directory-files In general, P"" or `pathname` is a frontend/tooling convenience concept with tab-complete for paths in the ui. Backend apis should see strings. --- diff --git a/basis/io/directories/directories-tests.factor b/basis/io/directories/directories-tests.factor index 0f2933cdb6..2d027e88de 100644 --- a/basis/io/directories/directories-tests.factor +++ b/basis/io/directories/directories-tests.factor @@ -294,3 +294,9 @@ tools.test ; ] with-variable ] with-test-directory ] unit-test + +! test P"" pathnames +[ "resource:extra/math" recursive-directory-files drop ] must-not-fail + +{ "/foo/bar" } [ P"/foo" P"./bar" append-path ] unit-test +{ "/bar/foo" } [ P"./foo" P"/bar" prepend-path ] unit-test diff --git a/basis/io/directories/directories.factor b/basis/io/directories/directories.factor index c061d30f41..6f6b00dc75 100644 --- a/basis/io/directories/directories.factor +++ b/basis/io/directories/directories.factor @@ -88,6 +88,7 @@ TUPLE: directory-iterator [ queue>> swap '[ _ _ [ push-front ] [ push-back ] if ] each ] bi ; : ( path bfs? -- iter ) + [ pathname> ] dip directory-iterator boa dup path>> over push-directory-entries ; diff --git a/core/io/pathnames/pathnames.factor b/core/io/pathnames/pathnames.factor index 6bf366797f..a172fe45bc 100644 --- a/core/io/pathnames/pathnames.factor +++ b/core/io/pathnames/pathnames.factor @@ -78,6 +78,16 @@ ERROR: no-parent-directory path ; PRIVATE> +TUPLE: pathname string ; + +C: pathname + +: >pathname ( obj -- pathname ) + dup pathname? [ ] unless ; + +: pathname> ( pathname -- obj ) + dup pathname? [ string>> ] when ; + : absolute-path? ( path -- ? ) { { [ dup empty? ] [ drop f ] } @@ -92,6 +102,7 @@ PRIVATE> [ trim-head-separators ] bi* "/" glue ; : append-path ( path1 path2 -- path ) + [ pathname> ] bi@ { { [ over empty? ] [ append-path-empty ] } { [ dup empty? ] [ drop ] } @@ -221,13 +232,6 @@ M: object canonicalize-path-full canonicalize-path canonicalize-drive ; : >windows-path ( path -- path' ) H{ { CHAR: / CHAR: \\ } } substitute ; -TUPLE: pathname string ; - -C: pathname - M: pathname absolute-path string>> absolute-path ; M: pathname <=> [ string>> ] compare ; - -: >pathname ( obj -- pathname ) - dup pathname? [ ] unless ;