Skip to content

Commit 47c1c05

Browse files
Update path API (closes #248)
1 parent b27ba8d commit 47c1c05

15 files changed

Lines changed: 587 additions & 105 deletions

File tree

‎docs/specification/10-standard-library.html‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@
3131

3232
#### 10.1.1 API
3333

34-
- `str NORMALIZE_PATH(str path)` = Replace all backslashes in the path with forward slashes.
35-
- `str WINPATH(str path)` = Replace all forward slashes in the path with backslashes.
36-
- `str BASEPATH(str path)` = Return the directory part of the path, excluding the file name.
37-
- `str BASENAME(str path)` = Return the file name part of the path, excluding the directory.
38-
- `tensor SPLITEXT(str path)` = Split the path into a tensor containing the base name and the extension.
39-
- `str EXTNAME(str path)` = Return the file extension from the path, without the dot.
40-
- `str DELEXT(str path)` = Return the path without the file extension.
41-
- `str TEMPFILE(str prefix, str suffix)` = Modify the path to originate in the system's temporary directory.
42-
- `str interpreter` = The path to the Prefix interpreter executable.
43-
- `str interpreter_dir` = The directory containing the Prefix interpreter executable.
44-
- `str script` = The path to the currently executing script.
45-
- `str script_dir` = The directory containing the currently executing script.
34+
- `str path.normalize_path(str path)` = Replace all backslashes in the path with forward slashes.
35+
- `str path.windows_path(str path)` = Replace all forward slashes in the path with backslashes.
36+
- `str path.directory_name(str path)` = Return the directory part of the path, excluding the file name.
37+
- `str path.file_name(str path)` = Return the file name part of the path, excluding the directory.
38+
- `tensor path.split_extension(str path)` = Split the path into the path without its extension and its extension.
39+
- `str path.extension_name(str path)` = Return the file extension from the path, without the dot.
40+
- `str path.strip_extension(str path)` = Return the path without the file extension.
41+
- `str path.temp_file(str path)` = Modify the path to originate in the system's temporary directory.
42+
- `str path.interpreter` = The path to the Prefix interpreter executable.
43+
- `str path.interpreter_dir` = The directory containing the Prefix interpreter executable.
44+
- `str path.script` = The path to the currently executing script.
45+
- `str path.script_dir` = The directory containing the currently executing script.
4646

4747
### 10.2 Garbage collection
4848

‎lib/std/image/init.pre‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extend("image")
1111
import("path")
1212

1313
func tensor LOAD(str img_path){
14-
str ext = path.EXTNAME(img_path)
14+
str ext = path.extension_name(img_path)
1515
if(eq(ext, "png")){
1616
return(LOAD_PNG(img_path))
1717
} elseif (or(eq(ext, "jpg"),eq(ext, "jpeg"))){

‎lib/std/path.pre‎

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
! Path utilities for Prefix
22

3-
func str NORMALIZE_PATH(str path){
3+
func str normalize_path(str path){
44
return(replace(path,"\\","/"))
55
}
66

7-
func str WINPATH(str path){
7+
func str windows_path(str path){
88
return(replace(path,"/","\\"))
99
}
1010

11-
func str BASEPATH(str path){ ! returns directory portion of path
12-
str npath = NORMALIZE_PATH(path)
13-
str base = BASENAME(npath)
11+
func str directory_name(str path){ ! returns directory portion of path
12+
str npath = normalize_path(path)
13+
str base = file_name(npath)
1414
if(eq(base, "")){
1515
return("")
1616
}
17+
if(eq(npath, base)){
18+
return("")
19+
}
1720
str dir = replace(npath, join("/", base), "")
18-
del("npath")
1921
pop("dir")
2022
}
2123

22-
func str BASENAME(str path){ ! returns filename portion of path
23-
tensor tpath = split(NORMALIZE_PATH(path), "/")
24+
func str file_name(str path){ ! returns filename portion of path
25+
tensor tpath = split(normalize_path(path), "/")
2426
str basename = tpath[max(len_dim(tpath, 0d1), 0d1)]
2527
del("tpath")
2628
pop("basename")
2729
}
2830

29-
func tensor SPLITEXT(str path){ ! returns [name without ext, ext]
30-
str npath = NORMALIZE_PATH(path)
31-
str base = BASENAME(npath)
32-
str dir = BASEPATH(npath)
31+
func tensor split_extension(str path){ ! returns [name without ext, ext]
32+
str npath = normalize_path(path)
33+
str base = file_name(npath)
34+
str dir = directory_name(npath)
3335
tensor parts = split(base, ".")
3436
! If there is no dot, return the path unchanged and empty ext
3537
if(eq(len_dim(parts, 0d1), 0d1)){
@@ -49,7 +51,7 @@ func tensor SPLITEXT(str path){ ! returns [name without ext, ext]
4951
if( or( eq(dir, npath), eq(dir, join(npath, "/")) ) ){
5052
delex = name
5153
} else {
52-
delex = join(dir, name)
54+
delex = join(dir, "/", name)
5355
}
5456
}
5557
del("dir")
@@ -58,19 +60,19 @@ func tensor SPLITEXT(str path){ ! returns [name without ext, ext]
5860
pop("result")
5961
}
6062

61-
func str EXTNAME(str path){ ! returns extension portion of path
62-
return(SPLITEXT(path)[0d2])
63+
func str extension_name(str path){ ! returns extension portion of path
64+
return(split_extension(path)[0d2])
6365
}
6466

65-
func str DELEXT(str path){ ! returns path without extension
66-
return(SPLITEXT(path)[0d1])
67+
func str strip_extension(str path){ ! returns path without extension
68+
return(split_extension(path)[0d1])
6769
}
6870

69-
func str TEMPFILE(str local_path){
71+
func str temp_file(str local_path){
7072
if(eq(os(),"win")){
7173
str temp = "C:/Windows/Temp/"
7274
} elseif(eq(os(),"mac")){
73-
throw("TEMPFILE not implemented for macOS yet")
75+
throw("temp_file not implemented for macOS yet")
7476
! per-user temp dirs on macOS are more complex
7577
! add later
7678
} else {
@@ -82,10 +84,10 @@ func str TEMPFILE(str local_path){
8284
}
8385

8486
str interpreter = argv()[0d1]
85-
str interpreter_dir = NORMALIZE_PATH(BASEPATH(argv()[0d1]))
87+
str interpreter_dir = normalize_path(directory_name(argv()[0d1]))
8688
if(gte(len_dim(argv(), 0d1), 0d2)){
8789
str script = argv()[0d2]
88-
str script_dir = NORMALIZE_PATH(BASEPATH(argv()[0d2]))
90+
str script_dir = normalize_path(directory_name(argv()[0d2]))
8991
} else {
9092
str script = ""
9193
str script_dir = ""

‎tests/cases/operators/file-host/deletefile.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ passing:
44
source: |
55
import("path")
66
7-
str p = path.TEMPFILE("temp.txt")
7+
str p = path.temp_file("temp.txt")
88
write_file("Prefix", p)
99
assert(exist_file(p))
1010
assert(delete_file(p))
@@ -15,7 +15,7 @@ failing:
1515
source: |
1616
import("path")
1717
18-
str p = path.TEMPFILE("temp.txt")
18+
str p = path.temp_file("temp.txt")
1919
write_file("Prefix", p)
2020
assert(exist_file(p))
2121
assert(delete_file(p, "Prefix"))

‎tests/cases/operators/file-host/readfile.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ passing:
3333
source: |
3434
import("path")
3535
36-
str p = path.TEMPFILE("prefix-readfile-utf16-replacement.bin")
36+
str p = path.temp_file("prefix-readfile-utf16-replacement.bin")
3737
3838
assert(write_file("00D8", p, coding = "hex"))
3939
assert(eq(read_file(p, coding = "UTF-16 LE"), "\uFFFD"))
@@ -44,7 +44,7 @@ failing:
4444
source: |
4545
import("path")
4646
47-
str missing = path.TEMPFILE("prefix-readfile-missing.txt")
47+
str missing = path.temp_file("prefix-readfile-missing.txt")
4848
try{ delete_file(missing) } catch { }
4949
read_file(missing)
5050

‎tests/cases/operators/file-host/writefile.yaml‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ passing:
44
source: |
55
import("path")
66
7-
str p = path.TEMPFILE("prefix-writefile-ansi.txt")
7+
str p = path.temp_file("prefix-writefile-ansi.txt")
88
99
assert(write_file("Prefix", p, coding = "ANSI"))
1010
assert(eq(read_file(p, coding = "ANSI"), "Prefix"))
@@ -14,7 +14,7 @@ passing:
1414
source: |
1515
import("path")
1616
17-
str p = path.TEMPFILE("prefix-writefile-bin.bin")
17+
str p = path.temp_file("prefix-writefile-bin.bin")
1818
1919
assert(write_file("0100000101111010", p, coding = "bin"))
2020
assert(eq(read_file(p, coding = "bin"), "0100000101111010"))
@@ -25,7 +25,7 @@ passing:
2525
source: |
2626
import("path")
2727
28-
str p = path.TEMPFILE("prefix-writefile-binary.bin")
28+
str p = path.temp_file("prefix-writefile-binary.bin")
2929
3030
assert(write_file("0100000101111010", p, coding = "binary"))
3131
assert(eq(read_file(p, coding = "binary"), "0100000101111010"))
@@ -36,10 +36,10 @@ passing:
3636
source: |
3737
import("path")
3838
39-
str d = path.TEMPFILE("prefix-writefile-fail-dir")
39+
str d = path.temp_file("prefix-writefile-fail-dir")
4040
4141
if(eq(os(),"win")){
42-
assert(eq(cl(join("powershell -NoProfile -NonInteractive -Command \"New-Item -ItemType Directory -Force -Path '", path.WINPATH(d), "'\"")), 0d0))
42+
assert(eq(cl(join("powershell -NoProfile -NonInteractive -Command \"New-Item -ItemType Directory -Force -Path '", path.windows_path(d), "'\"")), 0d0))
4343
} else {
4444
assert(eq(cl(join("mkdir -p ", d)), 0d0))
4545
}
@@ -51,7 +51,7 @@ passing:
5151
source: |
5252
import("path")
5353
54-
str p = path.TEMPFILE("prefix-writefile-hex.bin")
54+
str p = path.temp_file("prefix-writefile-hex.bin")
5555
5656
assert(write_file("507265666978", p, coding = "hex"))
5757
assert(eq(read_file(p), "Prefix"))
@@ -62,7 +62,7 @@ passing:
6262
source: |
6363
import("path")
6464
65-
str p = path.TEMPFILE("prefix-writefile-utf16-be.txt")
65+
str p = path.temp_file("prefix-writefile-utf16-be.txt")
6666
6767
assert(write_file("Prefix", p, coding = "UTF-16 BE"))
6868
assert(eq(read_file(p, coding = "UTF-16 BE"), "Prefix"))
@@ -72,7 +72,7 @@ passing:
7272
source: |
7373
import("path")
7474
75-
str p = path.TEMPFILE("prefix-writefile-utf16-le.txt")
75+
str p = path.temp_file("prefix-writefile-utf16-le.txt")
7676
7777
assert(write_file("Prefix", p, coding = "UTF-16 LE"))
7878
assert(eq(read_file(p, coding = "UTF-16 LE"), "Prefix"))
@@ -82,7 +82,7 @@ passing:
8282
source: |
8383
import("path")
8484
85-
str p = path.TEMPFILE("prefix-writefile-utf8-bom.txt")
85+
str p = path.temp_file("prefix-writefile-utf8-bom.txt")
8686
8787
assert(write_file("Prefix", p, coding = "UTF-8 BOM"))
8888
assert(eq(read_file(p), "Prefix"))
@@ -93,7 +93,7 @@ passing:
9393
source: |
9494
import("path")
9595
96-
str p = path.TEMPFILE("prefix-writefile-utf8.txt")
96+
str p = path.temp_file("prefix-writefile-utf8.txt")
9797
9898
assert(write_file("Prefix", p))
9999
assert(eq(read_file(p), "Prefix"))
@@ -105,7 +105,7 @@ failing:
105105
source: |
106106
import("path")
107107
108-
str p = path.TEMPFILE("prefix-writefile-bad-binary2.txt")
108+
str p = path.temp_file("prefix-writefile-bad-binary2.txt")
109109
110110
write_file("010a0101", p, coding = "binary")
111111
@@ -114,7 +114,7 @@ failing:
114114
source: |
115115
import("path")
116116
117-
str p = path.TEMPFILE("prefix-writefile-bad-binary.txt")
117+
str p = path.temp_file("prefix-writefile-bad-binary.txt")
118118
119119
write_file("101", p, coding = "binary")
120120
@@ -123,7 +123,7 @@ failing:
123123
source: |
124124
import("path")
125125
126-
str p = path.TEMPFILE("prefix-writefile-bad-hex.txt")
126+
str p = path.temp_file("prefix-writefile-bad-hex.txt")
127127
128128
write_file("abc", p, coding = "hex")
129129
@@ -132,7 +132,7 @@ failing:
132132
source: |
133133
import("path")
134134
135-
str p = path.TEMPFILE("prefix-writefile-unsupported.txt")
135+
str p = path.temp_file("prefix-writefile-unsupported.txt")
136136
137137
write_file("Prefix", p, coding = "UTF-7")
138138

0 commit comments

Comments
 (0)