refactor function joinPaths and isAscii

This commit is contained in:
keitosuwahara 2025-07-07 19:09:46 +09:00
parent 688a429d19
commit e8622bf445
2 changed files with 4 additions and 20 deletions

View File

@ -114,13 +114,6 @@ func parseAccept(acceptHeader string) []string {
return out
}
func lastChar(str string) uint8 {
if str == "" {
panic("The length of the string can't be 0")
}
return str[len(str)-1]
}
func nameOfFunction(f any) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
@ -131,7 +124,7 @@ func joinPaths(absolutePath, relativePath string) string {
}
finalPath := path.Join(absolutePath, relativePath)
if lastChar(relativePath) == '/' && lastChar(finalPath) != '/' {
if strings.HasSuffix(relativePath, "/") && !strings.HasSuffix(finalPath, "/") {
return finalPath + "/"
}
return finalPath
@ -155,10 +148,7 @@ func resolveAddress(addr []string) string {
// https://stackoverflow.com/questions/53069040/checking-a-string-contains-only-ascii-characters
func isASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > unicode.MaxASCII {
return false
}
}
return true
return !strings.ContainsFunc(s, func(r rune) bool {
return r > unicode.MaxASCII
})
}

View File

@ -54,12 +54,6 @@ func TestWrap(t *testing.T) {
assert.Equal(t, "hola!", w.Body.String())
}
func TestLastChar(t *testing.T) {
assert.Equal(t, uint8('a'), lastChar("hola"))
assert.Equal(t, uint8('s'), lastChar("adios"))
assert.Panics(t, func() { lastChar("") })
}
func TestParseAccept(t *testing.T) {
parts := parseAccept("text/html , application/xhtml+xml,application/xml;q=0.9, */* ;q=0.8")
assert.Len(t, parts, 4)