Skip to content

Commit fadedcb

Browse files
authored
Use utils Trim functions instead of the strings/bytes functions (#3087)
* Use utils Trim functions instead of the strings/bytes functions * rename Test and Benchmark functions with same name
1 parent e2cd100 commit fadedcb

File tree

16 files changed

+46
-43
lines changed

16 files changed

+46
-43
lines changed

client/response.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"io/fs"
99
"os"
1010
"path/filepath"
11-
"strings"
1211
"sync"
1312

1413
"github.com/gofiber/utils/v2"
@@ -68,7 +67,7 @@ func (r *Response) Body() []byte {
6867

6968
// String method returns the body of the server response as String.
7069
func (r *Response) String() string {
71-
return strings.TrimSpace(string(r.Body()))
70+
return utils.Trim(string(r.Body()), ' ')
7271
}
7372

7473
// JSON method will unmarshal body to json.

ctx.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ iploop:
778778
i++
779779
}
780780

781-
s := strings.TrimRight(headerValue[i:j], " ")
781+
s := utils.TrimRight(headerValue[i:j], ' ')
782782

783783
if c.app.config.EnableIPValidation {
784784
// Skip validation if IP is clearly not IPv4/IPv6, otherwise validate without allocations
@@ -828,7 +828,7 @@ func (c *DefaultCtx) extractIPFromHeader(header string) string {
828828
i++
829829
}
830830

831-
s := strings.TrimRight(headerValue[i:j], " ")
831+
s := utils.TrimRight(headerValue[i:j], ' ')
832832

833833
if c.app.config.EnableIPValidation {
834834
if (!v6 && !v4) || (v6 && !utils.IsIPv6(s)) || (v4 && !utils.IsIPv4(s)) {
@@ -862,7 +862,7 @@ func (c *DefaultCtx) Is(extension string) bool {
862862
}
863863

864864
return strings.HasPrefix(
865-
strings.TrimLeft(utils.UnsafeString(c.fasthttp.Request.Header.ContentType()), " "),
865+
utils.TrimLeft(utils.UnsafeString(c.fasthttp.Request.Header.ContentType()), ' '),
866866
extensionHeader,
867867
)
868868
}
@@ -939,7 +939,7 @@ func (c *DefaultCtx) Links(link ...string) {
939939
bb.WriteString(`; rel="` + link[i] + `",`)
940940
}
941941
}
942-
c.setCanonical(HeaderLink, strings.TrimRight(c.app.getString(bb.Bytes()), ","))
942+
c.setCanonical(HeaderLink, utils.TrimRight(c.app.getString(bb.Bytes()), ','))
943943
bytebufferpool.Put(bb)
944944
}
945945

@@ -1810,7 +1810,7 @@ func (c *DefaultCtx) configDependentPaths() {
18101810
}
18111811
// If StrictRouting is disabled, we strip all trailing slashes
18121812
if !c.app.config.StrictRouting && len(c.detectionPathBuffer) > 1 && c.detectionPathBuffer[len(c.detectionPathBuffer)-1] == '/' {
1813-
c.detectionPathBuffer = bytes.TrimRight(c.detectionPathBuffer, "/")
1813+
c.detectionPathBuffer = utils.TrimRight(c.detectionPathBuffer, '/')
18141814
}
18151815
c.detectionPath = c.app.getString(c.detectionPathBuffer)
18161816

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/gofiber/fiber/v3
33
go 1.21
44

55
require (
6-
github.com/gofiber/utils/v2 v2.0.0-beta.5
6+
github.com/gofiber/utils/v2 v2.0.0-beta.6
77
github.com/google/uuid v1.6.0
88
github.com/mattn/go-colorable v0.1.13
99
github.com/mattn/go-isatty v0.0.20

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1
22
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5-
github.com/gofiber/utils/v2 v2.0.0-beta.5 h1:zbDIU8gVAlZ2Ak9Fk8APlis4S7wUiQFbcvv6UASkm6A=
6-
github.com/gofiber/utils/v2 v2.0.0-beta.5/go.mod h1:3Kz8Px3jInKFvqxDzDeoSygwEOO+3uyubTmUa6PqY+0=
5+
github.com/gofiber/utils/v2 v2.0.0-beta.6 h1:ED62bOmpRXdgviPlfTmf0Q+AXzhaTUAFtdWjgx+XkYI=
6+
github.com/gofiber/utils/v2 v2.0.0-beta.6/go.mod h1:3Kz8Px3jInKFvqxDzDeoSygwEOO+3uyubTmUa6PqY+0=
77
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
88
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
99
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=

helpers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func getGroupPath(prefix, path string) string {
222222
path = "/" + path
223223
}
224224

225-
return strings.TrimRight(prefix, "/") + path
225+
return utils.TrimRight(prefix, '/') + path
226226
}
227227

228228
// acceptsOffer This function determines if an offer matches a given specification.
@@ -336,7 +336,7 @@ func getSplicedStrList(headerValue string, dst []string) []string {
336336
dst = make([]string, len(dst)+(len(dst)>>1)+2)
337337
copy(dst, oldSlice)
338338
}
339-
dst[insertIndex] = strings.TrimLeft(headerValue[lastElementEndsAt:index], " ")
339+
dst[insertIndex] = utils.TrimLeft(headerValue[lastElementEndsAt:index], ' ')
340340
lastElementEndsAt = uint8(index + 1)
341341
insertIndex++
342342
}
@@ -356,7 +356,7 @@ func forEachMediaRange(header []byte, functor func([]byte)) {
356356

357357
for len(header) > 0 {
358358
n := 0
359-
header = bytes.TrimLeft(header, " ")
359+
header = utils.TrimLeft(header, ' ')
360360
quotes := 0
361361
escaping := false
362362

@@ -459,7 +459,7 @@ func getOffer(header []byte, isAccepted func(spec, offer string, specParams head
459459
}
460460
}
461461

462-
spec = bytes.TrimSpace(spec)
462+
spec = utils.Trim(spec, ' ')
463463

464464
// Determine specificity
465465
var specificity int

middleware/cache/manager_msgp_test.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

middleware/cors/cors.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/gofiber/fiber/v3"
88
"github.com/gofiber/fiber/v3/log"
9+
"github.com/gofiber/utils/v2"
910
)
1011

1112
// New creates a new middleware handler
@@ -44,15 +45,15 @@ func New(config ...Config) fiber.Handler {
4445
break
4546
}
4647
if i := strings.Index(origin, "://*."); i != -1 {
47-
trimmedOrigin := strings.TrimSpace(origin[:i+3] + origin[i+4:])
48+
trimmedOrigin := utils.Trim(origin[:i+3]+origin[i+4:], ' ')
4849
isValid, normalizedOrigin := normalizeOrigin(trimmedOrigin)
4950
if !isValid {
5051
panic("[CORS] Invalid origin format in configuration: " + trimmedOrigin)
5152
}
5253
sd := subdomain{prefix: normalizedOrigin[:i+3], suffix: normalizedOrigin[i+3:]}
5354
allowSOrigins = append(allowSOrigins, sd)
5455
} else {
55-
trimmedOrigin := strings.TrimSpace(origin)
56+
trimmedOrigin := utils.Trim(origin, ' ')
5657
isValid, normalizedOrigin := normalizeOrigin(trimmedOrigin)
5758
if !isValid {
5859
panic("[CORS] Invalid origin format in configuration: " + trimmedOrigin)

middleware/csrf/csrf.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/gofiber/fiber/v3"
11+
"github.com/gofiber/utils/v2"
1112
)
1213

1314
var (
@@ -62,15 +63,15 @@ func New(config ...Config) fiber.Handler {
6263

6364
for _, origin := range cfg.TrustedOrigins {
6465
if i := strings.Index(origin, "://*."); i != -1 {
65-
trimmedOrigin := strings.TrimSpace(origin[:i+3] + origin[i+4:])
66+
trimmedOrigin := utils.Trim(origin[:i+3]+origin[i+4:], ' ')
6667
isValid, normalizedOrigin := normalizeOrigin(trimmedOrigin)
6768
if !isValid {
6869
panic("[CSRF] Invalid origin format in configuration:" + origin)
6970
}
7071
sd := subdomain{prefix: normalizedOrigin[:i+3], suffix: normalizedOrigin[i+3:]}
7172
trustedSubOrigins = append(trustedSubOrigins, sd)
7273
} else {
73-
trimmedOrigin := strings.TrimSpace(origin)
74+
trimmedOrigin := utils.Trim(origin, ' ')
7475
isValid, normalizedOrigin := normalizeOrigin(trimmedOrigin)
7576
if !isValid {
7677
panic("[CSRF] Invalid origin format in configuration:" + origin)

middleware/csrf/csrf_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func Test_CSRF_WithSession(t *testing.T) {
139139
h(ctx)
140140
token := string(ctx.Response.Header.Peek(fiber.HeaderSetCookie))
141141
for _, header := range strings.Split(token, ";") {
142-
if strings.Split(strings.TrimSpace(header), "=")[0] == ConfigDefault.CookieName {
142+
if strings.Split(utils.Trim(header, ' '), "=")[0] == ConfigDefault.CookieName {
143143
token = strings.Split(header, "=")[1]
144144
break
145145
}
@@ -248,7 +248,7 @@ func Test_CSRF_ExpiredToken_WithSession(t *testing.T) {
248248
h(ctx)
249249
token := string(ctx.Response.Header.Peek(fiber.HeaderSetCookie))
250250
for _, header := range strings.Split(token, ";") {
251-
if strings.Split(strings.TrimSpace(header), "=")[0] == ConfigDefault.CookieName {
251+
if strings.Split(utils.Trim(header, ' '), "=")[0] == ConfigDefault.CookieName {
252252
token = strings.Split(header, "=")[1]
253253
break
254254
}

middleware/csrf/manager_msgp_test.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)