Skip to content

Commit

Permalink
adding f32, f64 formats
Browse files Browse the repository at this point in the history
  • Loading branch information
drom committed Feb 8, 2024
1 parent 5c4e3ef commit 754b7a7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 33 deletions.
9 changes: 1 addition & 8 deletions format.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,10 @@ base ::= "b" | "o" | "d" | "h"

```
b base 2
B base 2, leading zeros
o base 8
O base 8, leading zeros
d base 10
h base 16, lower-case letters a-f
H base 16, lower-case letters A-F
c ASCII char string
A ASCII char string
```

### zero / sign extension
Expand Down
70 changes: 48 additions & 22 deletions lib/format.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,56 @@
'use strict';

const format = (fmt, val, len) => {
const m1 = fmt.match(/^%(?<sign>[s])?(?<radix>[bodhH])$/);
len = BigInt(len);
if (m1) {
const p = m1.groups;
const radix = ({b: 2, o: 8, d: 10, h: 16, H: 16})[p.radix];
if (p.sign === 's') {
const sign = (val >> (len - 1n)) & 1n;
if (sign) {
val = val - (2n ** len);
}
const format = (fmt, len) => {
{
const m1 = fmt.match(/^%(?<sign>[s])?(?<radix>[bodh])$/);
len = BigInt(len);
if (m1) {
const p = m1.groups;
const radix = ({b: 2, o: 8, d: 10, h: 16, H: 16})[p.radix];
return (val) => {
if (p.sign === 's') {
const sign = (val >> (len - 1n)) & 1n;
if (sign) {
val = val - (2n ** len);
}
}
let res = val.toString(radix);
return res;
};
}
let res = val.toString(radix);
if (p.radix === 'H') {
return res.toUpperCase();
} {
const m2 = fmt.match(/^%A$/);
if (m2) {
return (val) => {
let txtRes = '';
for (let i = 0n; i < len; i += 8n) {
txtRes = String.fromCharCode(Number((val >> i) & 0xffn)) + txtRes;
}
return txtRes;
};
}
return res;
}
const m2 = fmt.match(/^%A$/);
if (m2) {
let txtRes = '';
for (let i = 0n; i < len; i += 8n) {
txtRes = String.fromCharCode(Number((val >> i) & 0xffn)) + txtRes;
} {
const m3 = fmt.match(/^%f64$/);
if (m3) {
return (val) => {
const buf = new ArrayBuffer(8);
const bufFloat = new Float64Array(buf);
const bufBInt = new BigInt64Array(buf);
bufBInt[0] = val & 0xffffffffffffffffn;
return bufFloat[0].toString();
};
}
} {
const m3 = fmt.match(/^%f32$/);
if (m3) {
return (val) => {
const buf = new ArrayBuffer(4);
const bufFloat = new Float32Array(buf);
const bufUInt = new Uint32Array(buf);
bufUInt[0] = Number(val & 0xffffffffn);
return bufFloat[0].toString();
};
}
return txtRes;
}
};

Expand Down
29 changes: 26 additions & 3 deletions test/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,30 @@
const {expect} = require('chai');
const format = require('../lib/format.js');

const fp64_2bigint = (val) => {

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Identifier 'fp64_2bigint' is not in camel case

Check warning on line 6 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Identifier 'fp64_2bigint' is not in camel case
const buf = new ArrayBuffer(8);
const bufFloat = new Float64Array(buf);
const bufBInt = new BigInt64Array(buf);
bufFloat[0] = val;
return bufBInt[0];
};

const fp32_2bigint = (val) => {

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Identifier 'fp32_2bigint' is not in camel case

Check warning on line 14 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Identifier 'fp32_2bigint' is not in camel case
const buf = new ArrayBuffer(4);
const bufFloat = new Float32Array(buf);
const bufUInt = new Uint32Array(buf);
bufFloat[0] = val;
return bufUInt[0];
};

console.log(fp64_2bigint(123.456).toString(16));

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Unexpected console statement

Check warning on line 22 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Unexpected console statement
console.log(fp32_2bigint(123.456).toString(16));

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Unexpected console statement

Check warning on line 23 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Unexpected console statement

const tt = [
{fmt: '%b', val: 12345678n, len: 24, res: '101111000110000101001110'},
{fmt: '%o', val: 12345678n, len: 24, res: '57060516'},
{fmt: '%d', val: 12345678n, len: 24, res: '12345678'},
{fmt: '%h', val: 12345678n, len: 24, res: 'bc614e'},
{fmt: '%H', val: 12345678n, len: 24, res: 'BC614E'},

{fmt: '%sb', val: 5n, len: 3, res: '-11'},
{fmt: '%so', val: 5n, len: 3, res: '-3'},
Expand All @@ -19,20 +37,25 @@ const tt = [
{fmt: '%so', val: 12345678n, len: 24, res: '-20717262'},
{fmt: '%sd', val: 12345678n, len: 24, res: '-4431538'},
{fmt: '%sh', val: 12345678n, len: 24, res: '-439eb2'},
{fmt: '%sH', val: 12345678n, len: 24, res: '-439EB2'},

{fmt: '%sb', val: 12345678n, len: 42, res: '101111000110000101001110'},
{fmt: '%so', val: 12345678n, len: 42, res: '57060516'},
{fmt: '%sd', val: 12345678n, len: 42, res: '12345678'},
{fmt: '%sh', val: 12345678n, len: 42, res: 'bc614e'},

{fmt: '%A', val: 0x4142434445n, len: 40, res: 'ABCDE'},

{fmt: '%sh', val: 12345678n, len: 42, res: 'bc614e'},

{fmt: '%f64', val: 0x405edd2f1a9fbe77n, len: 64, res: '123.456'},
{fmt: '%f32', val: 0x42f6e979n, len: 32, res: '123.45600128173828'},

];

describe('format', () => {
for (const te of tt) {
it('t:' + te.fmt + ':' + te.val + ':' + te.len, async () => {
expect(format(te.fmt, te.val, te.len)).to.eq(te.res);
expect(format(te.fmt, te.len)(te.val)).to.eq(te.res);
});
}
});
Expand Down

0 comments on commit 754b7a7

Please sign in to comment.