Skip to content

@stdlib/string/base/percent-encode produces malformed encoding and silently drops characters #15595

Description

@barbierajput378-pixel

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

  • Read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions