Skip to content

Commit

Permalink
Match the spec's "find the IPv6 address compressed piece index"
Browse files Browse the repository at this point in the history
The spec's algorithm is based on ours, but the algorithm and variable names changed, so let's align.

Follows whatwg/url@7ff8de0.
  • Loading branch information
domenic committed Dec 4, 2024
1 parent 89b697a commit 8c0f670
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ whatwg-url is a full implementation of the WHATWG [URL Standard](https://url.spe

## Specification conformance

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

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"`).

Expand Down
41 changes: 20 additions & 21 deletions lib/url-state-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ function parseIPv6(input) {

function serializeIPv6(address) {
let output = "";
const compress = findLongestZeroSequence(address);
const compress = findTheIPv6AddressCompressedPieceIndex(address);
let ignore0 = false;

for (let pieceIndex = 0; pieceIndex <= 7; ++pieceIndex) {
Expand Down Expand Up @@ -384,35 +384,34 @@ function parseOpaqueHost(input) {
return utf8PercentEncodeString(input, isC0ControlPercentEncode);
}

function findLongestZeroSequence(arr) {
let maxIdx = null;
let maxLen = 1; // only find elements > 1
let currStart = null;
let currLen = 0;

for (let i = 0; i < arr.length; ++i) {
if (arr[i] !== 0) {
if (currLen > maxLen) {
maxIdx = currStart;
maxLen = currLen;
function findTheIPv6AddressCompressedPieceIndex(address) {
let longestIndex = null;
let longestSize = 1; // only find elements > 1
let foundIndex = null;
let foundSize = 0;

for (let pieceIndex = 0; pieceIndex < address.length; ++pieceIndex) {
if (address[pieceIndex] !== 0) {
if (foundSize > longestSize) {
longestIndex = foundIndex;
longestSize = foundSize;
}

currStart = null;
currLen = 0;
foundIndex = null;
foundSize = 0;
} else {
if (currStart === null) {
currStart = i;
if (foundIndex === null) {
foundIndex = pieceIndex;
}
++currLen;
++foundSize;
}
}

// if trailing zeros
if (currLen > maxLen) {
return currStart;
if (foundSize > longestSize) {
return foundIndex;
}

return maxIdx;
return longestIndex;
}

function serializeHost(host) {
Expand Down

0 comments on commit 8c0f670

Please sign in to comment.