locutusjs / locutus

Bringing stdlibs of other programming languages to JavaScript for educational purposes

Home Page:https://locutus.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Failure to recognize some format and extract values in sscanf

mortezafs opened this issue · comments

Hi.
First of all thank you for your great job.
I'm going to report some bug in sscanf function.
sscanf can not recognize this formats and extract matched values.
Tested at: Windows and ubnutu

Format 1: "%[^[]]"
Test Case:

let dataScanned = sscanf('mobile[country_code]', '%[^[]]');
console.log(dataScanned);

JS sscanf output:
[]
PHP output:

Array
(
    [0] => mobile
)

Format 2: "%[^[][%[^]]]"
Test Case:

let dataScanned = sscanf("alphanumericWithCustomChars[en,fa|-|_|)|(]", "%[^[][%[^]]]");
console.log(dataScanned);

JS sscanf output:
[]
PHP output:

Array
(
    [0] => alphanumericWithCustomChars
    [1] => en,fa|-|_|)|(
)

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 14 days.

The issue you're encountering with extracting values based on the provided formats (%[^[]] and %[^[][%[^]]]) stems from differences in how JavaScript's regular expression engine interprets patterns compared to PHP's implementation, particularly regarding the handling of brackets within character classes and the specific syntax used for scanning.

The sscanf function in PHP and the way you've attempted to replicate its behavior in JavaScript differ significantly because of the underlying mechanisms each language uses for pattern matching (PHP's string parsing vs. JavaScript's RegExp). In PHP, the format specifier %[^[] is interpreted as "match any character that is not an opening bracket," which is straightforward for PHP's parsing engine. JavaScript's RegExp engine, however, has its own rules and might not directly support such a syntax within the context of this sscanf implementation.

I don't think there is a trivial way to fix this one sadly