encoding-fixer/index.ts

28 lines
745 B
TypeScript

import { Glob } from 'bun';
// adjust path to the desired directory
const glob = new Glob('../../src/**/*.cpp');
for await (const fileName of glob.scan('.')) {
const file = Bun.file(fileName);
const content = await file.arrayBuffer();
const bytes = new Uint8Array(content);
const newFileContent = [];
let isString = false;
for (const byte of bytes) {
if (byte === 0x22) {
isString = !isString;
}
if (byte > 127 && isString) {
const stringifiedByte = `\\x${byte
.toString(16)
.padStart(2, '0')
.toUpperCase()}`;
newFileContent.push(...Buffer.from(stringifiedByte));
} else {
newFileContent.push(byte);
}
}
await Bun.write(file, Buffer.from(newFileContent));
}