Skip to content

Commit 96d4aac

Browse files
committed
update
1 parent 6077d1f commit 96d4aac

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

file.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ func TrimFileName(ppath string) string {
9494
return ``
9595
}
9696

97+
func BaseFileName(ppath string) string {
98+
if len(ppath) == 0 {
99+
return ppath
100+
}
101+
for i := len(ppath) - 1; i >= 0; i-- {
102+
if ppath[i] == '/' || ppath[i] == '\\' {
103+
if i+1 < len(ppath) {
104+
return ppath[i+1:]
105+
}
106+
return BaseFileName(ppath[0:i])
107+
}
108+
}
109+
return ppath
110+
}
111+
97112
// FileSize returns file size in bytes and possible error.
98113
func FileSize(file string) (int64, error) {
99114
f, err := os.Stat(file)

file_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,14 @@ func TestFileIsCompleted(t *testing.T) {
4343

4444
wg.Wait()
4545
}
46+
47+
func TestBaseFileName(t *testing.T) {
48+
r := BaseFileName(`abc/dd.txt`)
49+
assert.Equal(t, `dd.txt`, r)
50+
r = BaseFileName(`abc\dd.txt`)
51+
assert.Equal(t, `dd.txt`, r)
52+
r = BaseFileName(`abc\dd.txt/`)
53+
assert.Equal(t, `dd.txt`, r)
54+
r = BaseFileName(`/`)
55+
assert.Equal(t, ``, r)
56+
}

0 commit comments

Comments
 (0)