Skip to content
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

common: Improve IsHex and IsHexAddress & add tests #15551

Merged
merged 3 commits into from
Dec 4, 2017

Conversation

stevenroose
Copy link
Contributor

Fixes #15550.

common/bytes.go Outdated
func IsHex(str string) bool {
l := len(str)
return l >= 4 && l%2 == 0 && str[0:2] == "0x"
return hexRegex.MatchString(str)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only use of IsHex is in IsHexAddress, also in package common. I think it would be best to delete IsHex and improve IsHexAddress instead. Bonus points if you can do it without a regular expression ;)

@stevenroose
Copy link
Contributor Author

stevenroose commented Nov 27, 2017 via email

@fjl
Copy link
Contributor

fjl commented Nov 27, 2017

Yes

@fjl
Copy link
Contributor

fjl commented Nov 27, 2017

Regular expressions aren't bad, but package regexp is big and it's nice to avoid using it whenever possible.

@stevenroose
Copy link
Contributor Author

$ grep -rn "\"regexp\""
accounts/abi/bind/bind.go:26:	"regexp"
accounts/abi/type.go:22:	"regexp"
Binary file build/_workspace/pkg/linux_amd64/github.com/ethereum/go-ethereum/vendor/golang.org/x/tools/imports.a matches
build/update-license.go:31:	"regexp"
build/ci.go:56:	"regexp"
cmd/faucet/faucet.go:38:	"regexp"
common/compiler/solidity.go:27:	"regexp"
common/format.go:21:	"regexp"
common/bytes.go:22:	"regexp"
console/console.go:26:	"regexp"
internal/cmdtest/test_cmd.go:27:	"regexp"
p2p/discover/node.go:29:	"regexp"
p2p/discv5/node.go:29:	"regexp"
p2p/simulations/adapters/exec.go:32:	"regexp"
tests/init_test.go:27:	"regexp"
swarm/api/api.go:23:	"regexp"
vendor/github.com/davecgh/go-spew/spew/dump.go:26:	"regexp"
vendor/github.com/gizak/termui/helper.go:8:	"regexp"
vendor/github.com/gizak/termui/textbuilder.go:8:	"regexp"
vendor/github.com/huin/goupnp/httpu/serve.go:9:	"regexp"
vendor/github.com/huin/goupnp/soap/types.go:9:	"regexp"
vendor/github.com/huin/goupnp/ssdp/registry.go:8:	"regexp"
vendor/github.com/mattn/go-runewidth/runewidth_posix.go:7:	"regexp"
vendor/github.com/robertkrimen/otto/builtin_function.go:5:	"regexp"
vendor/github.com/robertkrimen/otto/cmpl_parse.go:5:	"regexp"
vendor/github.com/robertkrimen/otto/dbg/dbg.go:65:	"regexp"
vendor/github.com/robertkrimen/otto/otto.go:139:Go translates JavaScript-style regular expressions into something that is "regexp" compatible via `parser.TransformRegExp`.
vendor/github.com/robertkrimen/otto/otto_.go:5:	"regexp"
vendor/github.com/robertkrimen/otto/parser/README.markdown:79:TransformRegExp transforms a JavaScript pattern into a Go "regexp" pattern.
vendor/github.com/robertkrimen/otto/parser/expression.go:4:	"regexp"
vendor/github.com/robertkrimen/otto/parser/lexer.go:7:	"regexp"
vendor/github.com/robertkrimen/otto/parser/regexp.go:23:// TransformRegExp transforms a JavaScript pattern into  a Go "regexp" pattern.
vendor/github.com/robertkrimen/otto/type_date.go:6:	"regexp"
vendor/github.com/robertkrimen/otto/type_regexp.go:5:	"regexp"
vendor/github.com/robertkrimen/otto/value_string.go:6:	"regexp"
vendor/github.com/robertkrimen/otto/README.markdown:176:"regexp" compatible via `parser.TransformRegExp`. Unfortunately, RegExp requires
vendor/github.com/robertkrimen/otto/builtin.go:7:	"regexp"
vendor/github.com/robertkrimen/otto/builtin_string.go:5:	"regexp"
vendor/github.com/robertkrimen/otto/value_number.go:6:	"regexp"
vendor/github.com/Azure/go-autorest/autorest/date/time.go:4:	"regexp"
vendor/github.com/maruel/panicparse/stack/stack.go:21:	"regexp"
vendor/github.com/olekukonko/tablewriter/table.go:15:	"regexp"
vendor/github.com/olekukonko/tablewriter/util.go:12:	"regexp"
vendor/github.com/stretchr/testify/assert/assertions.go:11:	"regexp"
vendor/golang.org/x/text/language/maketables.go:21:	"regexp"
vendor/golang.org/x/tools/imports/mkstdlib.go:19:	"regexp"
vendor/golang.org/x/tools/imports/zstdlib.go:2846:	"regexp.Compile":                "regexp",
vendor/golang.org/x/tools/imports/zstdlib.go:2847:	"regexp.CompilePOSIX":           "regexp",
vendor/golang.org/x/tools/imports/zstdlib.go:2848:	"regexp.Match":                  "regexp",
vendor/golang.org/x/tools/imports/zstdlib.go:2849:	"regexp.MatchReader":            "regexp",
vendor/golang.org/x/tools/imports/zstdlib.go:2850:	"regexp.MatchString":            "regexp",
vendor/golang.org/x/tools/imports/zstdlib.go:2851:	"regexp.MustCompile":            "regexp",
vendor/golang.org/x/tools/imports/zstdlib.go:2852:	"regexp.MustCompilePOSIX":       "regexp",
vendor/golang.org/x/tools/imports/zstdlib.go:2853:	"regexp.QuoteMeta":              "regexp",
vendor/golang.org/x/tools/imports/zstdlib.go:2854:	"regexp.Regexp":                 "regexp",
vendor/golang.org/x/tools/imports/imports.go:21:	"regexp"
vendor/gopkg.in/check.v1/check.go:19:	"regexp"
vendor/gopkg.in/check.v1/checkers.go:6:	"regexp"
ethstats/ethstats.go:27:	"regexp"
log/handler_glog.go:22:	"regexp"

@fjl
Copy link
Contributor

fjl commented Nov 27, 2017

Hmm, so what this tells me is that we're not that far away from removing the common -> regexp dependency ;)

@stevenroose stevenroose force-pushed the ishex branch 2 times, most recently from 7497bf2 to 8360ce3 Compare November 29, 2017 12:09
@stevenroose
Copy link
Contributor Author

@fjl Updated. Also improved IsHexAddress and added some tests for that.

@stevenroose
Copy link
Contributor Author

(That switch statement there is copied from Go's hex package.

@stevenroose stevenroose force-pushed the ishex branch 2 times, most recently from 643f3cb to 25abdcb Compare November 29, 2017 12:35
@stevenroose stevenroose changed the title common: Correct IsHex to check if content is hex common: Improve IsHex and IsHexAddress & add tests Nov 29, 2017
common/bytes.go Outdated
}
}

return true
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove blank lines in this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

common/bytes.go Outdated
}

src := []byte(str)
for _, c := range src {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use for _, c := range []byte(src) because src is otherwise unused.

common/bytes.go Outdated
case 'A' <= c && c <= 'F':
return true
}
return false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a boolean expression instead of switch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The switch is taken from the hex package. I think it's more readable. If you insist, I could change it...

common/types.go Outdated
if len(s) == 2+2*AddressLength && IsHex(s) {
return true
if HasHexPrefix(s) {
return len(s) == 2+2*AddressLength && IsHex(s)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be s = s[2:] to avoid repeating the check expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True.

Also unexport IsHex, HasHexPrefix because IsHexAddress is the only caller.
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
}

func isHex(str string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the semantics of IsHex/isHex. Previously, it expected a 0x-prefixed string, now it expects no prefix. That's fine, though, since it's internal we know it's not used anywhere where it will break anything.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only use is in IsHexAddress, which already checks for the prefix ;)

@fjl fjl merged commit afb8154 into ethereum:master Dec 4, 2017
@stevenroose
Copy link
Contributor Author

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants