Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3(enhancement): remove utils.Trim* because stdlib has same performance in go1.19 #2030

Merged
merged 17 commits into from
Aug 20, 2022
16 changes: 8 additions & 8 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ func (c *DefaultCtx) Accepts(offers ...string) string {
for len(header) > 0 && commaPos != -1 {
commaPos = strings.IndexByte(header, ',')
if commaPos != -1 {
spec = utils.Trim(header[:commaPos], ' ')
spec = strings.TrimLeft(header[:commaPos], " ")
} else {
spec = utils.TrimLeft(header, ' ')
spec = strings.TrimLeft(header, " ")
}
if factorSign := strings.IndexByte(spec, ';'); factorSign != -1 {
spec = spec[:factorSign]
Expand Down Expand Up @@ -508,10 +508,10 @@ func (c *DefaultCtx) IPs() (ips []string) {
for {
commaPos = bytes.IndexByte(header, ',')
if commaPos != -1 {
ips[i] = utils.Trim(c.app.getString(header[:commaPos]), ' ')
ips[i] = strings.Trim(c.app.getString(header[:commaPos]), " ")
header, i = header[commaPos+1:], i+1
} else {
ips[i] = utils.Trim(c.app.getString(header), ' ')
ips[i] = strings.Trim(c.app.getString(header), " ")
return
}
}
Expand All @@ -526,7 +526,7 @@ func (c *DefaultCtx) Is(extension string) bool {
}

return strings.HasPrefix(
utils.TrimLeft(utils.UnsafeString(c.fasthttp.Request.Header.ContentType()), ' '),
strings.TrimLeft(utils.UnsafeString(c.fasthttp.Request.Header.ContentType()), " "),
extensionHeader,
)
}
Expand Down Expand Up @@ -597,7 +597,7 @@ func (c *DefaultCtx) Links(link ...string) {
_, _ = bb.WriteString(`; rel="` + link[i] + `",`)
}
}
c.setCanonical(HeaderLink, utils.TrimRight(c.app.getString(bb.Bytes()), ','))
c.setCanonical(HeaderLink, strings.TrimRight(c.app.getString(bb.Bytes()), ","))
bytebufferpool.Put(bb)
}

Expand Down Expand Up @@ -1245,7 +1245,7 @@ func (c *DefaultCtx) WriteString(s string) (int, error) {
// XHR returns a Boolean property, that is true, if the request's X-Requested-With header field is XMLHttpRequest,
// indicating that the request was issued by a client library (such as jQuery).
func (c *DefaultCtx) XHR() bool {
return utils.EqualFoldBytes(utils.UnsafeBytes(c.Get(HeaderXRequestedWith)), []byte("xmlhttprequest"))
return utils.EqualFold(c.Get(HeaderXRequestedWith), "xmlhttprequest")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we use EqualFoldBytes instead of EqualFold in the first place?

}

// configDependentPaths set paths for route recognition and prepared paths for the user,
Expand All @@ -1267,7 +1267,7 @@ func (c *DefaultCtx) configDependentPaths() {
}
// If StrictRouting is disabled, we strip all trailing slashes
if !c.app.config.StrictRouting && len(c.detectionPathBuffer) > 1 && c.detectionPathBuffer[len(c.detectionPathBuffer)-1] == '/' {
c.detectionPathBuffer = utils.TrimRightBytes(c.detectionPathBuffer, '/')
c.detectionPathBuffer = bytes.TrimRight(c.detectionPathBuffer, "/")
}
c.detectionPath = c.app.getString(c.detectionPathBuffer)

Expand Down
5 changes: 2 additions & 3 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"time"
"unsafe"

"github.com/gofiber/fiber/v3/utils"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp"
)
Expand Down Expand Up @@ -223,7 +222,7 @@ func getGroupPath(prefix, path string) string {
path = "/" + path
}

return utils.TrimRight(prefix, '/') + path
return strings.TrimRight(prefix, "/") + path
}

// return valid offer for header negotiation
Expand All @@ -238,7 +237,7 @@ func getOffer(header string, offers ...string) string {
for len(header) > 0 && commaPos != -1 {
commaPos = strings.IndexByte(header, ',')
if commaPos != -1 {
spec = utils.Trim(header[:commaPos], ' ')
spec = strings.TrimSpace(header[:commaPos])
} else {
spec = header
}
Expand Down
7 changes: 3 additions & 4 deletions middleware/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sync"

"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils"
)

// Config defines the config for middleware.
Expand Down Expand Up @@ -133,7 +132,7 @@ func New(config ...Config) fiber.Handler {
)

if len(path) > 1 {
path = utils.TrimRight(path, '/')
path = strings.TrimRight(path, "/")
}
file, err = cfg.Root.Open(path)
if err != nil && os.IsNotExist(err) && cfg.NotFoundFile != "" {
Expand All @@ -153,7 +152,7 @@ func New(config ...Config) fiber.Handler {

// Serve index if path is directory
if stat.IsDir() {
indexPath := utils.TrimRight(path, '/') + cfg.Index
indexPath := strings.TrimRight(path, "/") + cfg.Index
index, err := cfg.Root.Open(indexPath)
if err == nil {
indexStat, err := index.Stat()
Expand Down Expand Up @@ -226,7 +225,7 @@ func SendFile(c fiber.Ctx, fs http.FileSystem, path string) (err error) {

// Serve index if path is directory
if stat.IsDir() {
indexPath := utils.TrimRight(path, '/') + ConfigDefault.Index
indexPath := strings.TrimRight(path, "/") + ConfigDefault.Index
index, err := fs.Open(indexPath)
if err == nil {
indexStat, err := index.Stat()
Expand Down
3 changes: 1 addition & 2 deletions middleware/filesystem/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils"
)

func getFileExtension(path string) string {
Expand Down Expand Up @@ -41,7 +40,7 @@ func dirList(c fiber.Ctx, f http.File) error {
fmt.Fprint(c, "<ul>")

if len(basePathEscaped) > 1 {
parentPathEscaped := html.EscapeString(utils.TrimRight(c.Path(), '/') + "/..")
parentPathEscaped := html.EscapeString(strings.TrimRight(c.Path(), "/") + "/..")
fmt.Fprintf(c, `<li><a href="%s" class="dir">..</a></li>`, parentPathEscaped)
}

Expand Down
6 changes: 3 additions & 3 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"
"unicode"

"github.com/gofiber/fiber/v3/utils"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -51,6 +50,7 @@ const (
optionalParam byte = '?' // concludes a parameter by name and makes it optional
paramStarterChar byte = ':' // start character for a parameter with name
slashDelimiter byte = '/' // separator for the route, unlike the other delimiters this character at the end can be optional
slashDelimiterStr = "/" // separator for the route, unlike the other delimiters this character at the end can be optional
escapeChar byte = '\\' // escape character
paramConstraintStart byte = '<' // start of type constraint for a parameter
paramConstraintEnd byte = '>' // end of type constraint for a parameter
Expand Down Expand Up @@ -159,7 +159,7 @@ func addParameterMetaInfo(segs []*routeSegment) []*routeSegment {
} else {
comparePart = segs[i].Const
if len(comparePart) > 1 {
comparePart = utils.TrimRight(comparePart, slashDelimiter)
comparePart = strings.TrimRight(comparePart, slashDelimiterStr)
}
}
}
Expand Down Expand Up @@ -354,7 +354,7 @@ func findNextCharsetPositionConstraint(search string, charset []byte) int {
if char == paramConstraintEnd {
constraintEnd = pos
}
//fmt.Println(string(char))
// fmt.Println(string(char))
if pos != -1 && (pos < nextPosition || nextPosition == -1) {
if pos > constraintStart && pos < constraintEnd {
nextPosition = pos
Expand Down
4 changes: 2 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (app *App) addPrefixToRoute(prefix string, route *Route) *Route {
}
// Strict routing, remove trailing slashes
if !app.config.StrictRouting && len(prettyPath) > 1 {
prettyPath = utils.TrimRight(prettyPath, '/')
prettyPath = strings.TrimRight(prettyPath, "/")
}

route.Path = prefixedPath
Expand Down Expand Up @@ -246,7 +246,7 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) Router {
}
// Strict routing, remove trailing slashes
if !app.config.StrictRouting && len(pathPretty) > 1 {
pathPretty = utils.TrimRight(pathPretty, '/')
pathPretty = strings.TrimRight(pathPretty, "/")
}
// Is layer a middleware?
isUse := method == methodUse
Expand Down
Loading