Skip to content

Commit 8c0f670

Browse files
committed
Match the spec's "find the IPv6 address compressed piece index"
The spec's algorithm is based on ours, but the algorithm and variable names changed, so let's align. Follows whatwg/url@7ff8de0.
1 parent 89b697a commit 8c0f670

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ whatwg-url is a full implementation of the WHATWG [URL Standard](https://url.spe
44

55
## Specification conformance
66

7-
whatwg-url is currently up to date with the URL spec up to commit [302c5af](https://github.com/whatwg/url/commit/302c5afb376b7fb1d5943634ad6c3a2e71c37f37).
7+
whatwg-url is currently up to date with the URL spec up to commit [da212c9](https://github.com/whatwg/url/commit/da212c98f0bba9c35aec3728bbcc13f8f3a7eb52).
88

99
For `file:` URLs, whose [origin is left unspecified](https://url.spec.whatwg.org/#concept-url-origin), whatwg-url chooses to use a new opaque origin (which serializes to `"null"`).
1010

lib/url-state-machine.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ function parseIPv6(input) {
298298

299299
function serializeIPv6(address) {
300300
let output = "";
301-
const compress = findLongestZeroSequence(address);
301+
const compress = findTheIPv6AddressCompressedPieceIndex(address);
302302
let ignore0 = false;
303303

304304
for (let pieceIndex = 0; pieceIndex <= 7; ++pieceIndex) {
@@ -384,35 +384,34 @@ function parseOpaqueHost(input) {
384384
return utf8PercentEncodeString(input, isC0ControlPercentEncode);
385385
}
386386

387-
function findLongestZeroSequence(arr) {
388-
let maxIdx = null;
389-
let maxLen = 1; // only find elements > 1
390-
let currStart = null;
391-
let currLen = 0;
392-
393-
for (let i = 0; i < arr.length; ++i) {
394-
if (arr[i] !== 0) {
395-
if (currLen > maxLen) {
396-
maxIdx = currStart;
397-
maxLen = currLen;
387+
function findTheIPv6AddressCompressedPieceIndex(address) {
388+
let longestIndex = null;
389+
let longestSize = 1; // only find elements > 1
390+
let foundIndex = null;
391+
let foundSize = 0;
392+
393+
for (let pieceIndex = 0; pieceIndex < address.length; ++pieceIndex) {
394+
if (address[pieceIndex] !== 0) {
395+
if (foundSize > longestSize) {
396+
longestIndex = foundIndex;
397+
longestSize = foundSize;
398398
}
399399

400-
currStart = null;
401-
currLen = 0;
400+
foundIndex = null;
401+
foundSize = 0;
402402
} else {
403-
if (currStart === null) {
404-
currStart = i;
403+
if (foundIndex === null) {
404+
foundIndex = pieceIndex;
405405
}
406-
++currLen;
406+
++foundSize;
407407
}
408408
}
409409

410-
// if trailing zeros
411-
if (currLen > maxLen) {
412-
return currStart;
410+
if (foundSize > longestSize) {
411+
return foundIndex;
413412
}
414413

415-
return maxIdx;
414+
return longestIndex;
416415
}
417416

418417
function serializeHost(host) {

0 commit comments

Comments
 (0)