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...@@ -1197,7 +1197,7 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons
1197/// - `"main.zig"` ⇒ `".zig"`1197/// - `"main.zig"` ⇒ `".zig"`
1198/// - `"src/main.zig"` ⇒ `".zig"`1198/// - `"src/main.zig"` ⇒ `".zig"`
1199/// - `".gitignore"` ⇒ `""`1199/// - `".gitignore"` ⇒ `""`
1200/// - `"keep."` ⇒ `""`1200/// - `"keep."` ⇒ `"."`
1201/// - `"src.keep.me"` ⇒ `".me"`1201/// - `"src.keep.me"` ⇒ `".me"`
1202/// - `"/src/keep.me"` ⇒ `".me"`1202/// - `"/src/keep.me"` ⇒ `".me"`
1203/// - `"/src/keep.me/"` ⇒ `".me"`1203/// - `"/src/keep.me/"` ⇒ `".me"`
...@@ -1205,13 +1205,9 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons...@@ -1205,13 +1205,9 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons
1205/// pointer address range of `path`, even if it is length zero.1205/// pointer address range of `path`, even if it is length zero.
1206pub fn extension(path: []const u8) []const u8 {1206pub fn extension(path: []const u8) []const u8 {
1207 const filename = basename(path);1207 const filename = basename(path);
1208 return if (std.mem.lastIndexOf(u8, filename, ".")) |index|1208 const index = mem.lastIndexOf(u8, filename, ".") orelse return path[path.len..];
1209 if (index == 0 or index == filename.len - 1)1209 if (index == 0) return path[path.len..];
1210 path[path.len..]1210 return filename[index..];
1211 else
1212 filename[index..]
1213 else
1214 path[path.len..];
1215}1211}
12161212
1217fn testExtension(path: []const u8, expected: []const u8) void {1213fn testExtension(path: []const u8, expected: []const u8) void {
...@@ -1221,39 +1217,39 @@ fn testExtension(path: []const u8, expected: []const u8) void {...@@ -1221,39 +1217,39 @@ fn testExtension(path: []const u8, expected: []const u8) void {
1221test "extension" {1217test "extension" {
1222 testExtension("", "");1218 testExtension("", "");
1223 testExtension(".", "");1219 testExtension(".", "");
1224 testExtension("a.", "");1220 testExtension("a.", ".");
1225 testExtension("abc.", "");1221 testExtension("abc.", ".");
1226 testExtension(".a", "");1222 testExtension(".a", "");
1227 testExtension(".file", "");1223 testExtension(".file", "");
1228 testExtension(".gitignore", "");1224 testExtension(".gitignore", "");
1229 testExtension("file.ext", ".ext");1225 testExtension("file.ext", ".ext");
1230 testExtension("file.ext.", "");1226 testExtension("file.ext.", ".");
1231 testExtension("very-long-file.bruh", ".bruh");1227 testExtension("very-long-file.bruh", ".bruh");
1232 testExtension("a.b.c", ".c");1228 testExtension("a.b.c", ".c");
1233 testExtension("a.b.c/", ".c");1229 testExtension("a.b.c/", ".c");
12341230
1235 testExtension("/", "");1231 testExtension("/", "");
1236 testExtension("/.", "");1232 testExtension("/.", "");
1237 testExtension("/a.", "");1233 testExtension("/a.", ".");
1238 testExtension("/abc.", "");1234 testExtension("/abc.", ".");
1239 testExtension("/.a", "");1235 testExtension("/.a", "");
1240 testExtension("/.file", "");1236 testExtension("/.file", "");
1241 testExtension("/.gitignore", "");1237 testExtension("/.gitignore", "");
1242 testExtension("/file.ext", ".ext");1238 testExtension("/file.ext", ".ext");
1243 testExtension("/file.ext.", "");1239 testExtension("/file.ext.", ".");
1244 testExtension("/very-long-file.bruh", ".bruh");1240 testExtension("/very-long-file.bruh", ".bruh");
1245 testExtension("/a.b.c", ".c");1241 testExtension("/a.b.c", ".c");
1246 testExtension("/a.b.c/", ".c");1242 testExtension("/a.b.c/", ".c");
12471243
1248 testExtension("/foo/bar/bam/", "");1244 testExtension("/foo/bar/bam/", "");
1249 testExtension("/foo/bar/bam/.", "");1245 testExtension("/foo/bar/bam/.", "");
1250 testExtension("/foo/bar/bam/a.", "");1246 testExtension("/foo/bar/bam/a.", ".");
1251 testExtension("/foo/bar/bam/abc.", "");1247 testExtension("/foo/bar/bam/abc.", ".");
1252 testExtension("/foo/bar/bam/.a", "");1248 testExtension("/foo/bar/bam/.a", "");
1253 testExtension("/foo/bar/bam/.file", "");1249 testExtension("/foo/bar/bam/.file", "");
1254 testExtension("/foo/bar/bam/.gitignore", "");1250 testExtension("/foo/bar/bam/.gitignore", "");
1255 testExtension("/foo/bar/bam/file.ext", ".ext");1251 testExtension("/foo/bar/bam/file.ext", ".ext");
1256 testExtension("/foo/bar/bam/file.ext.", "");1252 testExtension("/foo/bar/bam/file.ext.", ".");
1257 testExtension("/foo/bar/bam/very-long-file.bruh", ".bruh");1253 testExtension("/foo/bar/bam/very-long-file.bruh", ".bruh");
1258 testExtension("/foo/bar/bam/a.b.c", ".c");1254 testExtension("/foo/bar/bam/a.b.c", ".c");
1259 testExtension("/foo/bar/bam/a.b.c/", ".c");1255 testExtension("/foo/bar/bam/a.b.c/", ".c");