user943702
user943702

Reputation: 954

non-ascii char as arguments

printargv.js:

console.log(Buffer.byteLength(process.argv[2]));

In cmd.exe (with chcp=65001,font='Lucida Console'), I ran:

node printargv.js Ā 

(Note: unicode code point of Ā is U+0100.) The script outputted:

1

I expected the script to print a number greater than 1 but it doesn't. Does anyone know why?

edit: i think that node 'parses' initial arguments incorrectly for cmd.exe after i tried the below code:

var i = require('readline').createInterface(process.stdin,process.stdout);

i.question('char: ', function(c){
  console.log( Buffer.byteLength(c) );
  i.close();
  process.stdin.destroy();
});

the output is 2

Upvotes: 0

Views: 476

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074385

Your program is not receiving the Ā, it's receiving an A instead. I used this program to test:

var n;
for (n = 0; n < process.argv.length; ++n) {
    console.log(n + ": '" + process.argv[n] + "'");
}
console.log("length: " + process.argv[2].length);
console.log("code: " + process.argv[2].charCodeAt(0));
console.log("size: " + Buffer.byteLength(process.argv[2]));

On Ubuntu using UTF-8 in the console, I got:

$ node test.js Ā
0: 'node'
1: '/home/tjc/temp/test.js'
2: 'Ā'
length: 1
code: 256
size: 2

...which is correct.

On Windows 7 using chcp 65001 and Lucida Console, I got:

C:\tmp>node temp.js Ā
0: 'node'
1: 'C:\tmp\temp.js'
2: 'A'
length: 1
code: 65
size: 1

Note that the Ā became an A at some point along the way.

As I said in my comment on the question, I can only assume there's some issue with Lucida Console, or cmd.exe's handling of UTF-8, or perhaps node.exe's handling of Unicode from the console on Windows (I used the pre-built 0.5.7 version).


Update: This might be something to take up with the NodeJS folks, since Windows appears to get it right on its own. If I put this code in a test.vbs file:

WScript.Echo WScript.Arguments(0)
WScript.Echo AscW(WScript.Arguments(0))

I get a correct result:

C:\tmp>cscript /nologo test.vbs Ā
Ā
256

...suggesting that the terminal is passing the argument correctly to the program. So it could be an issue with the Windows node.exe build.

Upvotes: 1

Related Questions