authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-11 18:20:57-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-11 18:23:57-07:00
loga42c7129347e6e5668b1001ff6f2fc97a89b0510
tree224bb1b12fff4c7e077ee214d8c99d41b9f2d5c9
parent1bbf731e7ef6ca738026ee82a8f50bf114efbf8c

std.fs.path.extension: different behavior for ending dot

extension("a.") now returns "." instead of "". This matches both Python and Node.js standard library behavior as well as my personal opinion on how this function should be defined. Apologies for missing this in the code review.

1 files changed, 13 insertions(+), 17 deletions(-)

lib/std/fs/path.zig+13-17
......@@ -1197,7 +1197,7 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons
11971197/// - `"main.zig"` ⇒ `".zig"`
11981198/// - `"src/main.zig"` ⇒ `".zig"`
11991199/// - `".gitignore"` ⇒ `""`
1200/// - `"keep."` ⇒ `""`
1200/// - `"keep."` ⇒ `"."`
12011201/// - `"src.keep.me"` ⇒ `".me"`
12021202/// - `"/src/keep.me"` ⇒ `".me"`
12031203/// - `"/src/keep.me/"` ⇒ `".me"`
......@@ -1205,13 +1205,9 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons
12051205/// pointer address range of `path`, even if it is length zero.
12061206pub fn extension(path: []const u8) []const u8 {
12071207 const filename = basename(path);
1208 return if (std.mem.lastIndexOf(u8, filename, ".")) |index|
1209 if (index == 0 or index == filename.len - 1)
1210 path[path.len..]
1211 else
1212 filename[index..]
1213 else
1214 path[path.len..];
1208 const index = mem.lastIndexOf(u8, filename, ".") orelse return path[path.len..];
1209 if (index == 0) return path[path.len..];
1210 return filename[index..];
12151211}
12161212
12171213fn testExtension(path: []const u8, expected: []const u8) void {
......@@ -1221,39 +1217,39 @@ fn testExtension(path: []const u8, expected: []const u8) void {
12211217test "extension" {
12221218 testExtension("", "");
12231219 testExtension(".", "");
1224 testExtension("a.", "");
1225 testExtension("abc.", "");
1220 testExtension("a.", ".");
1221 testExtension("abc.", ".");
12261222 testExtension(".a", "");
12271223 testExtension(".file", "");
12281224 testExtension(".gitignore", "");
12291225 testExtension("file.ext", ".ext");
1230 testExtension("file.ext.", "");
1226 testExtension("file.ext.", ".");
12311227 testExtension("very-long-file.bruh", ".bruh");
12321228 testExtension("a.b.c", ".c");
12331229 testExtension("a.b.c/", ".c");
12341230
12351231 testExtension("/", "");
12361232 testExtension("/.", "");
1237 testExtension("/a.", "");
1238 testExtension("/abc.", "");
1233 testExtension("/a.", ".");
1234 testExtension("/abc.", ".");
12391235 testExtension("/.a", "");
12401236 testExtension("/.file", "");
12411237 testExtension("/.gitignore", "");
12421238 testExtension("/file.ext", ".ext");
1243 testExtension("/file.ext.", "");
1239 testExtension("/file.ext.", ".");
12441240 testExtension("/very-long-file.bruh", ".bruh");
12451241 testExtension("/a.b.c", ".c");
12461242 testExtension("/a.b.c/", ".c");
12471243
12481244 testExtension("/foo/bar/bam/", "");
12491245 testExtension("/foo/bar/bam/.", "");
1250 testExtension("/foo/bar/bam/a.", "");
1251 testExtension("/foo/bar/bam/abc.", "");
1246 testExtension("/foo/bar/bam/a.", ".");
1247 testExtension("/foo/bar/bam/abc.", ".");
12521248 testExtension("/foo/bar/bam/.a", "");
12531249 testExtension("/foo/bar/bam/.file", "");
12541250 testExtension("/foo/bar/bam/.gitignore", "");
12551251 testExtension("/foo/bar/bam/file.ext", ".ext");
1256 testExtension("/foo/bar/bam/file.ext.", "");
1252 testExtension("/foo/bar/bam/file.ext.", ".");
12571253 testExtension("/foo/bar/bam/very-long-file.bruh", ".bruh");
12581254 testExtension("/foo/bar/bam/a.b.c", ".c");
12591255 testExtension("/foo/bar/bam/a.b.c/", ".c");