Skip to content

Commit e8622bf

Browse files
committed
refactor function joinPaths and isAscii
1 parent 688a429 commit e8622bf

File tree

2 files changed

+4
-20
lines changed

2 files changed

+4
-20
lines changed

utils.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,6 @@ func parseAccept(acceptHeader string) []string {
114114
return out
115115
}
116116

117-
func lastChar(str string) uint8 {
118-
if str == "" {
119-
panic("The length of the string can't be 0")
120-
}
121-
return str[len(str)-1]
122-
}
123-
124117
func nameOfFunction(f any) string {
125118
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
126119
}
@@ -131,7 +124,7 @@ func joinPaths(absolutePath, relativePath string) string {
131124
}
132125

133126
finalPath := path.Join(absolutePath, relativePath)
134-
if lastChar(relativePath) == '/' && lastChar(finalPath) != '/' {
127+
if strings.HasSuffix(relativePath, "/") && !strings.HasSuffix(finalPath, "/") {
135128
return finalPath + "/"
136129
}
137130
return finalPath
@@ -155,10 +148,7 @@ func resolveAddress(addr []string) string {
155148

156149
// https://stackoverflow.com/questions/53069040/checking-a-string-contains-only-ascii-characters
157150
func isASCII(s string) bool {
158-
for i := 0; i < len(s); i++ {
159-
if s[i] > unicode.MaxASCII {
160-
return false
161-
}
162-
}
163-
return true
151+
return !strings.ContainsFunc(s, func(r rune) bool {
152+
return r > unicode.MaxASCII
153+
})
164154
}

utils_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ func TestWrap(t *testing.T) {
5454
assert.Equal(t, "hola!", w.Body.String())
5555
}
5656

57-
func TestLastChar(t *testing.T) {
58-
assert.Equal(t, uint8('a'), lastChar("hola"))
59-
assert.Equal(t, uint8('s'), lastChar("adios"))
60-
assert.Panics(t, func() { lastChar("") })
61-
}
62-
6357
func TestParseAccept(t *testing.T) {
6458
parts := parseAccept("text/html , application/xhtml+xml,application/xml;q=0.9, */* ;q=0.8")
6559
assert.Len(t, parts, 4)

0 commit comments

Comments
 (0)