Live data from Hacker News

Go-Restructure: Sane regular expressions with struct fields

github.com

41–47 of 47 posts

Re: Go-Restructure: Sane regular expressions with struct fields

#41
post #11

This depends on reflect. What's the speed penalty?

I'm pretty sure you can't avoid it in the compilation phase, but it seems like you could remove reflection from the run phase by determining the offset of the struct field corresponding to each capture group and using unsafe operations to access the fields in order to write them.

Re: Go-Restructure: Sane regular expressions with struct fields

#42

This is such a simple but beautiful idea. I love it! As a Swift developer I am really envious of these tagged fields on Go and how they simplify e.g. JSON parsing. And now I realise they can simplify regexp parsing as well. Dam!!

Yeah this is really cool. But it's at least in part because the Go community has grown exponentially over the past year or two. It seems like it's fair to expect the same will befall the Swift community pretty shortly here.

Re: Go-Restructure: Sane regular expressions with struct fields

#43
post #14

Here's a 1-1 port in JavaScript https://github.com/benjamingr/js-restructure function matcher(obj) { "use strict"; let props = Object.getOwnPropertyNames(obj); const re = new RegExp(props.reduce((p, c) => p + (c.startsWith("_") ? obj[c] : `(${obj[c]})`), "")); props = props.filter(x => !x.startsWith("_")); return function(pattern) { let o = {}; const res = re.exec(pattern); for(let i = 0; i And example usage: ```js m…

JavaScript doesn't guarantee the order of keys. [edit] Also, the API makes less sense because JavaScript isn't statically typed so you can make up the result object on the fly. match('^(? \w+)@(? [^@]+)$', 'joe@example.com') => {'user': 'joe', 'host': 'example.com'}

> [edit] Also, the API makes less sense because JavaScript isn't statically typed so you can make up the result object on the fly.

But now you need to parse the string and correctly extract named groups before flogging that to the regex engine, whereas the other way around you do some string concatenation then match back on the groups by index.

Re: Go-Restructure: Sane regular expressions with struct fields

#44
post #14

Here's a 1-1 port in JavaScript https://github.com/benjamingr/js-restructure function matcher(obj) { "use strict"; let props = Object.getOwnPropertyNames(obj); const re = new RegExp(props.reduce((p, c) => p + (c.startsWith("_") ? obj[c] : `(${obj[c]})`), "")); props = props.filter(x => !x.startsWith("_")); return function(pattern) { let o = {}; const res = re.exec(pattern); for(let i = 0; i And example usage: ```js m…

JavaScript doesn't guarantee the order of keys. [edit] Also, the API makes less sense because JavaScript isn't statically typed so you can make up the result object on the fly. match('^(? \w+)@(? [^@]+)$', 'joe@example.com') => {'user': 'joe', 'host': 'example.com'}

JavaScript has no named capturing groups.

Re: Go-Restructure: Sane regular expressions with struct fields

#46

Very nice solution! I try to avoid using 3rd party libraries but this I would use without hesitation. Some benchmarks would be interesting.

Thanks! I'm planning to do some benchmarking so will hopefully have something up in the next few days.

Re: Go-Restructure: Sane regular expressions with struct fields

#47
post #29

I recommend that the first example be changed to use struct{} as well. The "strings" are still present in the struct footprint if you leave them there: http://play.golang.org/p/ZG1ULgzSwZ And people luuuuuv to pick up the one example where you did something a bit wrong and copy paste like mad.... I'd really rather see something that generates marshaling code based on a regex or something, though. This loads an awful…

Thanks for the feedback. One of the motivations behind go-restructure was to help with regex composition. This happens to be particularly pressing at my day job where we have a few small regexes that get copied into lots of larger regexes, which means that when we find a bug in the small regex we have to go and update lots of other strings.

One alternative is to build up regexes dynamically concatenating smaller regexes, but that leads to whole other kind of hell.

Post reply on HN