bruce-dunwiddie / tsql-parser

Library Written in C# For Parsing SQL Server T-SQL Scripts in .Net
Apache License 2.0
324 stars 56 forks source link

Issue with localization while parsing System.Double.Parse in TSQLNumericLiteral #72

Closed lemish closed 3 years ago

lemish commented 3 years ago

On machine with non en-US culture (such as ru-RU or uk-UA), System.Double.Parse() throws the FormatException if argument is number with point (like "12.34"). That can be easy fixed with adding third argument to method System.Double.Parse().

Value = Double.Parse(Text, NumberStyles.Any);

change to

Value = Double.Parse(Text, NumberStyles.Any, new CultureInfo("en-US"));

https://github.com/bruce-dunwiddie/tsql-parser/blob/cdbb2c6344611e1dfc362c7ec947dceb9bec659a/TSQL_Parser/TSQL_Parser/Tokens/TSQLNumericLiteral.cs#L19

bruce-dunwiddie commented 3 years ago

Thank you for reporting this. Culture based parsing is one of the harder things to notice and troubleshoot.

Can you tell me how T-SQL normally handles numbers in your region? I agree that your submitted change will force always parsing with the assumption of US formatting, regardless of local settings, but then it wouldn't handle parsing 12,34. Is that a valid number in T-SQL, or does T-SQL only accept US format?

I'm pondering Invariant Culture, or allowing the caller to override the US setting if needed.

bruce-dunwiddie commented 3 years ago

https://www.nuget.org/packages/TSQL.Parser/1.5.3