0

I'm not an experienced JavaScript programmer. This is my first project using JS.

I'm developing on Linux and am at the point for my first release. Running the project on a Windows machine I came across the following ...

const os = require("os");
global.DBSysInfo;

console.log("DBSysInfo =\n" + DBSysInfo);
var pnt1 = DBSysInfo.indexOf('DBActive = "yes"');
console.log("pnt1 = " + pnt1);
var pnt2 = DBSysInfo.indexOf(os.EOL + os.EOL, pnt1) + 1;
console.log("pnt1 = " + pnt1 + "; pnt2 = " + pnt2);
console.log("DBSysInfo.substring =\n" + DBSysInfo.substring(pnt1, pnt2));

The above code works as expected on Linux:

DBSysInfo =
SysLocation = "C:\Users\mlake\MELGenKey"  
  
DBActive = "yes"  
DBName = "SOT2-KILLE20240704"  
DBUserID = "2.0"  
DBStatus = "1"  
DBSecurity = "0"  
DBLocation = "DBs/SOT2-KILLE20240704"  


pnt1 = 42  
pnt1 = 42; pnt2 = 176  
DBSysInfo.substring =  
DBActive = "yes"  
DBName = "SOT2-KILLE20240704"  
DBUserID = "2.0"  
DBStatus = "1"  
DBSecurity = "0"  
DBLocation = "DBs/SOT2-KILLE20240704"  

On Windows, the output is:

DBSysInfo =  
SysLocation = "C:\Users\mlake\MELGenKey"  
  
DBActive = "yes"  
DBName = "SOT2-KILLE20240704"  
DBUserID = "2.0"  
DBStatus = "1"  
DBSecurity = "0"  
DBLocation = "DBs/SOT2-KILLE20240704"  
  
  
pnt1 = 42  
pnt1 = 42; pnt2 = 0  
DBSysInfo.substring =  
SysLocation = "C:\Users\mlake\MELGenKey"  

I don't understand why the output would be different on Windows. Why would pnt2 be 0? The only thing I can think is the second "indexOf" statement is treating "DBSysInfo" as an array. If so, why? Is global treated differently on Windows than it is on Linux?

I tried using both Edge and Firefox on Windows with the same results, but the code is in node and I wouldn't think the browser used would make any difference in this instance.

5
  • 1
    linux and windows do have a different "EOL" convention ... windows \r\n ... linux \n - what is the respective value of os.EOL in your code for linux and windows - perhaps whatever DBSysInfo is always uses the linux "convention" regardless of the OS Commented Jul 10 at 1:45
  • 3
    indexOf is the same, but i think it's more likely your input is different. Many tools on windows write text files using different line endings for example
    – Evert
    Commented Jul 10 at 1:46
  • 1
    Oops yeah I agree with Jaramanda X.
    – Evert
    Commented Jul 10 at 1:46
  • 1
    e.g. in windows console.log("\n".length) is 1 but console.log(os.EOL.length) is 2 - so, using the literal \n is not the same as in some environments where \n can be taken to mean \r\n (I can't remember where that was, but I know it was many years ago that this tripped me up) Commented Jul 10 at 1:52
  • 1
    Note that global.DBSysInfo; is a property access that doesn’t do anything, even as an assertion (if global.DBSysInfo doesn’t exist, there won’t be an error). Not sure if that was the intent.
    – Ry-
    Commented Jul 10 at 2:06

3 Answers 3

2

Is string.indexOf() treated the same on Linux vs Windows?

Yes.

The only thing I can think is the second "indexOf" statement is treating "DBSysInfo" as an array.

That’s possible (not “treating���, but that DBSysInfo is somehow actually an array), but more likely is that the contents of the string aren’t what you expect, and that indexOf is returning −1 because the search pattern actually doesn’t exist in the string. In any case, you can confirm with either a debugger or print debugging:

console.log("%O", DBSysInfo);

The %O format specifier corresponds to inspect-type output that will quote strings, bracket arrays, and display non-printing characters as escapes.

Even on Windows, it’s common to see Unix line endings, so looking strictly for os.EOL is prone to error. A regular expression that allows flexible line endings is a decent alternative to plug in:

let pnt2 = DBSysInfo.indexOf(/(\r?\n){2}/m, pnt1);

Or, if you’re not writing this string back, consider normalizing it as it arrives with .replace(/\r\n/g, "\n"). Then you can search for "\n\n" directly and not have to also potentially worry about how many characters a newline is.

0

The issue is not related to the js method. you are getting different output because there is a difference between newline characters on Linux and Windows operating systems.

This is how you can solve it

console.log("DBSysInfo =\n" + DBSysInfo);

// This line replaces all occurrences of \r\n with \n, ensuring that the string has consistent newlines regardless of the operating system
const normalizedDBSysInfo = DBSysInfo.replace(/\r\n/g, '\n');

var pnt1 = normalizedDBSysInfo.indexOf('DBActive = "yes"');
console.log("pnt1 = " + pnt1);

//  To Find the next double newline sequence
var pnt2 = normalizedDBSysInfo.indexOf('\n\n', pnt1) + 1;
// For debugging only 
console.log("pnt1 = " + pnt1 + "; pnt2 = " + pnt2);

console.log("DBSysInfo.substring =\n" + normalizedDBSysInfo.substring(pnt1, pnt2));

Please let me know if this does not work properly. I have verified it, and it is working for me, but exceptions are always possible.

0

The answers and comments made me realize what was going on. (Kind of a "duh" moment for me!) The contents of "DBSysInfo" comes from reading a file that was created on Linux and just copied over to the Windows machine. The reverse could also happen as the project is used. Thanks, everyone, for your answers and comments.

3
  • Great. Can you please mark my answer if you think it solved your problem
    – abuzain
    Commented Jul 10 at 4:14
  • 1
    @abuzain, By "mark" do you mean to vote it up or accept it as the best answer? I think both answers solved my problem. Can I accept/mark both answers?
    – Marshall
    Commented Jul 10 at 4:22
  • I want you to accept, but it's okay if you think this isn't the best answer. 😢
    – abuzain
    Commented Jul 10 at 4:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.