Skip to content

IPAddress#loopback? should consider ::ffff:127.0.0.1/104 loopback too#12783

Merged
straight-shoota merged 8 commits intocrystal-lang:masterfrom
carlhoerberg:ipv6_loopback
Jan 6, 2023
Merged

IPAddress#loopback? should consider ::ffff:127.0.0.1/104 loopback too#12783
straight-shoota merged 8 commits intocrystal-lang:masterfrom
carlhoerberg:ipv6_loopback

Conversation

@carlhoerberg
Copy link
Contributor

No description provided.

@straight-shoota straight-shoota changed the title IPAddress#loopback? should consider ::ffff:127.0.0.1/104 loopback too IPAddress#loopback? should consider ::ffff:127.0.0.1/104 loopback too Nov 29, 2022
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
ipv6_addr8(addr) == StaticArray[0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 1_u8]
addr8 = ipv6_addr8(addr)
addr8 == StaticArray[0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 1_u8] || # "::1"
addr8.to_slice[0, 13] == Bytes[0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 255_u8, 255_u8, 127_u8] # "::ffff:127.0.0.1/104"
Copy link
Contributor

@HertzDevil HertzDevil Dec 13, 2022

Choose a reason for hiding this comment

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

This should be turned into a constant to avoid having to allocate the Bytes on every call (and the StaticArray above too if you wish)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or StaticArray#to_slice? will only stack allocate.

Copy link
Member

Choose a reason for hiding this comment

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

I think it might be simpler to turn the raw bytes into UInt64 or whatever and compare things against numbers. It should also be faster.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same speed for StaticArray[...]#to_slice and .unsafe_as(UInt128) (1ns to do the check), I guess it comes down to readability. See for comparison: 3663384

As they aren't supported in older crystal versions

Co-authored-by: Johannes Müller <straightshoota@gmail.com>
@straight-shoota
Copy link
Member

@carlhoerberg Something went wrong with your rebase. I suppose you intended to update the branch on master. But somewhow that got botched and there were some weird commits that don't belong here.
We prefer to just merge master into the branch, no rebase.
I reset the branch and merged master.

@straight-shoota straight-shoota added this to the 1.7.0 milestone Jan 4, 2023
@straight-shoota straight-shoota merged commit 5402f6a into crystal-lang:master Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants