Parsing ready
"a.b.c.d", "addr/prefixlen", "addr/netmask", bracketed IPv6 ("[::1]/64"), %zone identifiers and embedded IPv4 tails ("::ffff:1.2.3.4"), with MRI’s ambiguous-zero-fill and out-of-range octet rejections.
Ruby's IPAddr in pure Go โ parsing, masking, predicates & operators, MRI byte-exact, no cgo.
go-ruby-ipaddr is a pure-Go (no cgo) reimplementation of Ruby's IPAddr standard library โ MRI's IPAddr class. It models an IP address together with a netmask (IPv4 or IPv6) and reproduces MRI's parsing, masking, string formatting, set predicates, bitwise operators and comparison semantics byte-for-byte, without any Ruby runtime. The arithmetic is carried in math/big.Int, so the full 128-bit IPv6 space and MRI's unbounded-integer behaviour are matched exactly. It is a standalone, reusable module โ a sibling of go-ruby-regexp, go-ruby-erb and go-ruby-yaml โ and the IPAddr backend for go-embedded-ruby.
"a.b.c.d", "addr/prefixlen", "addr/netmask", bracketed IPv6 ("[::1]/64"), %zone identifiers and embedded IPv4 tails ("::ffff:1.2.3.4"), with MRI’s ambiguous-zero-fill and out-of-range octet rejections.
to_s (compact, with MRI’s exact zero-run collapsing and the ::a.b.c.d / ::ffff:a.b.c.d rewrites), to_string (canonical expanded), cidr, inspect (#<IPAddr: IPv4:โฆ/mask>) and netmask.
mask(prefixlen|netmask), prefix / prefix=, masked construction with non-contiguous-netmask rejection; include? / ===, to_range, and an idiomatic Each over the range.
&, |, ~, +, -, succ, and Xor (the natural completion of the &/| set), all coercing strings / integers / IPAddr, carried in math/big.Int over the full 128-bit space.
<=> (Comparable), ==, Hash; ipv4? / ipv6? / loopback? / private? / link_local? / ipv4_mapped? / ipv4_compat?; native / ipv4_mapped / ipv4_compat / hton / ntop / new_ntoh / family / to_i; the InvalidAddressError / InvalidPrefixError / AddressFamilyError family.
A wide corpus parsed and formatted here and compared to the system ruby -ripaddr โ strings, predicates, operators, to_range, conversions, error class + message. 100% coverage, gofmt + go vet clean, green across all six 64-bit Go arches and three OSes.
A faithful port of Ruby's IPAddr class in pure Go, cgo disabled, so it cross-compiles and embeds anywhere. It parses addr/prefixlen / addr/netmask, bracketed IPv6 and %zone identifiers; formats to_s / to_string / cidr / inspect; masks; tests membership (include? / ===, to_range); and offers the & / | / ~ / + / - / succ operators and the predicate set โ all carried in math/big.Int. Validated differentially against the system ruby -ripaddr. The IPAddr backend for the sibling org github.com/go-embedded-ruby.