Skip to content

common: Improve IsHex and IsHexAddress & add tests#15551

Merged
fjl merged 3 commits into
ethereum:masterfrom
stevenroose:ishex
Dec 4, 2017
Merged

common: Improve IsHex and IsHexAddress & add tests#15551
fjl merged 3 commits into
ethereum:masterfrom
stevenroose:ishex

Conversation

@stevenroose
Copy link
Copy Markdown
Contributor

Fixes #15550.

Comment thread common/bytes.go Outdated
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

stevenroose commented Nov 27, 2017 via email

@fjl
Copy link
Copy Markdown
Contributor

fjl commented Nov 27, 2017

Yes

@fjl
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

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

@stevenroose
Copy link
Copy Markdown
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
Comment thread common/bytes.go Outdated
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment thread common/bytes.go Outdated
Copy link
Copy Markdown
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.

Comment thread common/bytes.go Outdated
Copy link
Copy Markdown
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
Copy Markdown
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...

Comment thread common/types.go Outdated
Copy link
Copy Markdown
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
Copy Markdown
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.
Comment thread common/bytes.go
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
}

func isHex(str string) bool {
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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