Thanks to visit codestin.com
Credit goes to github.com

Skip to content

compiler/natives/src/strconv: Use js string conversion utilities to improve performance #786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
31 changes: 27 additions & 4 deletions compiler/natives/fs_vfsdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions compiler/natives/src/strconv/atoi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// +build js

package strconv

import (
"github.com/gopherjs/gopherjs/js"
)

const maxInt32 float64 = 1<<31 - 1
const minInt32 float64 = -1 << 31

// Atoi returns the result of ParseInt(s, 10, 0) converted to type int.
func Atoi(s string) (int, error) {
const fnAtoi = "Atoi"
if len(s) == 0 {
return 0, syntaxError(fnAtoi, s)
}
// Investigate the bytes of the string
// Validate each byte is allowed in parsing
// Number allows some prefixes that Go does not: "0x" "0b", "0o"
// additionally Number accepts decimals where Go does not "10.2"
for i := 0; i < len(s); i++ {
v := s[i]

if v < '0' || v > '9' {
if v != '+' && v != '-' {
return 0, syntaxError(fnAtoi, s)
}
}
}
jsValue := js.Global.Call("Number", s, 10)
if !js.Global.Call("isFinite", jsValue).Bool() {
return 0, syntaxError(fnAtoi, s)
}
// Bounds checking
floatval := jsValue.Float()
if floatval > maxInt32 {
return int(maxInt32), rangeError(fnAtoi, s)
} else if floatval < minInt32 {
return int(minInt32), rangeError(fnAtoi, s)
}
// Success!
return jsValue.Int(), nil
}
13 changes: 13 additions & 0 deletions compiler/natives/src/strconv/itoa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build js

package strconv

import (
"github.com/gopherjs/gopherjs/js"
)

// Itoa in gopherjs is always a 32bit int so the native toString
// always handles it successfully.
func Itoa(i int) string {
return js.InternalObject(i).Call("toString").String()
}