Author Topic: Format option %s  (Read 4171 times)

Offline Andres.Pozo

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 927
Format option %s
« on: 2012 March 01 05:38:51 »
I think there is an error in the String.format() function. When I try to format an string using %s the result is gibberish.

For example:
Code: [Select]
console.writeln(format("test1: %s", "test1"));
var test2 = "test2";
console.writeln(format("test2: %s", test2));

The console should show:
Code: [Select]
test1: test1
test2: test2

However I get the result that can be seen in the attached screenshot.

The test has been done using PixInsight v01.07.05.0779 x64 and Win 7 x64 in spanish.

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Format option %s
« Reply #1 on: 2012 March 01 06:18:06 »
Hi Andrés,

Aha, you have discovered a hidden PixInsight feature. Now if someone comes and says that those strings have a coherent meaning in Chinese, I drop everything!  :D

Seriously now, this works correctly on both Linux and Mac OS X. So it must be a Windows issue (I don't have a Windows machine here to test right now). Probably one of the typical issues with Unicode on Windows (or another realization of MS's 'love' for open standards) which I've overlooked.

Anyway, this isn't a practical problem because there's no reason to use %s in a script. JavaScript has the string concatenation operator (+), so your code is better written as:

var test2 = "test2";
console.writeln( "test1: " + test2 );


However, you could try the following:

console.writeln(format("test1: %ls", "test1"));
var test2 = "test2";
console.writeln(format("test2: %ls", test2));


(note the %ls format specifiers) just to check if it's a UTF16 related issue.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Andres.Pozo

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 927
Re: Format option %s
« Reply #2 on: 2012 March 01 07:46:34 »
Anyway, this isn't a practical problem because there's no reason to use %s in a script. JavaScript has the string concatenation operator (+), so your code is better written as:

var test2 = "test2";
console.writeln( "test1: " + test2 );
I knew this possibility, but when formating a text mixing numbers and texts I prefer writing
   console.writeln( format("Field(%s): %.3f %s"), fieldname, value, unit) );
than
   console.writeln( "Field(" + fieldname + "): " + format("%.3f", value) + " " + unit);

Perhaps in this case there is not a big difference, but I am used to write multi-language applications and the first version is easier to localize. Anyway, it is only a matter of style.

Quote
However, you could try the following:

console.writeln(format("test1: %ls", "test1"));
var test2 = "test2";
console.writeln(format("test2: %ls", test2));


(note the %ls format specifiers) just to check if it's a UTF16 related issue.
It works in Win7 but I don't know if it is portable, and it should not be necessary to use the "l" option since in Javascript there is not any way to create ASCII or UNICODE strings.

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Format option %s
« Reply #3 on: 2012 March 01 08:26:24 »
Quote
console.writeln( format("Field(%s): %.3f %s"), fieldname, value, unit) );

I see your point. Of course %s should work on all platforms; I'll fix this bug in the next version.

Quote
It works in Win7 but I don't know if it is portable,

It is 100% portable and you can rely on it as a workaround to this bug, while we get it fixed. Sorry for the inconvenience.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline NKV

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 677
Re: Format option %s
« Reply #4 on: 2012 October 07 01:23:19 »
Juan, any news?
I see the String().Format("%s", var) problem in PCL too (Win7 PI 1.7.6.0793)

%ls don't work too.

Best regards,
Nikolay.
« Last Edit: 2012 October 07 01:38:58 by NKV »