Skip to content

Commit

Permalink
Fix all invariant float and double tryparse (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn authored Jul 23, 2024
1 parent 7f9a445 commit 7c41ccb
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion OneMore/Commands/Edit/PasteRtfCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ private string ConvertSize(string size, string units = null)

for (int i = 0; i < parts.Length; i++)
{
if (double.TryParse(parts[i], out var value))
if (double.TryParse(parts[i], NumberStyles.Any, CultureInfo.InvariantCulture, out var value))
{
parts[i] = Math.Ceiling(value * DeltaSize).ToString(CultureInfo.InvariantCulture);
}
Expand Down
3 changes: 2 additions & 1 deletion OneMore/Commands/Page/FitGridToTextCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace River.OneMoreAddIn.Commands
{
using River.OneMoreAddIn.Styles;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
Expand Down Expand Up @@ -109,7 +110,7 @@ private double CalculateLineHeight(StyleBase style)
using var image = new Bitmap(1, 1);
using var g = Graphics.FromImage(image);

var fontSize = float.Parse(style.FontSize);
var fontSize = float.Parse(style.FontSize, NumberStyles.Any, CultureInfo.InvariantCulture);
using var font = new Font(style.FontFamily, fontSize, FontStyle.Regular);

// the height of a single line is apparently greater than
Expand Down
5 changes: 4 additions & 1 deletion OneMore/Commands/Snippets/InsertTocCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace River.OneMoreAddIn.Commands
using River.OneMoreAddIn.Styles;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand Down Expand Up @@ -216,7 +217,9 @@ private async Task InsertTableOfContents(
var watt = container.Ancestors(ns + "Outline")
.Elements(ns + "Size").Attributes("width").FirstOrDefault();

var colwid = watt is not null && float.TryParse(watt.Value, out float width)
var colwid = watt is not null && float.TryParse(
watt.Value, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture,
out float width)
? (float)Math.Round(Math.Max(width, MinToCWidth + 40) - 40, 2)
: MinToCWidth;

Expand Down
3 changes: 2 additions & 1 deletion OneMore/Commands/Styles/ApplyStylesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace River.OneMoreAddIn.Commands
using River.OneMoreAddIn.Models;
using River.OneMoreAddIn.Styles;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand Down Expand Up @@ -116,7 +117,7 @@ private bool ApplyStyles(List<Style> styles)
string spacing = null;
if (FindStyle(styles, "p") is Style normal)
{
if (double.TryParse(normal.Spacing, out var spc) && spc > 0.0)
if (double.TryParse(normal.Spacing, NumberStyles.Any, CultureInfo.InvariantCulture, out var spc) && spc > 0.0)
{
spacing = normal.Spacing;
}
Expand Down
2 changes: 1 addition & 1 deletion OneMore/Commands/Tables/Formulas/Calculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ private static FormulaValue ExecuteTokens(List<string> tokens)
foreach (string token in tokens)
{
// TryParse is more performance and complete than regex
if (double.TryParse(token, out var d))
if (double.TryParse(token, out var d)) // Culture-specific user input?!
{
stack.Push(new FormulaValue(d));
}
Expand Down
2 changes: 1 addition & 1 deletion OneMore/Commands/Tables/Formulas/FormulaValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public double[] ToDoubleArray()
// empty cells are not included in the array
else if (v.Value is string s && !string.IsNullOrWhiteSpace(s))
{
if (double.TryParse(s, out var ds))
if (double.TryParse(s, out var ds)) // Culture-specific user input?!
{
list.Add(ds);
}
Expand Down
2 changes: 1 addition & 1 deletion OneMore/Commands/Tables/Formulas/MathFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private static double CountIf(FormulaValues p)
}

FormulaValue expected;
if (double.TryParse(s, out var d))
if (double.TryParse(s, out var d)) // Culture-specific user input?!
{
expected = new FormulaValue(d);
}
Expand Down
2 changes: 1 addition & 1 deletion OneMore/Commands/Tables/Formulas/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void ResolveCellReference(object sender, SymbolEventArgs e)
.Replace(AddIn.Culture.NumberFormat.PercentSymbol, string.Empty);

// common case is double
if (double.TryParse(text, out var dvalue))
if (double.TryParse(text, out var dvalue)) // Culture-specific user input?!
{
maxdec = Math.Max(dvalue.ToString().Length - ((int)dvalue).ToString().Length - 1, maxdec);

Expand Down

0 comments on commit 7c41ccb

Please sign in to comment.