Skip to content

Commit a4b8d54

Browse files
committed
Update Number
1 parent a529eb7 commit a4b8d54

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

common.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ func Number(num []byte, prec int) []byte {
328328
// normExp would be the exponent if it were normalised (0.1 <= f < 1)
329329
n := 0
330330
normExp := 0
331-
if dot == start {
331+
if start == end {
332+
return num // no number before exponent
333+
} else if dot == start {
332334
for i = dot + 1; i < end; i++ {
333335
if num[i] != '0' {
334336
n = end - i
@@ -404,24 +406,24 @@ func Number(num []byte, prec int) []byte {
404406
} else if zeroes < 0 {
405407
copy(num[start+1:], num[start:dot])
406408
num[start] = '.'
409+
} else {
410+
return num
407411
}
408412
num[end] = 'e'
409413
num[end+1] = '-'
410414
end += 2
411-
for i := end + lenNormExp - 1; end <= i; i-- {
415+
for i := end + lenNormExp - 2; end <= i; i-- {
412416
num[i] = -byte(normExp%10) + '0'
413417
normExp /= 10
414418
}
415-
end += lenNormExp
416-
} else if -lenIntExp-1 <= normExp {
419+
end += lenNormExp - 1
420+
} else if -lenIntExp <= normExp {
417421
// case 3: print number without exponent
418422
zeroes := -normExp
419423
if 0 < zeroes {
420-
// dot placed at the front and negative exponent, adding zeroes
421-
newDot := end - n - zeroes - 1
422-
if newDot != dot {
423-
d := start - newDot
424-
if 0 < d {
424+
// place dot at the front, adding zeroes after the dot
425+
if newDot := end - n - zeroes - 1; newDot != dot {
426+
if d := start - newDot; 0 < d {
425427
if dot < end {
426428
// copy original digits after the dot towards the end
427429
copy(num[dot+1+d:], num[dot+1:end])
@@ -444,18 +446,18 @@ func Number(num []byte, prec int) []byte {
444446
}
445447
}
446448
} else {
447-
// dot placed in the middle of the number
448-
if dot == start {
449-
// when there are zeroes after the dot
450-
dot = end - n - 1
451-
start = dot
452-
} else if end <= dot {
449+
// place dot in the middle of the number
450+
if end <= dot {
453451
// when input has no dot in it
454452
dot = end
455453
end++
454+
} else if dot == start {
455+
// when there are zeroes after the dot
456+
dot = end - n - 1
457+
start = dot
456458
}
457-
newDot := start + normExp
458459
// move digits between dot and newDot towards the end
460+
newDot := start + normExp
459461
if dot < newDot {
460462
copy(num[dot:], num[dot+1:newDot+1])
461463
} else if newDot < dot {
@@ -468,11 +470,11 @@ func Number(num []byte, prec int) []byte {
468470
// find new end, considering moving numbers to the front, removing the dot and increasing the length of the exponent
469471
newEnd := end
470472
if dot == start {
471-
newEnd = start + n
473+
newEnd = dot + n
472474
} else {
473475
newEnd--
474476
}
475-
newEnd += 2 + lenIntExp
477+
newEnd += 1 + lenIntExp
476478

477479
exp := intExp
478480
lenExp := lenIntExp
@@ -490,19 +492,16 @@ func Number(num []byte, prec int) []byte {
490492
} else {
491493
// it does not save space and will panic, so we revert to the original representation
492494
exp = origExp
493-
lenExp = 1
494-
if origExp <= -10 || 10 <= origExp {
495-
lenExp = strconv.LenInt(int64(origExp))
496-
}
495+
lenExp = strconv.LenInt(int64(origExp))
497496
}
498497
num[end] = 'e'
499498
num[end+1] = '-'
500499
end += 2
501-
for i := end + lenExp - 1; end <= i; i-- {
500+
for i := end + lenExp - 2; end <= i; i-- {
502501
num[i] = -byte(exp%10) + '0'
503502
exp /= 10
504503
}
505-
end += lenExp
504+
end += lenExp - 1
506505
}
507506

508507
if neg {

common_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ func TestNumber(t *testing.T) {
189189
{"11e9223372036854775807", "11e9223372036854775807"},
190190
{".01e-9223372036854775808", ".01e-9223372036854775808"},
191191
{".011e-9223372036854775808", ".011e-9223372036854775808"},
192+
{"1e-1", ".1"},
193+
{"e-1", "e-1"},
192194

193195
{".12345e8", "12345e3"},
194196
{".12345e7", "1234500"},
@@ -298,7 +300,7 @@ func TestNumberTruncate(t *testing.T) {
298300
{".3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333397e-902", 0, "3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333397e-999"},
299301
{".3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333397e-903", 0, ".3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333397e-903"},
300302
{"29.629775e-9", 0, ".29629775e-7"},
301-
{"e-9223372036854775808", 0, "e-9223372036854775808"},
303+
{"1e-9223372036854775808", 0, "1e-9223372036854775808"},
302304
{"139.99999999", 8, "140"},
303305
}
304306
for _, tt := range numberTests {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/fsnotify/fsnotify v1.8.0
1010
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2
1111
github.com/tdewolff/argp v0.0.0-20250209172303-079abae893fb
12-
github.com/tdewolff/parse/v2 v2.8.0
12+
github.com/tdewolff/parse/v2 v2.8.1
1313
github.com/tdewolff/test v1.0.11
1414
)
1515

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v
2525
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
2626
github.com/tdewolff/argp v0.0.0-20250209172303-079abae893fb h1:Id2aj2q74MC+yHBGQNeTVuQp/gqJd3vZjMmdoVPPfVc=
2727
github.com/tdewolff/argp v0.0.0-20250209172303-079abae893fb/go.mod h1:PKhwRVvnrI2gye5NRF3c4VWbE+3E9mGyRUsNWGcJlDY=
28-
github.com/tdewolff/parse/v2 v2.8.0 h1:jW0afj6zpUGXuZTwJ7/UfP2SddyLalb/SDryjaMTkA4=
29-
github.com/tdewolff/parse/v2 v2.8.0/go.mod h1:Hwlni2tiVNKyzR1o6nUs4FOF07URA+JLBLd6dlIXYqo=
28+
github.com/tdewolff/parse/v2 v2.8.1 h1:J5GSHru6o3jF1uLlEKVXkDxxcVx6yzOlIVIotK4w2po=
29+
github.com/tdewolff/parse/v2 v2.8.1/go.mod h1:Hwlni2tiVNKyzR1o6nUs4FOF07URA+JLBLd6dlIXYqo=
3030
github.com/tdewolff/test v1.0.6/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
3131
github.com/tdewolff/test v1.0.11 h1:FdLbwQVHxqG16SlkGveC0JVyrJN62COWTRyUFzfbtBE=
3232
github.com/tdewolff/test v1.0.11/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8=

0 commit comments

Comments
 (0)