Skip to content

Commit

Permalink
Merge pull request #50 from amatsuda/perf2
Browse files Browse the repository at this point in the history
String#split seems to be faster than capturing digits with Regexp
  • Loading branch information
tenderlove committed Feb 23, 2024
2 parents 10bdaad + e0feb07 commit afc3704
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/ipaddr.rb
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 afc3704

Please sign in to comment.