microsoft / Power-Fx

Power Fx low-code programming language
MIT License
3.21k stars 327 forks source link

Text function: Add consistent escaping to format string #1704

Closed gregli-msft closed 1 year ago

gregli-msft commented 1 year ago

Escaping in the Text format string is essential. As Excel and Power Fx pluck more characters for placeholders, it is critical that makers have a way to reliably insert the character that they want.

Excel handles escaping of placeholder characters in two ways:

image

Now the C# interpreter:

1> Text( 123.456, "mmm ddd yyy" )
"May Wed 1900"

2> Text( 123.456, """mmm"" ddd yyy" )
"mmm Wed 1900"

3> Text( 123.456, "\m\m\m ddd yyy" )
"\5\5\5 Wed 1900"

4> Text( 123.456, "'mmm' ddd yyy" )
"'May' Wed 1900"

5> Text( 123.456, "#.##" )
"123.46"

6> Text( 123.456, "#.""##""" )
"123##"

7> Text( 123.456, "#.\#\#" )
"123##"

8> Text( 123.456, "#.'##'" )
"123##"

9> Text( 123.456, "#.##'" )
"123.46"

Lines 3, 8, and 9 are incorrect, and lines 6-8 are missing the decimal point. Canvas gets many of these wrong in other interesting ways.

Tests for these scenarios can be found in Power-Fx/src/tests/Microsoft.PowerFx.Core.Tests/ExpressionTestCases/Text_ExcelCompat.txt at gregli/text-excel · microsoft/Power-Fx (github.com)

nguhoa commented 1 year ago

Fixed in https://github.com/microsoft/Power-Fx/pull/1716