From 42d921d353c0255197245d12a55d9280d0157c10 Mon Sep 17 00:00:00 2001 From: Juan Calderon-Perez <835733+gaby@users.noreply.github.com> Date: Mon, 27 Jan 2025 09:00:51 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20chore:=20Backport=20ctx.String()?= =?UTF-8?q?=20from=20v3=20(#3294)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Backport ctx.String() to v2 * Fix lint issues --- ctx.go | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/ctx.go b/ctx.go index c0a4413ab0..b418dc8e90 100644 --- a/ctx.go +++ b/ctx.go @@ -1834,14 +1834,39 @@ func (c *Ctx) Status(status int) *Ctx { // // The returned value may be useful for logging. func (c *Ctx) String() string { - return fmt.Sprintf( - "#%016X - %s <-> %s - %s %s", - c.fasthttp.ID(), - c.fasthttp.LocalAddr(), - c.fasthttp.RemoteAddr(), - c.fasthttp.Request.Header.Method(), - c.fasthttp.URI().FullURI(), - ) + // Get buffer from pool + buf := bytebufferpool.Get() + + // Start with the ID, converting it to a hex string without fmt.Sprintf + buf.WriteByte('#') //nolint:errcheck // Not needed here + // Convert ID to hexadecimal + id := strconv.FormatUint(c.fasthttp.ID(), 16) + // Pad with leading zeros to ensure 16 characters + for i := 0; i < (16 - len(id)); i++ { + buf.WriteByte('0') //nolint:errcheck // Not needed here + } + buf.WriteString(id) //nolint:errcheck // Not needed here + buf.WriteString(" - ") //nolint:errcheck // Not needed here + + // Add local and remote addresses directly + buf.WriteString(c.fasthttp.LocalAddr().String()) //nolint:errcheck // Not needed here + buf.WriteString(" <-> ") //nolint:errcheck // Not needed here + buf.WriteString(c.fasthttp.RemoteAddr().String()) //nolint:errcheck // Not needed here + buf.WriteString(" - ") //nolint:errcheck // Not needed here + + // Add method and URI + buf.Write(c.fasthttp.Request.Header.Method()) //nolint:errcheck // Not needed here + buf.WriteByte(' ') //nolint:errcheck // Not needed here + buf.Write(c.fasthttp.URI().FullURI()) //nolint:errcheck // Not needed here + + // Allocate string + str := buf.String() + + // Reset buffer + buf.Reset() + bytebufferpool.Put(buf) + + return str } // Type sets the Content-Type HTTP header to the MIME type specified by the file extension.