Apologies a head of time if I'm out of line.
I've been working on an node and IPv6 project for my own curiosity and I've
found what for me is a big problem with the current IPv6 portion of
net.isIP. Basically, anything starting with :: returns as a valid IPv6
address. I'm not even at a regex newb level, but I felt that revamping the
regex would just create one that was way too complex. Perhaps it could be
done, but I don't have the skills. So I ended up doing what I know best,
brute forcing a solution.
What I settled on was hand parsing the address and making that as fast as
possible. With the tests below, the speed of my method is comparable with
the old net.isIP. Being only within a few milliseconds over 10,000
iterations. I feel this is an acceptable number for the trade off in
accuracy.
At the moment I'm trying to learn how to build node and run the tests
against it, so getting this into the pull request queue may take some time.
In the mean time, could I get some scathing commentary on my approach and
any pointers on how this could be done better or faster?
Thanks,
Josh Erickson
//Begin Code
var new_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;
//Changes start here
} else if(/([a-fA-F0-9:]){2,39}/.test(input)) {
var parts = input.split(":");
var colons = 0;
for(var i=0; i<parts.length; i++) {
if(parts[i].length > 4 || (parts[i] != "" &&
!(/([a-fA-F0-9]){1,4}/.test(parts[i])))) {
return 0;
}
if(parts[i].length == 0 && i%(parts.length-1)!=0) {
colons++;
}
}
return (colons>1)?0:6;
//changes end here
} else {
return 0;
}
};
//speed tests
var net = require('net');
var t=["::t", "::", "a::b::c","::122:1:1","1::","::a11:abcd",
"2001:aaa::41"];
console.log("old net.isIP");
for(var ti in t) {
console.time(t[ti]);
for(var i=0;i<10000;i++) {
net.isIP(t[ti]);
}
console.timeEnd(t[ti]);
}
console.log("new isIP");
for(var ti in t) {
console.time(t[ti]);
for(var i=0;i<10000;i++) {
isIP(t[ti]);
}
console.timeEnd(t[ti]);
}
--
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