| ... | ... | @@ -28,6 +28,7 @@ pub const delimiter_windows = ';'; |
| 28 | 28 | pub const delimiter_posix = ':'; |
| 29 | 29 | pub const delimiter = if (builtin.os.tag == .windows) delimiter_windows else delimiter_posix; |
| 30 | 30 | |
| 31 | /// Returns if the given byte is a valid path separator |
| 31 | 32 | pub fn isSep(byte: u8) bool { |
| 32 | 33 | if (builtin.os.tag == .windows) { |
| 33 | 34 | return byte == '/' or byte == '\\'; |
| ... | ... | @@ -1188,3 +1189,70 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons |
| 1188 | 1189 | defer testing.allocator.free(result); |
| 1189 | 1190 | testing.expectEqualSlices(u8, expected_output, result); |
| 1190 | 1191 | } |
| 1192 | |
| 1193 | /// Returns the extension of the file name (if any). |
| 1194 | /// This function will search for the file extension (separated by a `.`) and will return the text after the `.`. |
| 1195 | /// Files that end with `.` are considered to have no extension, files that start with `.` |
| 1196 | /// Examples: |
| 1197 | /// - `"main.zig"` ⇒ `".zig"` |
| 1198 | /// - `"src/main.zig"` ⇒ `".zig"` |
| 1199 | /// - `".gitignore"` ⇒ `""` |
| 1200 | /// - `"keep."` ⇒ `""` |
| 1201 | /// - `"src.keep.me"` ⇒ `".me"` |
| 1202 | /// - `"/src/keep.me"` ⇒ `".me"` |
| 1203 | /// - `"/src/keep.me/"` ⇒ `".me"` |
| 1204 | pub fn extension(path: []const u8) []const u8 { |
| 1205 | const filename = basename(path); |
| 1206 | return if (std.mem.lastIndexOf(u8, filename, ".")) |index| |
| 1207 | if (index == 0 or index == filename.len - 1) |
| 1208 | "" |
| 1209 | else |
| 1210 | filename[index..] |
| 1211 | else |
| 1212 | ""; |
| 1213 | } |
| 1214 | |
| 1215 | fn testExtension(path: []const u8, expected: []const u8) void { |
| 1216 | std.testing.expectEqualStrings(expected, extension(path)); |
| 1217 | } |
| 1218 | |
| 1219 | test "extension" { |
| 1220 | testExtension("", ""); |
| 1221 | testExtension(".", ""); |
| 1222 | testExtension("a.", ""); |
| 1223 | testExtension("abc.", ""); |
| 1224 | testExtension(".a", ""); |
| 1225 | testExtension(".file", ""); |
| 1226 | testExtension(".gitignore", ""); |
| 1227 | testExtension("file.ext", ".ext"); |
| 1228 | testExtension("file.ext.", ""); |
| 1229 | testExtension("very-long-file.bruh", ".bruh"); |
| 1230 | testExtension("a.b.c", ".c"); |
| 1231 | testExtension("a.b.c/", ".c"); |
| 1232 | |
| 1233 | testExtension("/", ""); |
| 1234 | testExtension("/.", ""); |
| 1235 | testExtension("/a.", ""); |
| 1236 | testExtension("/abc.", ""); |
| 1237 | testExtension("/.a", ""); |
| 1238 | testExtension("/.file", ""); |
| 1239 | testExtension("/.gitignore", ""); |
| 1240 | testExtension("/file.ext", ".ext"); |
| 1241 | testExtension("/file.ext.", ""); |
| 1242 | testExtension("/very-long-file.bruh", ".bruh"); |
| 1243 | testExtension("/a.b.c", ".c"); |
| 1244 | testExtension("/a.b.c/", ".c"); |
| 1245 | |
| 1246 | testExtension("/foo/bar/bam/", ""); |
| 1247 | testExtension("/foo/bar/bam/.", ""); |
| 1248 | testExtension("/foo/bar/bam/a.", ""); |
| 1249 | testExtension("/foo/bar/bam/abc.", ""); |
| 1250 | testExtension("/foo/bar/bam/.a", ""); |
| 1251 | testExtension("/foo/bar/bam/.file", ""); |
| 1252 | testExtension("/foo/bar/bam/.gitignore", ""); |
| 1253 | testExtension("/foo/bar/bam/file.ext", ".ext"); |
| 1254 | testExtension("/foo/bar/bam/file.ext.", ""); |
| 1255 | testExtension("/foo/bar/bam/very-long-file.bruh", ".bruh"); |
| 1256 | testExtension("/foo/bar/bam/a.b.c", ".c"); |
| 1257 | testExtension("/foo/bar/bam/a.b.c/", ".c"); |
| 1258 | } |