authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-11 19:32:21-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-12-11 19:32:21-05:00
log1852dc7e13b49a0b3238b8d654d09c4fc91aa3e4
tree1898f8a94bcf06b0c6cf45a30c8497fcadac8c6b
parentc4f53d1ef65b7942383471876e02a1bd03d37dd9
parentb3af2f68b38c542f7d8bb5b1292c5301a07f9cac
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #7098 from MasterQ32/std.fs.path.extension

Implements std.fs.path.extension

1 files changed, 68 insertions(+), 0 deletions(-)

lib/std/fs/path.zig+68
...@@ -28,6 +28,7 @@ pub const delimiter_windows = ';';...@@ -28,6 +28,7 @@ pub const delimiter_windows = ';';
28pub const delimiter_posix = ':';28pub const delimiter_posix = ':';
29pub const delimiter = if (builtin.os.tag == .windows) delimiter_windows else delimiter_posix;29pub const delimiter = if (builtin.os.tag == .windows) delimiter_windows else delimiter_posix;
3030
31/// Returns if the given byte is a valid path separator
31pub fn isSep(byte: u8) bool {32pub fn isSep(byte: u8) bool {
32 if (builtin.os.tag == .windows) {33 if (builtin.os.tag == .windows) {
33 return byte == '/' or byte == '\\';34 return byte == '/' or byte == '\\';
...@@ -1188,3 +1189,70 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons...@@ -1188,3 +1189,70 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons
1188 defer testing.allocator.free(result);1189 defer testing.allocator.free(result);
1189 testing.expectEqualSlices(u8, expected_output, result);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"`
1204pub 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
1215fn testExtension(path: []const u8, expected: []const u8) void {
1216 std.testing.expectEqualStrings(expected, extension(path));
1217}
1218
1219test "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}