Skip to content

Commit

Permalink
Short-circuit IPv6 validity check with string.indexOf(":") == -1.
Browse files Browse the repository at this point in the history
Fixes #27.
  • Loading branch information
whitequark committed Oct 17, 2015
1 parent 51abe4e commit 500dca0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ipaddr.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion lib/ipaddr.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion src/ipaddr.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,19 @@ ipaddr.IPv4.isIPv4 = ipaddr.IPv6.isIPv6 = (string) ->
return @parser(string) != null

# Checks if a given string is a valid IPv4/IPv6 address.
ipaddr.IPv4.isValid = ipaddr.IPv6.isValid = (string) ->
ipaddr.IPv4.isValid = (string) ->
try
new this(@parser(string))
return true
catch e
return false

ipaddr.IPv6.isValid = (string) ->
# Since IPv6.isValid is always called first, this shortcut
# provides a substantial performance gain.
if string.indexOf(":") == -1
return false

try
new this(@parser(string))
return true
Expand Down

0 comments on commit 500dca0

Please sign in to comment.