Fix ClearPath()

This commit is contained in:
Eugeny Leonov 2022-10-07 10:03:09 +05:00
parent 6ce210a00a
commit 51abd3cf47
2 changed files with 19 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package file
import (
"fmt"
"strconv"
"sync"
"testing"
@ -45,3 +46,13 @@ func TestFileCreate(t *testing.T) {
tw.Wait()
//< Threaded write =
}
func TestClearPath(t *testing.T) {
exp := "./path/to/file/with.ext"
fn := "....//path/////to/////file/with....ext"
r := ClearPath(fn)
if exp != r {
t.Errorf("%s not match %s", exp, r)
}
fmt.Println(r)
}

View File

@ -7,6 +7,7 @@ import (
"os"
"os/user"
"path/filepath"
"regexp"
"strings"
"github.com/gofrs/flock"
@ -87,9 +88,15 @@ func RealPath(path string) string {
return rpath
}
var (
rs = regexp.MustCompile("[/]+")
rd = regexp.MustCompile("[.]+")
)
// Очистить путь от лишних символов
func ClearPath(path string) string {
return strings.ReplaceAll(strings.ReplaceAll(path, "//", ""), "..", "")
out := rd.ReplaceAllString(rs.ReplaceAllString(path, "/"), ".")
return out
}
// Получить расширение файла