| ... | ... | @@ -1185,11 +1185,17 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons |
| 1185 | 1185 | |
| 1186 | 1186 | /// Returns the extension of the file name (if any). |
| 1187 | 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 | 1195 | pub fn extension(path: []const u8) ?[]const u8 { |
| 1190 | 1196 | const filename = basename(path); |
| 1191 | 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 | 1199 | null |
| 1194 | 1200 | else |
| 1195 | 1201 | filename[index + 1 ..] |
| ... | ... | @@ -1213,29 +1219,32 @@ test "extension" { |
| 1213 | 1219 | testExtension(".", null); |
| 1214 | 1220 | testExtension("a.", null); |
| 1215 | 1221 | testExtension("abc.", null); |
| 1216 | | testExtension(".a", "a"); |
| 1217 | | testExtension(".file", "file"); |
| 1218 | | testExtension(".gitignore", "gitignore"); |
| 1222 | testExtension(".a", null); |
| 1223 | testExtension(".file", null); |
| 1224 | testExtension(".gitignore", null); |
| 1219 | 1225 | testExtension("file.gitignore", "gitignore"); |
| 1220 | 1226 | testExtension("a.gitignore", "gitignore"); |
| 1227 | testExtension("a.b.c", "c"); |
| 1221 | 1228 | |
| 1222 | 1229 | testExtension("/", null); |
| 1223 | 1230 | testExtension("/.", null); |
| 1224 | 1231 | testExtension("/a.", null); |
| 1225 | 1232 | testExtension("/abc.", null); |
| 1226 | | testExtension("/.a", "a"); |
| 1227 | | testExtension("/.file", "file"); |
| 1228 | | testExtension("/.gitignore", "gitignore"); |
| 1233 | testExtension("/.a", null); |
| 1234 | testExtension("/.file", null); |
| 1235 | testExtension("/.gitignore", null); |
| 1229 | 1236 | testExtension("/file.gitignore", "gitignore"); |
| 1230 | 1237 | testExtension("/a.gitignore", "gitignore"); |
| 1238 | testExtension("/a.b.c", "c"); |
| 1231 | 1239 | |
| 1232 | 1240 | testExtension("/foo/bar/bam/", null); |
| 1233 | 1241 | testExtension("/foo/bar/bam/.", null); |
| 1234 | 1242 | testExtension("/foo/bar/bam/a.", null); |
| 1235 | 1243 | testExtension("/foo/bar/bam/abc.", null); |
| 1236 | | testExtension("/foo/bar/bam/.a", "a"); |
| 1237 | | testExtension("/foo/bar/bam/.file", "file"); |
| 1238 | | testExtension("/foo/bar/bam/.gitignore", "gitignore"); |
| 1244 | testExtension("/foo/bar/bam/.a", null); |
| 1245 | testExtension("/foo/bar/bam/.file", null); |
| 1246 | testExtension("/foo/bar/bam/.gitignore", null); |
| 1239 | 1247 | testExtension("/foo/bar/bam/file.gitignore", "gitignore"); |
| 1240 | 1248 | testExtension("/foo/bar/bam/a.gitignore", "gitignore"); |
| 1249 | testExtension("/foo/bar/bam/a.b.c", "c"); |
| 1241 | 1250 | } |