Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Nov 19, 2023
1 parent 6077d1f commit 96d4aac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ func TrimFileName(ppath string) string {
return ``
}

func BaseFileName(ppath string) string {
if len(ppath) == 0 {
return ppath
}
for i := len(ppath) - 1; i >= 0; i-- {
if ppath[i] == '/' || ppath[i] == '\\' {
if i+1 < len(ppath) {
return ppath[i+1:]
}
return BaseFileName(ppath[0:i])
}
}
return ppath
}

// FileSize returns file size in bytes and possible error.
func FileSize(file string) (int64, error) {
f, err := os.Stat(file)
Expand Down
11 changes: 11 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ func TestFileIsCompleted(t *testing.T) {

wg.Wait()
}

func TestBaseFileName(t *testing.T) {
r := BaseFileName(`abc/dd.txt`)
assert.Equal(t, `dd.txt`, r)
r = BaseFileName(`abc\dd.txt`)
assert.Equal(t, `dd.txt`, r)
r = BaseFileName(`abc\dd.txt/`)
assert.Equal(t, `dd.txt`, r)
r = BaseFileName(`/`)
assert.Equal(t, ``, r)
}

0 comments on commit 96d4aac

Please sign in to comment.