unexpected behavior when reading a text file into array using javascript

Viewed 69

I am writing a emulator and I am trying to compare opcode logs from another working emulator. I have a log of executed opcodes ready to be compared it looks something like this

//log.txt (10000 lines long)
0
195
33
195
18...

I am reading this text file into a array using this code that looks kinda like this

//CPU.js
let log  = fs.readFileSync("./log.txt").toString().split("\n"); // read file
let logCount = 0;
function executeOP(){
let opcode = memoryRead(PC.inc()); // get opcode
  if(opcode != parseInt(log[logCount])){ // convert string to number and compare opcodes
    console.log("failed opcode " + log[logCount]); // print out failed opcode
    console.log("failed at line " + logCount); // print out failed opcode position
  }
  logCount++ // index of next opcode to compare
....
}

The problem is that parse in evaluates to NaN randomly on certain places in the test log array. It seems that extra invisible character example

console.log(log[2]) // 33
console.log(log[2][0]) // invisible
console.log(log[2].length) // 3? should be 2

I have tried correcting the array with (didn't work for me)

for(let i = 0; i < log.length; i++){
 log[i] = log[i].trim();
}
// also
for(let i = 0; i < log.length; i++){
 log[i] = log[i].replace(/(\r\n|\n|\r)/gm,"");
}

similar issue thread Loading text file in Javascript adds unexpected extra hidden characters

Thank you

2 Answers

I've tried simple solution on replicated textfile and I don't really see any issue.

const fs = reqiure('fs-extra')
const text = fs.readFileSync("./data.txt").toString().split("\r\n")

console.log(text[2][1]) // 3

Potential problem is with \n in your split function, in this code there is used \r\n to split these lines at glance, optionally you're able to use library dedicated for working on massive text-data, I've used that library for larger linear progression in node.js so maybe it will be useful for you, it's called skale.

turn out it was a few zero width spaces very frustrating but a simple fix

    for(let i = 0; i < log.length; i++){
        log[i] = log[i].replace(/\u200B/g,"");
    }
Related