core utils updates
This commit is contained in:
+54
-3
@@ -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
|
||||
}
|
||||
|
||||
const TR_CLASS = {
|
||||
'[:alnum:]':
|
||||
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
|
||||
'[:alpha:]': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
||||
'[:blank:]': ' \t',
|
||||
'[:cntrl:]': [...Array(32).keys()].map((i) => String.fromCharCode(i)).join('') + '\x7f',
|
||||
'[:digit:]': '0123456789',
|
||||
'[:lower:]': 'abcdefghijklmnopqrstuvwxyz',
|
||||
'[:print:]': [...Array(95).keys()].map((i) => String.fromCharCode(i + 32)).join(''),
|
||||
'[:punct:]': '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~',
|
||||
'[:space:]': ' \t\n\r\v\f',
|
||||
'[:upper:]': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
||||
'[:xdigit:]': '0123456789abcdefABCDEF'
|
||||
}
|
||||
|
||||
/** @param {string} s */
|
||||
function trExpandClasses(s) {
|
||||
let out = s
|
||||
for (const [name, chars] of Object.entries(TR_CLASS)) {
|
||||
out = out.split(name).join(chars)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
async function run(ctx, argv) {
|
||||
let del = false
|
||||
const sets = []
|
||||
@@ -74,14 +125,14 @@ async function run(ctx, argv) {
|
||||
}
|
||||
const s = bareStdin(ctx)
|
||||
if (del) {
|
||||
const kill = new Set(sets[0].split(''))
|
||||
const kill = new Set(trExpandClasses(sets[0]).split(''))
|
||||
let o = ''
|
||||
for (const ch of s) if (!kill.has(ch)) o += ch
|
||||
ctx.console.log(o)
|
||||
return
|
||||
}
|
||||
const from = sets[0]
|
||||
const to = sets[1]
|
||||
const from = trExpandClasses(sets[0])
|
||||
const to = trExpandClasses(sets[1])
|
||||
const map = Object.create(null)
|
||||
const n = Math.max(from.length, to.length)
|
||||
for (let i = 0; i < n; i++) {
|
||||
|
||||
Reference in New Issue
Block a user