Skip to content

Commit

Permalink
Bugfix: large negative exponents not always give minification
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed May 26, 2015
1 parent 2964f6e commit d9bc3b7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 8 additions & 5 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,31 @@ func Number(num []byte) []byte {
}

// append the exponent or change the mantissa to incorporate the exponent
relExp := exp + int64(end-start)
relExp := exp + int64(end-start) // exp when the first non-zero digit is directly after the dot
n := 0 // number of exp digits
if exp != 0 {
n = int(math.Log10(math.Abs(float64(exp)))) + 1
}
if exp == 0 {
if neg {
start--
num[start] = '-'
}
return num[start:end]
} else if relExp < -2 || 2 < exp {
} else if int(relExp)+n+1 < 0 || 2 < exp { // add exponent for exp 3 and higher and where a lower exp really makes it shorter
num[end] = 'e'
end++
if exp < 0 {
num[end] = '-'
end++
exp = -exp
}
n := int(math.Log10(float64(exp))) + 1
for i := end + n - 1; i >= end; i-- {
num[i] = byte(exp%10) + '0'
exp /= 10
}
end += n
} else if exp < 0 {
} else if exp < 0 { // omit exponent
if relExp > 0 {
copy(num[start+int(relExp)+1:], num[start+int(relExp):end])
num[start+int(relExp)] = '.'
Expand All @@ -189,7 +192,7 @@ func Number(num []byte) []byte {
}
end -= int(relExp) - 1
}
} else {
} else { // for exponent 1 and 2
num[end] = '0'
if exp == 2 {
num[end+1] = '0'
Expand Down
3 changes: 3 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,8 @@ func TestNumber(t *testing.T) {
assertNumber(t, "-1.252", "-1.252")
assertNumber(t, "0.075", ".075")
assertNumber(t, "789012345678901234567890123456789e9234567890123456789", "789012345678901234567890123456789e9234567890123456789")
assertNumber(t, ".000100009", "100009e-9")
assertNumber(t, ".0001000009", ".0001000009")
assertNumber(t, ".0001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009", ".0001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009")
//assertNumber(t, "96px", "1in")
}

0 comments on commit d9bc3b7

Please sign in to comment.