Description
percentEncode claims RFC 3986 conformance but has two related bugs, both caused by using a UTF-8 byte index to also index the original UTF-16 string.
Root cause
utf16ToUTF8Array returns an array of UTF-8 byte octets, so the loop variable i in lib/main.js counts bytes. But str.charAt(i) (used for unreserved characters) indexes UTF-16 code units. These only coincide for pure-ASCII strings — any multi-byte character before it throws off every subsequent index.
Related Issues
Related issues # , # , and # .
Questions
No.
Demo
No response
Reproduction
var percentEncode = require('@stdlib/string/base/percent-encode');
percentEncode('\t'); // Bug 1: missing zero-padding
percentEncode('\r'); // Bug 1: missing zero-padding
percentEncode('é1'); // Bug 2: character silently dropped
Expected Results
percentEncode('\t') → '%09'
percentEncode('\r') → '%0D'
percentEncode('é1') → '%C3%A91'
Actual Results
percentEncode('\t') → '%9'
percentEncode('\r') → '%D'
percentEncode('é1') → '%C3%A9' (the '1' is dropped entirely)
Version
develop
Environments
Node.js
Browser Version
No response
Node.js / npm Version
v24.17.0
Platform
Windows
Checklist
Description
percentEncodeclaims RFC 3986 conformance but has two related bugs, both caused by using a UTF-8 byte index to also index the original UTF-16 string.Root cause
utf16ToUTF8Arrayreturns an array of UTF-8 byte octets, so the loop variableiinlib/main.jscounts bytes. Butstr.charAt(i)(used for unreserved characters) indexes UTF-16 code units. These only coincide for pure-ASCII strings — any multi-byte character before it throws off every subsequent index.Related Issues
Related issues # , # , and # .
Questions
No.
Demo
No response
Reproduction
var percentEncode = require('@stdlib/string/base/percent-encode');
percentEncode('\t'); // Bug 1: missing zero-padding
percentEncode('\r'); // Bug 1: missing zero-padding
percentEncode('é1'); // Bug 2: character silently dropped
Expected Results
Actual Results
Version
develop
Environments
Node.js
Browser Version
No response
Node.js / npm Version
v24.17.0
Platform
Windows
Checklist