@@ -328,7 +328,9 @@ func Number(num []byte, prec int) []byte {
328
328
// normExp would be the exponent if it were normalised (0.1 <= f < 1)
329
329
n := 0
330
330
normExp := 0
331
- if dot == start {
331
+ if start == end {
332
+ return num // no number before exponent
333
+ } else if dot == start {
332
334
for i = dot + 1 ; i < end ; i ++ {
333
335
if num [i ] != '0' {
334
336
n = end - i
@@ -404,24 +406,24 @@ func Number(num []byte, prec int) []byte {
404
406
} else if zeroes < 0 {
405
407
copy (num [start + 1 :], num [start :dot ])
406
408
num [start ] = '.'
409
+ } else {
410
+ return num
407
411
}
408
412
num [end ] = 'e'
409
413
num [end + 1 ] = '-'
410
414
end += 2
411
- for i := end + lenNormExp - 1 ; end <= i ; i -- {
415
+ for i := end + lenNormExp - 2 ; end <= i ; i -- {
412
416
num [i ] = - byte (normExp % 10 ) + '0'
413
417
normExp /= 10
414
418
}
415
- end += lenNormExp
416
- } else if - lenIntExp - 1 <= normExp {
419
+ end += lenNormExp - 1
420
+ } else if - lenIntExp <= normExp {
417
421
// case 3: print number without exponent
418
422
zeroes := - normExp
419
423
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 {
425
427
if dot < end {
426
428
// copy original digits after the dot towards the end
427
429
copy (num [dot + 1 + d :], num [dot + 1 :end ])
@@ -444,18 +446,18 @@ func Number(num []byte, prec int) []byte {
444
446
}
445
447
}
446
448
} 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 {
453
451
// when input has no dot in it
454
452
dot = end
455
453
end ++
454
+ } else if dot == start {
455
+ // when there are zeroes after the dot
456
+ dot = end - n - 1
457
+ start = dot
456
458
}
457
- newDot := start + normExp
458
459
// move digits between dot and newDot towards the end
460
+ newDot := start + normExp
459
461
if dot < newDot {
460
462
copy (num [dot :], num [dot + 1 :newDot + 1 ])
461
463
} else if newDot < dot {
@@ -468,11 +470,11 @@ func Number(num []byte, prec int) []byte {
468
470
// find new end, considering moving numbers to the front, removing the dot and increasing the length of the exponent
469
471
newEnd := end
470
472
if dot == start {
471
- newEnd = start + n
473
+ newEnd = dot + n
472
474
} else {
473
475
newEnd --
474
476
}
475
- newEnd += 2 + lenIntExp
477
+ newEnd += 1 + lenIntExp
476
478
477
479
exp := intExp
478
480
lenExp := lenIntExp
@@ -490,19 +492,16 @@ func Number(num []byte, prec int) []byte {
490
492
} else {
491
493
// it does not save space and will panic, so we revert to the original representation
492
494
exp = origExp
493
- lenExp = 1
494
- if origExp <= - 10 || 10 <= origExp {
495
- lenExp = strconv .LenInt (int64 (origExp ))
496
- }
495
+ lenExp = strconv .LenInt (int64 (origExp ))
497
496
}
498
497
num [end ] = 'e'
499
498
num [end + 1 ] = '-'
500
499
end += 2
501
- for i := end + lenExp - 1 ; end <= i ; i -- {
500
+ for i := end + lenExp - 2 ; end <= i ; i -- {
502
501
num [i ] = - byte (exp % 10 ) + '0'
503
502
exp /= 10
504
503
}
505
- end += lenExp
504
+ end += lenExp - 1
506
505
}
507
506
508
507
if neg {
0 commit comments