Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 64bit integer to hex formatting #233

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/sprintf.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
}

function sprintf_format(parse_tree, argv) {
var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign
const MAXINT = 0x80000000
var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign, low, high
for (i = 0; i < tree_length; i++) {
if (typeof parse_tree[i] === 'string') {
output += parse_tree[i]
Expand Down Expand Up @@ -112,10 +113,14 @@
arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
break
case 'x':
arg = (parseInt(arg, 10) >>> 0).toString(16)
break
case 'X':
arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase()
high = "0"
if (parseInt(arg, 10) > MAXINT - 1 || parseInt(arg, 10) < -MAXINT) {
high = BigInt.asUintN(32, BigInt(arg) >> BigInt(32)).toString(16) // eslint-disable-line
Copy link
Author

@lvcabral lvcabral Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to disable lint in this line because the eslint version on this repo does not recognize BigInt, but it is available in all browsers and platforms already: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

}
low = (parseInt(arg, 10) >>> 0).toString(16)
arg = parseInt(high, 16) !== 0 ? high + low.padStart(8, '0') : low
arg = ph.type === 'X' ? arg.toUpperCase() : arg
break
}
if (re.json.test(ph.type)) {
Expand Down
7 changes: 4 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var assert = require('assert'),
describe('sprintfjs', function() {
var pi = 3.141592653589793

it('should return formated strings for simple placeholders', function() {
it('should return formatted strings for simple placeholders', function() {
assert.equal('%', sprintf('%%'))
assert.equal('10', sprintf('%b', 2))
assert.equal('A', sprintf('%c', 65))
Expand All @@ -31,6 +31,7 @@ describe('sprintfjs', function() {
assert.equal('ffffff01', sprintf('%x', -255))
assert.equal('FF', sprintf('%X', 255))
assert.equal('FFFFFF01', sprintf('%X', -255))
assert.equal('2308249009', sprintf('%X', 150460469257))
assert.equal('Polly wants a cracker', sprintf('%2$s %3$s a %1$s', 'cracker', 'Polly', 'wants'))
assert.equal('Hello world!', sprintf('Hello %(who)s!', {who: 'world'}))
assert.equal('true', sprintf('%t', true))
Expand Down Expand Up @@ -60,7 +61,7 @@ describe('sprintfjs', function() {
assert.equal('/<("[^"]*"|\'[^\']*\'|[^\'">])*>/', sprintf('%v', /<("[^"]*"|'[^']*'|[^'">])*>/))
})

it('should return formated strings for complex placeholders', function() {
it('should return formatted strings for complex placeholders', function() {
// sign
assert.equal('2', sprintf('%d', 2))
assert.equal('-2', sprintf('%d', -2))
Expand Down Expand Up @@ -106,7 +107,7 @@ describe('sprintfjs', function() {

})

it('should return formated strings for callbacks', function() {
it('should return formatted strings for callbacks', function() {
assert.equal('foobar', sprintf('%s', function() { return 'foobar' }))
})
})