core utils updates
This commit is contained in:
@@ -60,6 +60,57 @@ function barePosixBlocks(size) {
|
||||
return Math.ceil(Number(size) / 512) || 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Raw stdout for NUL/binary when **`process.stdout.write`** is missing.
|
||||
* If **`ctx.bareOsBinWrite(Uint8Array|string)`** is set (tests / host), use it.
|
||||
* @param {Record<string, unknown>} ctx
|
||||
* @param {string | Uint8Array} chunk
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function bareOsEmitRaw(ctx, chunk) {
|
||||
if (typeof ctx.bareOsBinWrite === 'function') {
|
||||
const b4 = ctx.b4a
|
||||
const u8 =
|
||||
typeof chunk === 'string'
|
||||
? b4 && typeof b4.from === 'function'
|
||||
? b4.from(chunk)
|
||||
: new TextEncoder().encode(chunk)
|
||||
: chunk
|
||||
ctx.bareOsBinWrite(u8 instanceof Uint8Array ? u8 : new Uint8Array(u8))
|
||||
return true
|
||||
}
|
||||
const w = globalThis.process?.stdout?.write
|
||||
if (typeof w === 'function') {
|
||||
w.call(globalThis.process.stdout, chunk)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/** @param {string} s */
|
||||
function barePrintfBackslashArg(s) {
|
||||
let o = ''
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
if (s[i] !== '\\') {
|
||||
o += s[i]
|
||||
continue
|
||||
}
|
||||
const c = s[++i]
|
||||
if (c === undefined) break
|
||||
if (c === 'n') o += '\n'
|
||||
else if (c === 't') o += '\t'
|
||||
else if (c === 'r') o += '\r'
|
||||
else if (c === '\\') o += '\\'
|
||||
else if (c >= '0' && c <= '7') {
|
||||
let oct = c
|
||||
while (i + 1 < s.length && /[0-7]/.test(s[i + 1]) && oct.length < 3)
|
||||
oct += s[++i]
|
||||
o += String.fromCharCode(Number.parseInt(oct, 8) & 0xff)
|
||||
} else o += c
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
function barePrintfFormat(fmt, args) {
|
||||
let ai = 0
|
||||
let o = ''
|
||||
@@ -86,6 +137,10 @@ function barePrintfFormat(fmt, args) {
|
||||
else if (spec === 'o') o += (Math.trunc(Number(arg)) >>> 0).toString(8)
|
||||
else if (spec === 'c') o += String.fromCharCode(Number(arg) || 0)
|
||||
else if (spec === 'f') o += String(Number(arg))
|
||||
else if (spec === 'e') o += Number(arg).toExponential(6)
|
||||
else if (spec === 'E')
|
||||
o += Number(arg).toExponential(6).replace(/e/g, 'E')
|
||||
else if (spec === 'b') o += barePrintfBackslashArg(String(arg))
|
||||
else o += String(arg)
|
||||
i = j
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user