fix the bug of ipToString with tailing double colon

This commit is contained in:
lion 2025-10-13 18:42:20 +08:00
parent ed42fd0fba
commit e073e26f52
2 changed files with 7 additions and 3 deletions

View File

@ -10,7 +10,7 @@ import * as util from '../util.js';
test('parse ip address', () => { test('parse ip address', () => {
let ip_list = [ let ip_list = [
"1.0.0.0", "58.251.30.115", "192.168.1.100", "126.255.32.255", "219.xx.xx.11", "1.0.0.0", "58.251.30.115", "192.168.1.100", "126.255.32.255", "219.xx.xx.11",
"::", "::1", "fffe::", "2c0f:fff0::", "2c0f:fff0::1", "::", "::1", "fffe::", "2c0f:fff0::", "2c0f:fff0::1", "2a02:26f7:c409:4001::",
"2fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "240e:982:e617:ffff:ffff:ffff:ffff:ffff", "::xx:ffff" "2fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "240e:982:e617:ffff:ffff:ffff:ffff:ffff", "::xx:ffff"
]; ];
@ -26,6 +26,7 @@ test('parse ip address', () => {
let to_Str = ipBytes == null ? '0' : util.ipToString(ipBytes, true); let to_Str = ipBytes == null ? '0' : util.ipToString(ipBytes, true);
let toByte = ipBytes == null ? '0' : util.ipBytesString(ipBytes); let toByte = ipBytes == null ? '0' : util.ipBytesString(ipBytes);
console.log(`parseIP(${ipString}): {Bytes: ${toByte}, String: ${to_Str}}`); console.log(`parseIP(${ipString}): {Bytes: ${toByte}, String: ${to_Str}}`);
expect(ipString).toBe(to_Str);
}); });
}); });

View File

@ -196,7 +196,7 @@ function _ipv6_to_string(v6Bytes, compress) {
_.push(''); // empty for double colon _.push(''); // empty for double colon
// make sure there is an empty tail // make sure there is an empty tail
if (i == ps.length && _.length == 2) { if (i == ps.length && _.length < ps.length) {
_.push(''); _.push('');
} }
} }
@ -375,4 +375,7 @@ export function loadContentFromFile(dbPath) {
const content = loadContent(fd); const content = loadContent(fd);
fs.close(fd, function(){}); fs.close(fd, function(){});
return content; return content;
} }
const ipBytes = parseIP("2a02:26f7:c409:4001::");
console.log(ipToString(ipBytes));