refactor: replace magic number 128 with unicode.MaxASCII in AsciiJSON Render (#4224)

Co-authored-by: huangzw <huangzw@2345.com>
This commit is contained in:
Name 2025-04-21 22:05:28 +08:00 committed by GitHub
parent bb82473103
commit 67c9d4ee11
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,6 +9,7 @@ import (
"fmt"
"html/template"
"net/http"
"unicode"
"github.com/gin-gonic/gin/internal/bytesconv"
"github.com/gin-gonic/gin/internal/json"
@ -162,7 +163,7 @@ func (r AsciiJSON) Render(w http.ResponseWriter) error {
escapeBuf := make([]byte, 0, 6) // Preallocate 6 bytes for Unicode escape sequences
for _, r := range bytesconv.BytesToString(ret) {
if r >= 128 {
if r > unicode.MaxASCII {
escapeBuf = fmt.Appendf(escapeBuf[:0], "\\u%04x", r) // Reuse escapeBuf
buffer.Write(escapeBuf)
} else {