Skip to content

Commit 98a382b

Browse files
author
1911860538
committed
archive/tar: optimize nanosecond parsing in parsePAXTime
1 parent cfe9950 commit 98a382b

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

src/archive/tar/strconv.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,21 +213,15 @@ func parsePAXTime(s string) (time.Time, error) {
213213
}
214214

215215
// Parse the nanoseconds.
216-
nanoDigits := [maxNanoSecondDigits]byte{}
217-
i := 0
218-
for _, c := range sn {
219-
if c < '0' || c > '9' {
216+
// Initialize an array with '0's to handle right padding automatically
217+
nanoDigits := [maxNanoSecondDigits]byte{'0', '0', '0', '0', '0', '0', '0', '0', '0'}
218+
for i := range sn {
219+
switch c := sn[i]; {
220+
case c < '0' || c > '9':
220221
return time.Time{}, ErrHeader
222+
case i < len(nanoDigits):
223+
nanoDigits[i] = c
221224
}
222-
// Right truncate
223-
if i < maxNanoSecondDigits {
224-
nanoDigits[i] = byte(c)
225-
i++
226-
}
227-
}
228-
// Right pad
229-
for ; i < maxNanoSecondDigits; i++ {
230-
nanoDigits[i] = '0'
231225
}
232226
nsecs, _ := strconv.ParseInt(string(nanoDigits[:]), 10, 64) // Must succeed after validation
233227
if len(ss) > 0 && ss[0] == '-' {

src/archive/tar/strconv_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ func BenchmarkParsePAXTimeError(b *testing.B) {
488488
}
489489
}
490490

491-
func Benchmark_ParsePAXTimeError1(b *testing.B) {
491+
func BenchmarkParsePAXTimeError1(b *testing.B) {
492492
sample := "1.a123abc"
493493

494494
b.ReportAllocs()

0 commit comments

Comments
 (0)