mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
Respects leading . and ignores it.
This commit is contained in:
parent
2554273805
commit
f51064f79e
1 changed files with 20 additions and 11 deletions
|
|
@ -1185,11 +1185,17 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons
|
||||||
|
|
||||||
/// Returns the extension of the file name (if any).
|
/// Returns the extension of the file name (if any).
|
||||||
/// This function will search for the file extension (separated by a `.`) and will return the text after the `.`.
|
/// This function will search for the file extension (separated by a `.`) and will return the text after the `.`.
|
||||||
/// Files that end with `.` are considered to have no extension.
|
/// Files that end with `.` are considered to have no extension, files that start with `.`
|
||||||
|
/// Examples:
|
||||||
|
/// - `"main.zig"` ⇒ `"zig"`
|
||||||
|
/// - `"src/main.zig"` ⇒ `"zig"`
|
||||||
|
/// - `".gitignore"` ⇒ `null`
|
||||||
|
/// - `"keep."` ⇒ `null`
|
||||||
|
/// - `"src.keep.me"` ⇒ `"me"`
|
||||||
pub fn extension(path: []const u8) ?[]const u8 {
|
pub fn extension(path: []const u8) ?[]const u8 {
|
||||||
const filename = basename(path);
|
const filename = basename(path);
|
||||||
return if (std.mem.lastIndexOf(u8, filename, ".")) |index|
|
return if (std.mem.lastIndexOf(u8, filename, ".")) |index|
|
||||||
if (index == filename.len - 1)
|
if (index == 0 or index == filename.len - 1)
|
||||||
null
|
null
|
||||||
else
|
else
|
||||||
filename[index + 1 ..]
|
filename[index + 1 ..]
|
||||||
|
|
@ -1213,29 +1219,32 @@ test "extension" {
|
||||||
testExtension(".", null);
|
testExtension(".", null);
|
||||||
testExtension("a.", null);
|
testExtension("a.", null);
|
||||||
testExtension("abc.", null);
|
testExtension("abc.", null);
|
||||||
testExtension(".a", "a");
|
testExtension(".a", null);
|
||||||
testExtension(".file", "file");
|
testExtension(".file", null);
|
||||||
testExtension(".gitignore", "gitignore");
|
testExtension(".gitignore", null);
|
||||||
testExtension("file.gitignore", "gitignore");
|
testExtension("file.gitignore", "gitignore");
|
||||||
testExtension("a.gitignore", "gitignore");
|
testExtension("a.gitignore", "gitignore");
|
||||||
|
testExtension("a.b.c", "c");
|
||||||
|
|
||||||
testExtension("/", null);
|
testExtension("/", null);
|
||||||
testExtension("/.", null);
|
testExtension("/.", null);
|
||||||
testExtension("/a.", null);
|
testExtension("/a.", null);
|
||||||
testExtension("/abc.", null);
|
testExtension("/abc.", null);
|
||||||
testExtension("/.a", "a");
|
testExtension("/.a", null);
|
||||||
testExtension("/.file", "file");
|
testExtension("/.file", null);
|
||||||
testExtension("/.gitignore", "gitignore");
|
testExtension("/.gitignore", null);
|
||||||
testExtension("/file.gitignore", "gitignore");
|
testExtension("/file.gitignore", "gitignore");
|
||||||
testExtension("/a.gitignore", "gitignore");
|
testExtension("/a.gitignore", "gitignore");
|
||||||
|
testExtension("/a.b.c", "c");
|
||||||
|
|
||||||
testExtension("/foo/bar/bam/", null);
|
testExtension("/foo/bar/bam/", null);
|
||||||
testExtension("/foo/bar/bam/.", null);
|
testExtension("/foo/bar/bam/.", null);
|
||||||
testExtension("/foo/bar/bam/a.", null);
|
testExtension("/foo/bar/bam/a.", null);
|
||||||
testExtension("/foo/bar/bam/abc.", null);
|
testExtension("/foo/bar/bam/abc.", null);
|
||||||
testExtension("/foo/bar/bam/.a", "a");
|
testExtension("/foo/bar/bam/.a", null);
|
||||||
testExtension("/foo/bar/bam/.file", "file");
|
testExtension("/foo/bar/bam/.file", null);
|
||||||
testExtension("/foo/bar/bam/.gitignore", "gitignore");
|
testExtension("/foo/bar/bam/.gitignore", null);
|
||||||
testExtension("/foo/bar/bam/file.gitignore", "gitignore");
|
testExtension("/foo/bar/bam/file.gitignore", "gitignore");
|
||||||
testExtension("/foo/bar/bam/a.gitignore", "gitignore");
|
testExtension("/foo/bar/bam/a.gitignore", "gitignore");
|
||||||
|
testExtension("/foo/bar/bam/a.b.c", "c");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue