core utils updates
This commit is contained in:
@@ -1,3 +1,27 @@
|
||||
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 = []
|
||||
@@ -12,14 +36,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