Skip to content

Commit

Permalink
[ruby/ipaddr] String#split seems to be faster than capturing digits w…
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda authored and matzbot committed Feb 23, 2024
1 parent 32f8ed7 commit 6ad52e3
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class IPAddr
# Regexp _internally_ used for parsing IPv4 address.
RE_IPV4ADDRLIKE = %r{
\A
(\d+) \. (\d+) \. (\d+) \. (\d+)
\d+ \. \d+ \. \d+ \. \d+
\z
}x

Expand Down Expand Up @@ -669,8 +669,8 @@ def in_addr(addr)
when Array
octets = addr
else
m = RE_IPV4ADDRLIKE.match(addr) or return nil
octets = m.captures
RE_IPV4ADDRLIKE.match?(addr) or return nil
octets = addr.split('.')
end
octets.inject(0) { |i, s|
(n = s.to_i) < 256 or raise InvalidAddressError, "invalid address: #{@addr}"
Expand Down

0 comments on commit 6ad52e3

Please sign in to comment.