| ... | @@ -1185,11 +1185,17 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons | ... | @@ -1185,11 +1185,17 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons |
| 1185 | | 1185 | |
| 1186 | /// Returns the extension of the file name (if any). | 1186 | /// Returns the extension of the file name (if any). |
| 1187 | /// This function will search for the file extension (separated by a `.`) and will return the text after the `.`. | 1187 | /// This function will search for the file extension (separated by a `.`) and will return the text after the `.`. |
| 1188 | /// Files that end with `.` are considered to have no extension. | 1188 | /// Files that end with `.` are considered to have no extension, files that start with `.` |
| | 1189 | /// Examples: |
| | 1190 | /// - `"main.zig"` ⇒ `"zig"` |
| | 1191 | /// - `"src/main.zig"` ⇒ `"zig"` |
| | 1192 | /// - `".gitignore"` ⇒ `null` |
| | 1193 | /// - `"keep."` ⇒ `null` |
| | 1194 | /// - `"src.keep.me"` ⇒ `"me"` |
| 1189 | pub fn extension(path: []const u8) ?[]const u8 { | 1195 | pub fn extension(path: []const u8) ?[]const u8 { |
| 1190 | const filename = basename(path); | 1196 | const filename = basename(path); |
| 1191 | return if (std.mem.lastIndexOf(u8, filename, ".")) |index| | 1197 | return if (std.mem.lastIndexOf(u8, filename, ".")) |index| |
| 1192 | if (index == filename.len - 1) | 1198 | if (index == 0 or index == filename.len - 1) |
| 1193 | null | 1199 | null |
| 1194 | else | 1200 | else |
| 1195 | filename[index + 1 ..] | 1201 | filename[index + 1 ..] |
| ... | @@ -1213,29 +1219,32 @@ test "extension" { | ... | @@ -1213,29 +1219,32 @@ test "extension" { |
| 1213 | testExtension(".", null); | 1219 | testExtension(".", null); |
| 1214 | testExtension("a.", null); | 1220 | testExtension("a.", null); |
| 1215 | testExtension("abc.", null); | 1221 | testExtension("abc.", null); |
| 1216 | testExtension(".a", "a"); | 1222 | testExtension(".a", null); |
| 1217 | testExtension(".file", "file"); | 1223 | testExtension(".file", null); |
| 1218 | testExtension(".gitignore", "gitignore"); | 1224 | testExtension(".gitignore", null); |
| 1219 | testExtension("file.gitignore", "gitignore"); | 1225 | testExtension("file.gitignore", "gitignore"); |
| 1220 | testExtension("a.gitignore", "gitignore"); | 1226 | testExtension("a.gitignore", "gitignore"); |
| | 1227 | testExtension("a.b.c", "c"); |
| 1221 | | 1228 | |
| 1222 | testExtension("/", null); | 1229 | testExtension("/", null); |
| 1223 | testExtension("/.", null); | 1230 | testExtension("/.", null); |
| 1224 | testExtension("/a.", null); | 1231 | testExtension("/a.", null); |
| 1225 | testExtension("/abc.", null); | 1232 | testExtension("/abc.", null); |
| 1226 | testExtension("/.a", "a"); | 1233 | testExtension("/.a", null); |
| 1227 | testExtension("/.file", "file"); | 1234 | testExtension("/.file", null); |
| 1228 | testExtension("/.gitignore", "gitignore"); | 1235 | testExtension("/.gitignore", null); |
| 1229 | testExtension("/file.gitignore", "gitignore"); | 1236 | testExtension("/file.gitignore", "gitignore"); |
| 1230 | testExtension("/a.gitignore", "gitignore"); | 1237 | testExtension("/a.gitignore", "gitignore"); |
| | 1238 | testExtension("/a.b.c", "c"); |
| 1231 | | 1239 | |
| 1232 | testExtension("/foo/bar/bam/", null); | 1240 | testExtension("/foo/bar/bam/", null); |
| 1233 | testExtension("/foo/bar/bam/.", null); | 1241 | testExtension("/foo/bar/bam/.", null); |
| 1234 | testExtension("/foo/bar/bam/a.", null); | 1242 | testExtension("/foo/bar/bam/a.", null); |
| 1235 | testExtension("/foo/bar/bam/abc.", null); | 1243 | testExtension("/foo/bar/bam/abc.", null); |
| 1236 | testExtension("/foo/bar/bam/.a", "a"); | 1244 | testExtension("/foo/bar/bam/.a", null); |
| 1237 | testExtension("/foo/bar/bam/.file", "file"); | 1245 | testExtension("/foo/bar/bam/.file", null); |
| 1238 | testExtension("/foo/bar/bam/.gitignore", "gitignore"); | 1246 | testExtension("/foo/bar/bam/.gitignore", null); |
| 1239 | testExtension("/foo/bar/bam/file.gitignore", "gitignore"); | 1247 | testExtension("/foo/bar/bam/file.gitignore", "gitignore"); |
| 1240 | testExtension("/foo/bar/bam/a.gitignore", "gitignore"); | 1248 | testExtension("/foo/bar/bam/a.gitignore", "gitignore"); |
| | 1249 | testExtension("/foo/bar/bam/a.b.c", "c"); |
| 1241 | } | 1250 | } |