refactor: optimize length handling

This commit is contained in:
SunBK201 2025-11-20 13:49:06 +08:00
parent 5a8bd5e79e
commit 37107ac08d

View File

@ -52,13 +52,14 @@ func (r *Rewriter) buildReplacement(srcAddr, dstAddr string, originalUA string,
}) })
// Adjust to the exact length needed // Adjust to the exact length needed
if len(newUA) >= n { newUALen := len(newUA)
if newUALen >= n {
return []byte(newUA[:n]) return []byte(newUA[:n])
} }
out := make([]byte, n) out := make([]byte, n)
copy(out, newUA) copy(out, newUA)
// Pad with spaces if newUA is shorter than needed // Pad with spaces if newUA is shorter than needed
for i := len(newUA); i < n; i++ { for i := newUALen; i < n; i++ {
out[i] = ' ' out[i] = ' '
} }