archive/tar: optimize formatter's formatString

When dealing with the last special case,
we only need to know the string length after TrimRight '/'.
This optimization clarifies what we really want and speeds up the execution.
This commit is contained in:
Jes Cok 2023-06-11 21:42:07 +08:00
parent 1fd3cc7cd0
commit 34a6028663
1 changed files with 7 additions and 2 deletions

View File

@ -73,8 +73,13 @@ func (f *formatter) formatString(b []byte, s string) {
// in the V7 path field as a directory even though the full path
// recorded elsewhere (e.g., via PAX record) contains no trailing slash.
if len(s) > len(b) && b[len(b)-1] == '/' {
n := len(strings.TrimRight(s[:len(b)], "/"))
b[n] = 0 // Replace trailing slash with NUL terminator
i := len(b) - 2 // s[len(b)-1] == '/'
for ; i >= 0; i-- {
if s[i] != '/' {
break
}
}
b[i+1] = 0 // Replace trailing slash with NUL terminator
}
}