Skip to content

Commit

Permalink
String#split seems to be faster than capturing digits with Regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda committed Jan 6, 2023
1 parent d92acb3 commit e0feb07
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 @@ -647,8 +647,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 e0feb07

Please sign in to comment.