-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add Socket::IPAddress.valid?
#10492
Add Socket::IPAddress.valid?
#10492
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 It'll definitely migrate the logic for https://athenaframework.org/Validator/Constraints/IP/ to use this verus the current ugly regexes.
I also tested the implementation against the spec suite for the constraint and was all green.
old 10.79k ( 92.72µs) (± 2.18%) 34.7kB/op 48.16× slower
new 519.40k ( 1.93µs) (± 1.92%) 2.19kB/op fastest
EDIT: Added benchmark between the two
I realized there is already
|
@straight-shoota I think it'd make more sense to have them on the actual |
@straight-shoota Any plans on revisiting this? |
I think we should go with the proposed action to add the new |
a7aedd6
to
77d204f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @straight-shoota 🙏
Hi, when run code
https://github.com/crystal-lang/crystal/blob/master/src/openssl/ssl/socket.cr#L18 |
This patch adds utility methods
IPAddress.valid?
as well as.valid_v4?
and.valid_v6?
which enable testing whether a string describes a valid IP address.A practical use case is validation of a cookie's
Domain
attribute which can either be a domain name or an IPv4 address. I'm sure it can be useful for other purposes as well.The implementation actually tries to parse the string into an
InAddr
orIn6Addr
and returns true if that was successful. This can probably be more performant with a custom implementation, but for now I've just adapted what was already used for the constructor.