Replace bytes.Buffer with strings.Builder where appropriate (#3347)

To build strings more efficiently, use strings.Builder instead.
This commit is contained in:
hopehook 2023-01-20 09:51:42 +08:00 committed by GitHub
parent 8cd11c82e4
commit b2d4185eec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 21 deletions

View File

@ -5,7 +5,6 @@
package gin package gin
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
@ -13,6 +12,7 @@ import (
"log" "log"
"os" "os"
"runtime" "runtime"
"strings"
"sync" "sync"
"testing" "testing"
@ -138,7 +138,7 @@ func captureOutput(t *testing.T, f func()) string {
wg := new(sync.WaitGroup) wg := new(sync.WaitGroup)
wg.Add(1) wg.Add(1)
go func() { go func() {
var buf bytes.Buffer var buf strings.Builder
wg.Done() wg.Done()
_, err := io.Copy(&buf, reader) _, err := io.Copy(&buf, reader)
assert.NoError(t, err) assert.NoError(t, err)

View File

@ -5,12 +5,12 @@
package gin package gin
import ( import (
"bytes"
"fmt" "fmt"
"math/rand" "math/rand"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -401,7 +401,7 @@ func TestGithubAPI(t *testing.T) {
} }
func exampleFromPath(path string) (string, Params) { func exampleFromPath(path string) (string, Params) {
output := new(bytes.Buffer) output := new(strings.Builder)
params := make(Params, 0, 6) params := make(Params, 0, 6)
start := -1 start := -1
for i, c := range path { for i, c := range path {

View File

@ -5,10 +5,10 @@
package gin package gin
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"strings"
"testing" "testing"
"time" "time"
@ -20,7 +20,7 @@ func init() {
} }
func TestLogger(t *testing.T) { func TestLogger(t *testing.T) {
buffer := new(bytes.Buffer) buffer := new(strings.Builder)
router := New() router := New()
router.Use(LoggerWithWriter(buffer)) router.Use(LoggerWithWriter(buffer))
router.GET("/example", func(c *Context) {}) router.GET("/example", func(c *Context) {})
@ -84,7 +84,7 @@ func TestLogger(t *testing.T) {
} }
func TestLoggerWithConfig(t *testing.T) { func TestLoggerWithConfig(t *testing.T) {
buffer := new(bytes.Buffer) buffer := new(strings.Builder)
router := New() router := New()
router.Use(LoggerWithConfig(LoggerConfig{Output: buffer})) router.Use(LoggerWithConfig(LoggerConfig{Output: buffer}))
router.GET("/example", func(c *Context) {}) router.GET("/example", func(c *Context) {})
@ -148,7 +148,7 @@ func TestLoggerWithConfig(t *testing.T) {
} }
func TestLoggerWithFormatter(t *testing.T) { func TestLoggerWithFormatter(t *testing.T) {
buffer := new(bytes.Buffer) buffer := new(strings.Builder)
d := DefaultWriter d := DefaultWriter
DefaultWriter = buffer DefaultWriter = buffer
@ -182,7 +182,7 @@ func TestLoggerWithFormatter(t *testing.T) {
func TestLoggerWithConfigFormatting(t *testing.T) { func TestLoggerWithConfigFormatting(t *testing.T) {
var gotParam LogFormatterParams var gotParam LogFormatterParams
var gotKeys map[string]any var gotKeys map[string]any
buffer := new(bytes.Buffer) buffer := new(strings.Builder)
router := New() router := New()
router.engine.trustedCIDRs, _ = router.engine.prepareTrustedCIDRs() router.engine.trustedCIDRs, _ = router.engine.prepareTrustedCIDRs()
@ -382,7 +382,7 @@ func TestErrorLogger(t *testing.T) {
} }
func TestLoggerWithWriterSkippingPaths(t *testing.T) { func TestLoggerWithWriterSkippingPaths(t *testing.T) {
buffer := new(bytes.Buffer) buffer := new(strings.Builder)
router := New() router := New()
router.Use(LoggerWithWriter(buffer, "/skipped")) router.Use(LoggerWithWriter(buffer, "/skipped"))
router.GET("/logged", func(c *Context) {}) router.GET("/logged", func(c *Context) {})
@ -397,7 +397,7 @@ func TestLoggerWithWriterSkippingPaths(t *testing.T) {
} }
func TestLoggerWithConfigSkippingPaths(t *testing.T) { func TestLoggerWithConfigSkippingPaths(t *testing.T) {
buffer := new(bytes.Buffer) buffer := new(strings.Builder)
router := New() router := New()
router.Use(LoggerWithConfig(LoggerConfig{ router.Use(LoggerWithConfig(LoggerConfig{
Output: buffer, Output: buffer,

View File

@ -5,7 +5,6 @@
package gin package gin
import ( import (
"bytes"
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
@ -18,7 +17,7 @@ import (
) )
func TestPanicClean(t *testing.T) { func TestPanicClean(t *testing.T) {
buffer := new(bytes.Buffer) buffer := new(strings.Builder)
router := New() router := New()
password := "my-super-secret-password" password := "my-super-secret-password"
router.Use(RecoveryWithWriter(buffer)) router.Use(RecoveryWithWriter(buffer))
@ -50,7 +49,7 @@ func TestPanicClean(t *testing.T) {
// TestPanicInHandler assert that panic has been recovered. // TestPanicInHandler assert that panic has been recovered.
func TestPanicInHandler(t *testing.T) { func TestPanicInHandler(t *testing.T) {
buffer := new(bytes.Buffer) buffer := new(strings.Builder)
router := New() router := New()
router.Use(RecoveryWithWriter(buffer)) router.Use(RecoveryWithWriter(buffer))
router.GET("/recovery", func(_ *Context) { router.GET("/recovery", func(_ *Context) {
@ -122,7 +121,7 @@ func TestPanicWithBrokenPipe(t *testing.T) {
for errno, expectMsg := range expectMsgs { for errno, expectMsg := range expectMsgs {
t.Run(expectMsg, func(t *testing.T) { t.Run(expectMsg, func(t *testing.T) {
var buf bytes.Buffer var buf strings.Builder
router := New() router := New()
router.Use(RecoveryWithWriter(&buf)) router.Use(RecoveryWithWriter(&buf))
@ -145,8 +144,8 @@ func TestPanicWithBrokenPipe(t *testing.T) {
} }
func TestCustomRecoveryWithWriter(t *testing.T) { func TestCustomRecoveryWithWriter(t *testing.T) {
errBuffer := new(bytes.Buffer) errBuffer := new(strings.Builder)
buffer := new(bytes.Buffer) buffer := new(strings.Builder)
router := New() router := New()
handleRecovery := func(c *Context, err any) { handleRecovery := func(c *Context, err any) {
errBuffer.WriteString(err.(string)) errBuffer.WriteString(err.(string))
@ -179,8 +178,8 @@ func TestCustomRecoveryWithWriter(t *testing.T) {
} }
func TestCustomRecovery(t *testing.T) { func TestCustomRecovery(t *testing.T) {
errBuffer := new(bytes.Buffer) errBuffer := new(strings.Builder)
buffer := new(bytes.Buffer) buffer := new(strings.Builder)
router := New() router := New()
DefaultErrorWriter = buffer DefaultErrorWriter = buffer
handleRecovery := func(c *Context, err any) { handleRecovery := func(c *Context, err any) {
@ -214,8 +213,8 @@ func TestCustomRecovery(t *testing.T) {
} }
func TestRecoveryWithWriterWithCustomRecovery(t *testing.T) { func TestRecoveryWithWriterWithCustomRecovery(t *testing.T) {
errBuffer := new(bytes.Buffer) errBuffer := new(strings.Builder)
buffer := new(bytes.Buffer) buffer := new(strings.Builder)
router := New() router := New()
DefaultErrorWriter = buffer DefaultErrorWriter = buffer
handleRecovery := func(c *Context, err any) { handleRecovery := func(c *Context, err any) {