File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -94,6 +94,21 @@ func TrimFileName(ppath string) string {
94
94
return ``
95
95
}
96
96
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
+
97
112
// FileSize returns file size in bytes and possible error.
98
113
func FileSize (file string ) (int64 , error ) {
99
114
f , err := os .Stat (file )
Original file line number Diff line number Diff line change @@ -43,3 +43,14 @@ func TestFileIsCompleted(t *testing.T) {
43
43
44
44
wg .Wait ()
45
45
}
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
+ }
You can’t perform that action at this time.
0 commit comments