Skip to content

Fixed parsing of floating point numbers. #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/UkooLabs.SVGSharpie/SvgColorTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static SvgColor FromSvgColorCode(string color)
byte ParsePercent(string str)
{
var trimmed = str.Trim().TrimEnd('%');
var percent = Math.Max(0, Math.Min(100, float.Parse(trimmed)));
var percent = Math.Max(0, Math.Min(100, float.Parse(trimmed, CultureInfo.InvariantCulture)));
var value = Math.Floor(255 * (percent / 100));
return (byte)value;
}
Expand Down
7 changes: 4 additions & 3 deletions src/UkooLabs.SVGSharpie/SvgElementStyleDataDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand All @@ -25,7 +26,7 @@ internal static class SvgElementStyleDataDeserializer
["stroke-width"] = CreateParserAndSetterForNullable(v => new SvgLength(v, SvgLengthContext.Null), s => s.StrokeWidth),
["stroke-linecap"] = CreateXmlEnumParserAndSetter(s => s.StrokeLineCap),
["stroke-linejoin"] = CreateXmlEnumParserAndSetter(s => s.StrokeLineJoin),
["stroke-miterlimit"] = CreateParserAndSetterForNullable(float.Parse, s => s.StrokeMiterLimit),
["stroke-miterlimit"] = CreateParserAndSetterForNullable(s => float.Parse(s, CultureInfo.InvariantCulture), s => s.StrokeMiterLimit),
["visibility"] = CreateXmlEnumParserAndSetter(s => s.Visibility),
["stroke-opacity"] = CreateParserAndSetterForInheritableNumber(s => s.StrokeOpacity),
["stroke-dasharray"] = CreateParserAndSetter(v =>
Expand Down Expand Up @@ -60,7 +61,7 @@ internal static class SvgElementStyleDataDeserializer
}
else
{
var parsed = Math.Max(0, Math.Min(1, float.Parse(v.Value)));
var parsed = Math.Max(0, Math.Min(1, float.Parse(v.Value, CultureInfo.InvariantCulture)));
value = new StyleProperty<float>(parsed, v.IsImportant);
}
s.FillOpacity = value;
Expand Down Expand Up @@ -141,7 +142,7 @@ void Result(SvgElementStyleData style, CssStylePropertyValue value)
}
else
{
var parsedValue = float.Parse(value.Value);
var parsedValue = float.Parse(value.Value, CultureInfo.InvariantCulture);
StyleProperty<float>? propertyValue = new StyleProperty<float>(parsedValue, isImportant: value.IsImportant);
propertyInfo.SetValue(style, propertyValue);
}
Expand Down
5 changes: 3 additions & 2 deletions src/UkooLabs.SVGSharpie/SvgLength.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;

namespace UkooLabs.SVGSharpie
{
Expand Down Expand Up @@ -56,11 +57,11 @@ internal SvgLength(string value, SvgLengthContext context)
if (unit < 0)
{
LengthType = SvgLengthType.Number;
ValueInSpecifiedUnits = float.Parse(value);
ValueInSpecifiedUnits = float.Parse(value, CultureInfo.InvariantCulture);
}
else
{
ValueInSpecifiedUnits = float.Parse(value.Substring(0, unit));
ValueInSpecifiedUnits = float.Parse(value.Substring(0, unit), CultureInfo.InvariantCulture);
switch (value.Substring(unit).ToLowerInvariant())
{
case "%":
Expand Down
4 changes: 3 additions & 1 deletion src/UkooLabs.SVGSharpie/SvgPathSegListParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Text;

namespace UkooLabs.SVGSharpie
Expand Down Expand Up @@ -53,7 +54,8 @@ public static SvgPathSegList Parse(string markup)
buffer.Append(stream.Read());
}

args[arg] = float.Parse(buffer.ToString());
args[arg] = float.Parse(buffer.ToString(), CultureInfo.InvariantCulture);

}

result.Add(CreatePathSegment(command, isNewCommand, args));
Expand Down
5 changes: 3 additions & 2 deletions src/UkooLabs.SVGSharpie/SvgPolyPointListParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;

namespace UkooLabs.SVGSharpie
{
Expand Down Expand Up @@ -55,8 +56,8 @@ void AddCoordsToResult()
throw new Exception($"Invalid coordinate '{point}'");
}

coords.Add(float.Parse(point.Substring(0, signIndex)));
coords.Add(float.Parse(point.Substring(signIndex + 1)));
coords.Add(float.Parse(point.Substring(0, signIndex), CultureInfo.InvariantCulture));
coords.Add(float.Parse(point.Substring(signIndex + 1), CultureInfo.InvariantCulture));
}

AddCoordsToResult();
Expand Down
9 changes: 5 additions & 4 deletions src/UkooLabs.SVGSharpie/SvgRect.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Globalization;

namespace UkooLabs.SVGSharpie
{
Expand Down Expand Up @@ -99,10 +100,10 @@ public static SvgRect Parse(string str)
}
return new SvgRect
(
float.Parse(values[0]),
float.Parse(values[1]),
float.Parse(values[2]),
float.Parse(values[3])
float.Parse(values[0], CultureInfo.InvariantCulture),
float.Parse(values[1], CultureInfo.InvariantCulture),
float.Parse(values[2], CultureInfo.InvariantCulture),
float.Parse(values[3], CultureInfo.InvariantCulture)
);
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/UkooLabs.SVGSharpie/SvgStopElement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Xml.Serialization;
using System.Globalization;
using System.Xml.Serialization;

namespace UkooLabs.SVGSharpie
{
Expand Down Expand Up @@ -86,10 +87,10 @@ protected override SvgElement CreateClone()

if (trimmed.EndsWith("%"))
{
return float.Parse(trimmed.Substring(0, trimmed.Length - 1)) / 100;
return float.Parse(trimmed.Substring(0, trimmed.Length - 1), CultureInfo.InvariantCulture) / 100;
}

return float.Parse(trimmed);
return float.Parse(trimmed, CultureInfo.InvariantCulture);
}
}
}
17 changes: 9 additions & 8 deletions src/UkooLabs.SVGSharpie/SvgTransformListParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

Expand Down Expand Up @@ -57,7 +58,7 @@ private static SvgTransform CreateMatrixTransformationFromArgs(string args)
{
throw new Exception($"Invalid matrix transformation arguments '{args}'");
}
var values = split.Select(float.Parse).ToArray();
var values = split.Select(x => float.Parse(x, CultureInfo.InvariantCulture)).ToArray();
return new SvgTransform(SvgTransformType.Matrix, new SvgMatrix(values));
}

Expand All @@ -69,8 +70,8 @@ private static SvgTransform CreateScaleTransformationFromArgs(string args)
{
throw new Exception($"Invalid scale transformation arguments '{args}'");
}
var x = float.Parse(split[0]);
var y = split.Length == 2 ? float.Parse(split[1]) : x;
var x = float.Parse(split[0], CultureInfo.InvariantCulture);
var y = split.Length == 2 ? float.Parse(split[1], CultureInfo.InvariantCulture) : x;
return new SvgTransform(SvgTransformType.Scale, SvgMatrix.CreateScale(x, y));
}

Expand All @@ -90,9 +91,9 @@ private static SvgTransform CreateRotateTransformationFromArgs(string args)
{
throw new Exception($"Invalid rotate transformation arguments '{args}'");
}
var angle = float.Parse(split[0]);
var cx = split.Length > 1 ? (float?)float.Parse(split[1]) : null;
var cy = split.Length > 1 ? (float?)float.Parse(split[2]) : null;
var angle = float.Parse(split[0], CultureInfo.InvariantCulture);
var cx = split.Length > 1 ? (float?)float.Parse(split[1], CultureInfo.InvariantCulture) : null;
var cy = split.Length > 1 ? (float?)float.Parse(split[2], CultureInfo.InvariantCulture) : null;
var matrix = SvgMatrix.CreateRotate(angle, cx, cy);
return new SvgTransform(SvgTransformType.Rotate, matrix, angle);
}
Expand All @@ -105,8 +106,8 @@ private static SvgTransform CreateTranslateTransformationFromArgs(string args)
{
throw new Exception($"Invalid translate transformation arguments '{args}'");
}
var tx = float.Parse(split[0]);
var ty = split.Length == 2 ? float.Parse(split[1]) : tx;
var tx = float.Parse(split[0], CultureInfo.InvariantCulture);
var ty = split.Length == 2 ? float.Parse(split[1], CultureInfo.InvariantCulture) : tx;
return new SvgTransform(SvgTransformType.Translate, SvgMatrix.CreateTranslate(tx, ty));
}

Expand Down