Skip to content

Commit e0a6822

Browse files
Fix extra arg consumption by %% (Fixes locutusjs#441)
* Fix extra arg consumption by %% (Fixes locutusjs#441) * Add fixed example call
1 parent c176d5a commit e0a6822

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/c/stdio/sprintf.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ function pad (str, minLength, padChar, leftJustify) {
77

88
module.exports = function sprintf (format, ...args) {
99
// original by: Rafał Kukawski
10+
// bugfixed by: Param Siddharth
1011
// example 1: sprintf('%+10.*d', 5, 1)
1112
// returns 1: ' +00001'
13+
// example 2: sprintf('%s is a %d%% %s %s.', 'Param', 90, 'good', 'boy')
14+
// returns 2: 'Param is a 90% good boy.'
1215
const placeholderRegex = /%(?:(\d+)\$)?([-+#0 ]*)(\*|\d+)?(?:\.(\*|\d*))?([\s\S])/g
1316

1417
let index = 0
@@ -35,7 +38,11 @@ module.exports = function sprintf (format, ...args) {
3538

3639
// compiling with default clang params, mixed positional and non-positional params
3740
// give only a warning
38-
const arg = param ? args[param - 1] : args[index++]
41+
const arg = param ? args[param - 1] : args[index]
42+
43+
if (modifier !== '%') {
44+
index++
45+
}
3946

4047
if (precision === undefined || isNaN(precision)) {
4148
precision = 'eEfFgG'.includes(modifier) ? 6 : (modifier === 's' ? String(arg).length : undefined)

0 commit comments

Comments
 (0)