Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,9 @@ func Number(num []byte, prec int) []byte {
// normExp would be the exponent if it were normalised (0.1 <= f < 1)
n := 0
normExp := 0
if dot == start {
if start == end {
return num // no number before exponent
} else if dot == start {
for i = dot + 1; i < end; i++ {
if num[i] != '0' {
n = end - i
Expand Down Expand Up @@ -404,24 +406,24 @@ func Number(num []byte, prec int) []byte {
} else if zeroes < 0 {
copy(num[start+1:], num[start:dot])
num[start] = '.'
} else {
return num
}
num[end] = 'e'
num[end+1] = '-'
end += 2
for i := end + lenNormExp - 1; end <= i; i-- {
for i := end + lenNormExp - 2; end <= i; i-- {
num[i] = -byte(normExp%10) + '0'
normExp /= 10
}
end += lenNormExp
} else if -lenIntExp-1 <= normExp {
end += lenNormExp - 1
} else if -lenIntExp <= normExp {
// case 3: print number without exponent
zeroes := -normExp
if 0 < zeroes {
// dot placed at the front and negative exponent, adding zeroes
newDot := end - n - zeroes - 1
if newDot != dot {
d := start - newDot
if 0 < d {
// place dot at the front, adding zeroes after the dot
if newDot := end - n - zeroes - 1; newDot != dot {
if d := start - newDot; 0 < d {
if dot < end {
// copy original digits after the dot towards the end
copy(num[dot+1+d:], num[dot+1:end])
Expand All @@ -444,18 +446,18 @@ func Number(num []byte, prec int) []byte {
}
}
} else {
// dot placed in the middle of the number
if dot == start {
// when there are zeroes after the dot
dot = end - n - 1
start = dot
} else if end <= dot {
// place dot in the middle of the number
if end <= dot {
// when input has no dot in it
dot = end
end++
} else if dot == start {
// when there are zeroes after the dot
dot = end - n - 1
start = dot
}
newDot := start + normExp
// move digits between dot and newDot towards the end
newDot := start + normExp
if dot < newDot {
copy(num[dot:], num[dot+1:newDot+1])
} else if newDot < dot {
Expand All @@ -468,11 +470,11 @@ func Number(num []byte, prec int) []byte {
// find new end, considering moving numbers to the front, removing the dot and increasing the length of the exponent
newEnd := end
if dot == start {
newEnd = start + n
newEnd = dot + n
} else {
newEnd--
}
newEnd += 2 + lenIntExp
newEnd += 1 + lenIntExp

exp := intExp
lenExp := lenIntExp
Expand All @@ -490,19 +492,16 @@ func Number(num []byte, prec int) []byte {
} else {
// it does not save space and will panic, so we revert to the original representation
exp = origExp
lenExp = 1
if origExp <= -10 || 10 <= origExp {
lenExp = strconv.LenInt(int64(origExp))
}
lenExp = strconv.LenInt(int64(origExp))
}
num[end] = 'e'
num[end+1] = '-'
end += 2
for i := end + lenExp - 1; end <= i; i-- {
for i := end + lenExp - 2; end <= i; i-- {
num[i] = -byte(exp%10) + '0'
exp /= 10
}
end += lenExp
end += lenExp - 1
}

if neg {
Expand Down
4 changes: 3 additions & 1 deletion common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ func TestNumber(t *testing.T) {
{"11e9223372036854775807", "11e9223372036854775807"},
{".01e-9223372036854775808", ".01e-9223372036854775808"},
{".011e-9223372036854775808", ".011e-9223372036854775808"},
{"1e-1", ".1"},
{"e-1", "e-1"},

{".12345e8", "12345e3"},
{".12345e7", "1234500"},
Expand Down Expand Up @@ -298,7 +300,7 @@ func TestNumberTruncate(t *testing.T) {
{".3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333397e-902", 0, "3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333397e-999"},
{".3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333397e-903", 0, ".3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333397e-903"},
{"29.629775e-9", 0, ".29629775e-7"},
{"e-9223372036854775808", 0, "e-9223372036854775808"},
{"1e-9223372036854775808", 0, "1e-9223372036854775808"},
{"139.99999999", 8, "140"},
}
for _, tt := range numberTests {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/fsnotify/fsnotify v1.8.0
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2
github.com/tdewolff/argp v0.0.0-20250209172303-079abae893fb
github.com/tdewolff/parse/v2 v2.8.0
github.com/tdewolff/parse/v2 v2.8.1
github.com/tdewolff/test v1.0.11
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/tdewolff/argp v0.0.0-20250209172303-079abae893fb h1:Id2aj2q74MC+yHBGQNeTVuQp/gqJd3vZjMmdoVPPfVc=
github.com/tdewolff/argp v0.0.0-20250209172303-079abae893fb/go.mod h1:PKhwRVvnrI2gye5NRF3c4VWbE+3E9mGyRUsNWGcJlDY=
github.com/tdewolff/parse/v2 v2.8.0 h1:jW0afj6zpUGXuZTwJ7/UfP2SddyLalb/SDryjaMTkA4=
github.com/tdewolff/parse/v2 v2.8.0/go.mod h1:Hwlni2tiVNKyzR1o6nUs4FOF07URA+JLBLd6dlIXYqo=
github.com/tdewolff/parse/v2 v2.8.1 h1:J5GSHru6o3jF1uLlEKVXkDxxcVx6yzOlIVIotK4w2po=
github.com/tdewolff/parse/v2 v2.8.1/go.mod h1:Hwlni2tiVNKyzR1o6nUs4FOF07URA+JLBLd6dlIXYqo=
github.com/tdewolff/test v1.0.6/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
github.com/tdewolff/test v1.0.11 h1:FdLbwQVHxqG16SlkGveC0JVyrJN62COWTRyUFzfbtBE=
github.com/tdewolff/test v1.0.11/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8=
Expand Down
6 changes: 1 addition & 5 deletions js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (m *jsMinifier) minifyStmt(i js.IStmt) {
m.minifyExpr(stmt.Init, js.OpExpr)
m.write(closeParenOpenBracketBytes)
m.needsSemicolon = false
for i, _ := range stmt.List {
for i := range stmt.List {
stmt.List[i].List = optimizeStmtList(stmt.List[i].List, defaultBlock)
}
m.renamer.renameScope(stmt.Scope)
Expand Down Expand Up @@ -1194,10 +1194,6 @@ func (m *jsMinifier) minifyExpr(i js.IExpr, prec js.OpPrec) {
m.write(newTargetBytes)
m.writeSpaceBeforeIdent()
case *js.ImportMetaExpr:
if m.expectExpr == expectExprStmt {
m.write(openParenBytes)
m.groupedStmt = true
}
m.write(importMetaBytes)
m.writeSpaceBeforeIdent()
case *js.YieldExpr:
Expand Down
4 changes: 3 additions & 1 deletion js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,9 @@ func TestJS(t *testing.T) {
{`(a?.b.c).d`, `a?.b.c.d`},
{`class a extends (new b){}`, `class a extends new b{}`},
{`(new.target)`, `new.target`},
{`(import.meta)`, `(import.meta)`},
{`(import.meta)`, `import.meta`},
{`import.meta.url`, `import.meta.url`},
{`super.name`, `super.name`},
{"(`tmpl`)", "`tmpl`"},
{"(a`tmpl`)", "a`tmpl`"},
{"a=-(b=5)", "a=-(b=5)"},
Expand Down
Loading