Thanks guys! I didn't know that about split operations.

Jonny, do you have benchmarks handy on your django port? If not, I'll hack 
something together and get them myself. If it is faster, I'd just rather it 
be brought into node itself then me stumble around in the dark anymore.

This past day I've done some more thinking on how regex might be done 
differently so as to not use splits and less lines. The result is the code 
below. The additions to the regex currently found in node fix the :: prefix 
issue as well as a::b::c and a:b:c situations. The trade off is that in 
some cases the tests can 3-5 times slower than the old net.isIP. I still 
think that's okay if better results are given. Still though, I want to 
believe this could be faster. I do have some code laying around for 
converting an IPv6 string to Buffer object.

It's also ran though the test-net-isip.js unit without issue, which is more 
than I could have said for my last set of code.

Beyond these, perhaps delving into some C and sending data straight to 
inet_pton and letting it success or fail would bring the speed up.

isIP = function(input) {
  if (!input) {
    return 0;
  } else if 
(/^(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)$/.test(input)) {
    var parts = input.split('.');
    for (var i = 0; i < parts.length; i++) {
      var part = parseInt(parts[i]);
      if (part < 0 || 255 < part) {
        return 0;
      }
    }
    return 4;
  } else if 
(/^::$|^::1$|^([a-fA-F0-9]{0,4}::?){1,7}([a-fA-F0-9]{0,4})$/.test(input) && 
!(/::.+::/.test(input))) {
    if (input.match(/:/g).length < 7 && !(/::/.test(input))) {
      return 0;
    }
    return 6;
  } else {
    return 0;
  }
};

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to