authorgravatar for xq@random-projects.netFelix "xq" Queißner <xq@random-projects.net> 2020-11-18 00:03:24+01:00
committergravatar for xq@random-projects.netFelix "xq" Queißner <xq@random-projects.net> 2020-11-18 00:03:34+01:00
logb3af2f68b38c542f7d8bb5b1292c5301a07f9cac
tree38336ddd23a7222660a63100f0e3ea9e395e602b
parent0bbf170514c74a70cf7fa5a9c044335a8f9dddd4

Changes behaviour from std.fs.path.extension from returning `null` to returning `""`.


1 files changed, 39 insertions(+), 37 deletions(-)

lib/std/fs/path.zig+39-37
......@@ -28,6 +28,7 @@ pub const delimiter_windows = ';';
2828pub const delimiter_posix = ':';
2929pub const delimiter = if (builtin.os.tag == .windows) delimiter_windows else delimiter_posix;
3030
31/// Returns if the given byte is a valid path separator
3132pub fn isSep(byte: u8) bool {
3233 if (builtin.os.tag == .windows) {
3334 return byte == '/' or byte == '\\';
......@@ -1189,62 +1190,63 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons
11891190/// Examples:
11901191/// - `"main.zig"` ⇒ `".zig"`
11911192/// - `"src/main.zig"` ⇒ `".zig"`
1192/// - `".gitignore"` ⇒ `null`
1193/// - `"keep."` ⇒ `null`
1193/// - `".gitignore"` ⇒ `""`
1194/// - `"keep."` ⇒ `""`
11941195/// - `"src.keep.me"` ⇒ `".me"`
1195pub fn extension(path: []const u8) ?[]const u8 {
1196/// - `"/src/keep.me"` ⇒ `".me"`
1197/// - `"/src/keep.me/"` ⇒ `".me"`
1198pub fn extension(path: []const u8) []const u8 {
11961199 const filename = basename(path);
11971200 return if (std.mem.lastIndexOf(u8, filename, ".")) |index|
11981201 if (index == 0 or index == filename.len - 1)
1199 null
1202 ""
12001203 else
12011204 filename[index..]
12021205 else
1203 null;
1206 "";
12041207}
12051208
1206fn testExtension(path: []const u8, expected: ?[]const u8) void {
1207 const actual = extension(path);
1208
1209 if (expected) |must_be| {
1210 std.testing.expect(actual != null);
1211 std.testing.expectEqualStrings(must_be, actual.?);
1212 } else {
1213 std.testing.expectEqual(expected, actual);
1214 }
1209fn testExtension(path: []const u8, expected: []const u8) void {
1210 std.testing.expectEqualStrings(expected, extension(path));
12151211}
12161212
12171213test "extension" {
1218 testExtension("", null);
1219 testExtension(".", null);
1220 testExtension("a.", null);
1221 testExtension("abc.", null);
1222 testExtension(".a", null);
1223 testExtension(".file", null);
1224 testExtension(".gitignore", null);
1214 testExtension("", "");
1215 testExtension(".", "");
1216 testExtension("a.", "");
1217 testExtension("abc.", "");
1218 testExtension(".a", "");
1219 testExtension(".file", "");
1220 testExtension(".gitignore", "");
12251221 testExtension("file.ext", ".ext");
1222 testExtension("file.ext.", "");
12261223 testExtension("very-long-file.bruh", ".bruh");
12271224 testExtension("a.b.c", ".c");
1228
1229 testExtension("/", null);
1230 testExtension("/.", null);
1231 testExtension("/a.", null);
1232 testExtension("/abc.", null);
1233 testExtension("/.a", null);
1234 testExtension("/.file", null);
1235 testExtension("/.gitignore", null);
1225 testExtension("a.b.c/", ".c");
1226
1227 testExtension("/", "");
1228 testExtension("/.", "");
1229 testExtension("/a.", "");
1230 testExtension("/abc.", "");
1231 testExtension("/.a", "");
1232 testExtension("/.file", "");
1233 testExtension("/.gitignore", "");
12361234 testExtension("/file.ext", ".ext");
1235 testExtension("/file.ext.", "");
12371236 testExtension("/very-long-file.bruh", ".bruh");
12381237 testExtension("/a.b.c", ".c");
1239
1240 testExtension("/foo/bar/bam/", null);
1241 testExtension("/foo/bar/bam/.", null);
1242 testExtension("/foo/bar/bam/a.", null);
1243 testExtension("/foo/bar/bam/abc.", null);
1244 testExtension("/foo/bar/bam/.a", null);
1245 testExtension("/foo/bar/bam/.file", null);
1246 testExtension("/foo/bar/bam/.gitignore", null);
1238 testExtension("/a.b.c/", ".c");
1239
1240 testExtension("/foo/bar/bam/", "");
1241 testExtension("/foo/bar/bam/.", "");
1242 testExtension("/foo/bar/bam/a.", "");
1243 testExtension("/foo/bar/bam/abc.", "");
1244 testExtension("/foo/bar/bam/.a", "");
1245 testExtension("/foo/bar/bam/.file", "");
1246 testExtension("/foo/bar/bam/.gitignore", "");
12471247 testExtension("/foo/bar/bam/file.ext", ".ext");
1248 testExtension("/foo/bar/bam/file.ext.", "");
12481249 testExtension("/foo/bar/bam/very-long-file.bruh", ".bruh");
12491250 testExtension("/foo/bar/bam/a.b.c", ".c");
1251 testExtension("/foo/bar/bam/a.b.c/", ".c");
12501252}