Skip to content

Commit b8fc8f0

Browse files
committed
made all float.Parse/TryParse culture-agnostic
1 parent 2ba4653 commit b8fc8f0

1 file changed

Lines changed: 35 additions & 13 deletions

File tree

Runtime/Dom/DomStyle.cs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,6 @@ public object transition {
798798
set {
799799
if (value is string s) {
800800
var transitions = s.Split(',');
801-
802801
var properties = new List<StylePropertyName>();
803802
var durations = new List<TimeValue>();
804803
var easingFunctions = new List<EasingFunction>();
@@ -1280,23 +1279,23 @@ void SetBorderWidth(object value) {
12801279
}
12811280

12821281
if (value is string s) {
1283-
var parts = s.Replace("px", "").Split(new char[] { ' ', '\t', ',' }, StringSplitOptions.RemoveEmptyEntries);
1282+
var parts = s.Replace("px", "", StringComparison.OrdinalIgnoreCase).Split(new char[] { ' ', '\t', ',' }, StringSplitOptions.RemoveEmptyEntries);
12841283
if (parts.Length == 1) {
1285-
if (float.TryParse(parts[0], out var l)) {
1284+
if (TryParseFloat(parts[0], out var l)) {
12861285
__setBorderWidths(_dom, l, l, l, l);
12871286
}
12881287
} else if (parts.Length == 2) {
1289-
if (float.TryParse(parts[0], out var tb) && float.TryParse(parts[1], out var lr)) {
1288+
if (TryParseFloat(parts[0], out var tb) && TryParseFloat(parts[1], out var lr)) {
12901289
__setBorderWidths(_dom, tb, lr, tb, lr);
12911290
}
12921291
} else if (parts.Length == 3) {
1293-
if (float.TryParse(parts[0], out var t) && float.TryParse(parts[1], out var lr) &&
1294-
float.TryParse(parts[2], out var b)) {
1292+
if (TryParseFloat(parts[0], out var t) && TryParseFloat(parts[1], out var lr) &&
1293+
TryParseFloat(parts[2], out var b)) {
12951294
__setBorderWidths(_dom, t, lr, b, lr);
12961295
}
12971296
} else if (parts.Length == 4) {
1298-
if (float.TryParse(parts[0], out var t) && float.TryParse(parts[1], out var r) &&
1299-
float.TryParse(parts[2], out var b) && float.TryParse(parts[3], out var l)) {
1297+
if (TryParseFloat(parts[0], out var t) && TryParseFloat(parts[1], out var r) &&
1298+
TryParseFloat(parts[2], out var b) && TryParseFloat(parts[3], out var l)) {
13001299
__setBorderWidths(_dom, t, r, b, l);
13011300
}
13021301
}
@@ -1600,7 +1599,7 @@ bool TryParseStyleRotate(object value, out StyleRotate styleRotate) {
16001599
if (value is string s) {
16011600
var match = rotateRegex.Match(s);
16021601
if (match.Success) {
1603-
float f = float.Parse(match.Groups[1].Value);
1602+
float f = float.Parse(match.Groups[1].Value, NumberStyles.Float, CultureInfo.InvariantCulture);
16041603
var unit = match.Groups[2].Value.ToLower();
16051604
AngleUnit angleUnit = AngleUnit.Degree; // Default to Degree
16061605

@@ -1655,12 +1654,12 @@ bool TryParseStyleScale(object value, out StyleScale styleScale) {
16551654
if (value is string s) {
16561655
var parts = s.Split(new char[] { ' ', '\t', ',' }, StringSplitOptions.RemoveEmptyEntries);
16571656
if (parts.Length == 1) {
1658-
if (float.TryParse(parts[0], out var l)) {
1657+
if (TryParseFloat(parts[0], out var l)) {
16591658
styleScale = new Scale(new Vector2(l, l));
16601659
return true;
16611660
}
16621661
} else if (parts.Length == 2) {
1663-
if (float.TryParse(parts[0], out var x) && float.TryParse(parts[1], out var y)) {
1662+
if (TryParseFloat(parts[0], out var x) && TryParseFloat(parts[1], out var y)) {
16641663
styleScale = new Scale(new Vector2(x, y));
16651664
return true;
16661665
}
@@ -1792,7 +1791,7 @@ bool TryParseStyleListTimeValue(object value, out StyleList<TimeValue> styleList
17921791
foreach (var part in parts) {
17931792
var match = timeRegex.Match(part);
17941793
if (match.Success) {
1795-
float f = float.Parse(match.Groups[1].Value);
1794+
float f = float.Parse(match.Groups[1].Value, NumberStyles.Float, CultureInfo.InvariantCulture);
17961795
var unit = match.Groups[2].Value.ToLower();
17971796
TimeUnit timeUnit = TimeUnit.Second; // Default to Second
17981797

@@ -1818,7 +1817,7 @@ bool TryParseStyleListTimeValue(object value, out StyleList<TimeValue> styleList
18181817
for (int i = 0; i < jsObj.Get<int>("length"); i++) {
18191818
var match = timeRegex.Match(jsObj.Get<string>(i.ToString()));
18201819
if (match.Success) {
1821-
float f = float.Parse(match.Groups[1].Value);
1820+
float f = float.Parse(match.Groups[1].Value, NumberStyles.Float, CultureInfo.InvariantCulture);
18221821
var unit = match.Groups[2].Value.ToLower();
18231822
TimeUnit timeUnit = TimeUnit.Second; // Default to Second
18241823

@@ -2115,6 +2114,29 @@ static bool TryNum(ReadOnlySpan<char> span, out float v) {
21152114
public static bool TryParseColorString(string s, out Color color) {
21162115
return ColorUtility.TryParseHtmlString(s, out color);
21172116
}
2117+
2118+
public static bool TryParseFloat(string token, out float value) {
2119+
token = token.Trim();
2120+
2121+
// Primary: CSS-style invariant (dot as decimal point)
2122+
if (float.TryParse(token, NumberStyles.Float, CultureInfo.InvariantCulture, out value))
2123+
return true;
2124+
2125+
// Fallback: user's current culture (e.g., comma decimals)
2126+
if (float.TryParse(token, NumberStyles.Float, CultureInfo.CurrentCulture, out value))
2127+
return true;
2128+
2129+
// Extra tolerant path: if it looks like a comma-decimal, normalize to dot and retry invariant
2130+
var nfi = CultureInfo.CurrentCulture.NumberFormat;
2131+
if (token.IndexOf(',') >= 0 && token.IndexOf('.') < 0 && nfi.NumberDecimalSeparator == ",") {
2132+
var normalized = token.Replace(',', '.');
2133+
if (float.TryParse(normalized, NumberStyles.Float, CultureInfo.InvariantCulture, out value))
2134+
return true;
2135+
}
2136+
2137+
value = 0f;
2138+
return false;
2139+
}
21182140

21192141
static void __setBorderColors(Dom dom, Color t, Color r, Color b, Color l) {
21202142
dom.ve.style.borderTopColor = new StyleColor(t);

0 commit comments

Comments
 (0)