From a098c100d7bad3fd95e6c46b35cff64ac7b198d9 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 28 Jul 2024 18:01:27 +0800 Subject: [PATCH] code refactor --- pkg/utils/io.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/pkg/utils/io.go b/pkg/utils/io.go index 01fdda56..44ee90ed 100644 --- a/pkg/utils/io.go +++ b/pkg/utils/io.go @@ -91,14 +91,28 @@ func WriteFile(path string, data []byte) error { // GetFileNameWithoutExtension returns the file name without extension func GetFileNameWithoutExtension(path string) string { - fileName := filepath.Base(path) - extension := filepath.Ext(fileName) - - if len(extension) < 1 { - return fileName + if path == "" { + return "" } - return fileName[0 : len(fileName)-len(extension)] + for i := len(path) - 1; i >= 0; i-- { + if path[i] == '/' || path[i] == '\\' { + path = path[i+1:] + break + } + } + + if path == "" { + return "" + } + + extension := filepath.Ext(path) + + if len(extension) < 1 { + return path + } + + return path[0 : len(path)-len(extension)] } // GetFileNameExtension returns the file extension without dot