Skip to content

Commit 6bd05bf

Browse files
committed
♻️Use GetCultureInfo instead of creating a new instance each time
Saves some unnecessary allocations.
1 parent d59bfe6 commit 6bd05bf

File tree

130 files changed

+1050
-1048
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+1050
-1048
lines changed

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ private void GenerateStaticParseMethods()
435435
/// </summary>
436436
/// <param name=""str"">String to parse. Typically in the form: {{number}} {{unit}}</param>
437437
/// <example>
438-
/// Length.Parse(""5.5 m"", new CultureInfo(""en-US""));
438+
/// Length.Parse(""5.5 m"", CultureInfo.GetCultureInfo(""en-US""));
439439
/// </example>
440440
/// <exception cref=""ArgumentNullException"">The value of 'str' cannot be null. </exception>
441441
/// <exception cref=""ArgumentException"">
@@ -462,7 +462,7 @@ private void GenerateStaticParseMethods()
462462
/// </summary>
463463
/// <param name=""str"">String to parse. Typically in the form: {{number}} {{unit}}</param>
464464
/// <example>
465-
/// Length.Parse(""5.5 m"", new CultureInfo(""en-US""));
465+
/// Length.Parse(""5.5 m"", CultureInfo.GetCultureInfo(""en-US""));
466466
/// </example>
467467
/// <exception cref=""ArgumentNullException"">The value of 'str' cannot be null. </exception>
468468
/// <exception cref=""ArgumentException"">
@@ -494,7 +494,7 @@ private void GenerateStaticParseMethods()
494494
/// <param name=""str"">String to parse. Typically in the form: {{number}} {{unit}}</param>
495495
/// <param name=""result"">Resulting unit quantity if successful.</param>
496496
/// <example>
497-
/// Length.Parse(""5.5 m"", new CultureInfo(""en-US""));
497+
/// Length.Parse(""5.5 m"", CultureInfo.GetCultureInfo(""en-US""));
498498
/// </example>
499499
public static bool TryParse(string? str, out {_quantity.Name} result)
500500
{{
@@ -508,7 +508,7 @@ public static bool TryParse(string? str, out {_quantity.Name} result)
508508
/// <param name=""result"">Resulting unit quantity if successful.</param>
509509
/// <returns>True if successful, otherwise false.</returns>
510510
/// <example>
511-
/// Length.Parse(""5.5 m"", new CultureInfo(""en-US""));
511+
/// Length.Parse(""5.5 m"", CultureInfo.GetCultureInfo(""en-US""));
512512
/// </example>
513513
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
514514
public static bool TryParse(string? str, IFormatProvider? provider, out {_quantity.Name} result)
@@ -525,7 +525,7 @@ public static bool TryParse(string? str, IFormatProvider? provider, out {_quanti
525525
/// </summary>
526526
/// <param name=""str"">String to parse. Typically in the form: {{number}} {{unit}}</param>
527527
/// <example>
528-
/// Length.ParseUnit(""m"", new CultureInfo(""en-US""));
528+
/// Length.ParseUnit(""m"", CultureInfo.GetCultureInfo(""en-US""));
529529
/// </example>
530530
/// <exception cref=""ArgumentNullException"">The value of 'str' cannot be null. </exception>
531531
/// <exception cref=""UnitsNetException"">Error parsing string.</exception>
@@ -540,7 +540,7 @@ public static bool TryParse(string? str, IFormatProvider? provider, out {_quanti
540540
/// <param name=""str"">String to parse. Typically in the form: {{number}} {{unit}}</param>
541541
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
542542
/// <example>
543-
/// Length.ParseUnit(""m"", new CultureInfo(""en-US""));
543+
/// Length.ParseUnit(""m"", CultureInfo.GetCultureInfo(""en-US""));
544544
/// </example>
545545
/// <exception cref=""ArgumentNullException"">The value of 'str' cannot be null. </exception>
546546
/// <exception cref=""UnitsNetException"">Error parsing string.</exception>
@@ -562,7 +562,7 @@ public static bool TryParseUnit(string str, out {_unitEnumName} unit)
562562
/// <param name=""unit"">The parsed unit if successful.</param>
563563
/// <returns>True if successful, otherwise false.</returns>
564564
/// <example>
565-
/// Length.TryParseUnit(""m"", new CultureInfo(""en-US""));
565+
/// Length.TryParseUnit(""m"", CultureInfo.GetCultureInfo(""en-US""));
566566
/// </example>
567567
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
568568
public static bool TryParseUnit(string str, IFormatProvider? provider, out {_unitEnumName} unit)

UnitsNet.Tests/CustomCode/DurationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public void DurationTimesVolumeFlowEqualsVolume()
192192
[InlineData("1000 мсек", 1, "ru-RU")]
193193
public void DurationFromStringUsingMultipleAbbreviationsParsedCorrectly(string textValue, double expectedSeconds, string? culture = null)
194194
{
195-
var cultureInfo = culture == null ? CultureInfo.InvariantCulture : new CultureInfo(culture);
195+
var cultureInfo = culture == null ? CultureInfo.InvariantCulture : CultureInfo.GetCultureInfo(culture);
196196

197197
AssertEx.EqualTolerance(expectedSeconds, Duration.Parse(textValue, cultureInfo).Seconds, SecondsTolerance);
198198
}

UnitsNet.Tests/CustomCode/LengthTests.FeetInches.cs

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace UnitsNet.Tests
99
{
1010
public class FeetInchesTests
1111
{
12+
private static readonly CultureInfo EnglishUs = new("en-US", useUserOverride: false);
13+
private static readonly CultureInfo GermanSwitzerland = new("de-CH", useUserOverride: false);
1214
private const double FeetInOneMeter = 3.28084;
1315
private const double InchesInOneMeter = 39.37007874;
1416
private const double FeetTolerance = 1e-5;
@@ -33,42 +35,42 @@ public void FeetInchesRoundTrip()
3335

3436
public static IEnumerable<object[]> ValidData => new List<object[]>
3537
{
36-
new object[]{"1'", 1, new CultureInfo("en-US", false)}, // Feet only
37-
new object[]{"1′", 1, new CultureInfo("en-US", false)}, // Feet only
38-
new object[]{"1,000′", 1000, new CultureInfo("en-US", false)}, // Feet only, with seperator
39-
new object[]{"1e3'", 1000, new CultureInfo("en-US", false)}, // Feet only, exponential notation
40-
new object[]{"1\"", 0.08333333, new CultureInfo("en-US", false)}, // Inches only
41-
new object[]{"1″", 0.08333333, new CultureInfo("en-US", false)}, // Inches only
42-
new object[]{"0' 1\"", 0.08333333, new CultureInfo("en-US", false)}, // Inches only
43-
new object[]{"0' 1″", 0.08333333, new CultureInfo("en-US", false)}, // Inches only
44-
new object[]{"0′ 1\"", 0.08333333, new CultureInfo("en-US", false)}, // Inches only
45-
new object[]{"0′ 1″", 0.08333333, new CultureInfo("en-US", false)}, // Inches only
46-
new object[]{"1' 1\"", 1.08333333, new CultureInfo("en-US", false)}, // Normal form
47-
new object[]{"1′ 1″", 1.08333333, new CultureInfo("en-US", false)}, // Normal form
48-
new object[]{" 1′ 1″ ", 1.08333333, new CultureInfo("en-US", false)}, // Normal form, requires trimming
49-
new object[]{"1'1\"", 1.08333333, new CultureInfo("en-US", false)}, // Without space
50-
new object[]{"1′1″", 1.08333333, new CultureInfo("en-US", false)}, // Without space
51-
new object[]{"1 ft 1 in", 1.08333333, new CultureInfo("en-US", false)},
52-
new object[]{"1ft 1in", 1.08333333, new CultureInfo("en-US", false)},
53-
new object[]{"-1'", -1, new CultureInfo("en-US", false)}, // Feet only
54-
new object[]{"-1′", -1, new CultureInfo("en-US", false)}, // Feet only
55-
new object[]{"-1,000′", -1000, new CultureInfo("en-US", false)}, // Feet only, with seperator
56-
new object[]{"-1e3'", -1000, new CultureInfo("en-US", false)}, // Feet only, exponential notation
57-
new object[]{"-1\"", -0.08333333, new CultureInfo("en-US", false)}, // Inches only
58-
new object[]{"-1″", -0.08333333, new CultureInfo("en-US", false)}, // Inches only
59-
new object[]{"-0' 1\"", -0.08333333, new CultureInfo("en-US", false)}, // Inches only
60-
new object[]{"-0' 1″", -0.08333333, new CultureInfo("en-US", false)}, // Inches only
61-
new object[]{"-0′ 1\"", -0.08333333, new CultureInfo("en-US", false)}, // Inches only
62-
new object[]{"-0′ 1″", -0.08333333, new CultureInfo("en-US", false)}, // Inches only
63-
new object[]{"-1' 1\"", -1.08333333, new CultureInfo("en-US", false)}, // Normal form
64-
new object[]{"-1′ 1″", -1.08333333, new CultureInfo("en-US", false)}, // Normal form
65-
new object[]{" -1′ 1″ ", -1.08333333, new CultureInfo("en-US", false)}, // Normal form, requires trimming
66-
new object[]{"-1'1\"", -1.08333333, new CultureInfo("en-US", false)}, // Without space
67-
new object[]{"-1′1″", -1.08333333, new CultureInfo("en-US", false)}, // Without space
68-
new object[]{"-1 ft 1 in", -1.08333333, new CultureInfo("en-US", false)},
69-
new object[]{"-1ft 1in", -1.08333333, new CultureInfo("en-US", false)},
70-
new object[]{"1’000′", 1000, new CultureInfo("de-CH", false)}, // Feet only, with seperator
71-
new object[]{"1’000′ 6\"", 1000.5, new CultureInfo("de-CH", false)}, // Normal form, using separators for culture
38+
new object[]{"1'", 1, EnglishUs}, // Feet only
39+
new object[]{"1′", 1, EnglishUs}, // Feet only
40+
new object[]{"1,000′", 1000, EnglishUs}, // Feet only, with seperator
41+
new object[]{"1e3'", 1000, EnglishUs}, // Feet only, exponential notation
42+
new object[]{"1\"", 0.08333333, EnglishUs}, // Inches only
43+
new object[]{"1″", 0.08333333, EnglishUs}, // Inches only
44+
new object[]{"0' 1\"", 0.08333333, EnglishUs}, // Inches only
45+
new object[]{"0' 1″", 0.08333333, EnglishUs}, // Inches only
46+
new object[]{"0′ 1\"", 0.08333333, EnglishUs}, // Inches only
47+
new object[]{"0′ 1″", 0.08333333, EnglishUs}, // Inches only
48+
new object[]{"1' 1\"", 1.08333333, EnglishUs}, // Normal form
49+
new object[]{"1′ 1″", 1.08333333, EnglishUs}, // Normal form
50+
new object[]{" 1′ 1″ ", 1.08333333, EnglishUs}, // Normal form, requires trimming
51+
new object[]{"1'1\"", 1.08333333, EnglishUs}, // Without space
52+
new object[]{"1′1″", 1.08333333, EnglishUs}, // Without space
53+
new object[]{"1 ft 1 in", 1.08333333, EnglishUs},
54+
new object[]{"1ft 1in", 1.08333333, EnglishUs},
55+
new object[]{"-1'", -1, EnglishUs}, // Feet only
56+
new object[]{"-1′", -1, EnglishUs}, // Feet only
57+
new object[]{"-1,000′", -1000, EnglishUs}, // Feet only, with seperator
58+
new object[]{"-1e3'", -1000, EnglishUs}, // Feet only, exponential notation
59+
new object[]{"-1\"", -0.08333333, EnglishUs}, // Inches only
60+
new object[]{"-1″", -0.08333333, EnglishUs}, // Inches only
61+
new object[]{"-0' 1\"", -0.08333333, EnglishUs}, // Inches only
62+
new object[]{"-0' 1″", -0.08333333, EnglishUs}, // Inches only
63+
new object[]{"-0′ 1\"", -0.08333333, EnglishUs}, // Inches only
64+
new object[]{"-0′ 1″", -0.08333333, EnglishUs}, // Inches only
65+
new object[]{"-1' 1\"", -1.08333333, EnglishUs}, // Normal form
66+
new object[]{"-1′ 1″", -1.08333333, EnglishUs}, // Normal form
67+
new object[]{" -1′ 1″ ", -1.08333333, EnglishUs}, // Normal form, requires trimming
68+
new object[]{"-1'1\"", -1.08333333, EnglishUs}, // Without space
69+
new object[]{"-1′1″", -1.08333333, EnglishUs}, // Without space
70+
new object[]{"-1 ft 1 in", -1.08333333, EnglishUs},
71+
new object[]{"-1ft 1in", -1.08333333, EnglishUs},
72+
new object[]{"1’000′", 1000, GermanSwitzerland}, // Feet only, with seperator
73+
new object[]{"1’000′ 6\"", 1000.5, GermanSwitzerland}, // Normal form, using separators for culture
7274
};
7375

7476
[Theory]
@@ -81,22 +83,22 @@ public void TryParseFeetInches(string str, double expectedFeet, CultureInfo form
8183

8284
public static IEnumerable<object[]> InvalidData => new List<object[]>
8385
{
84-
new object[]{"a", new CultureInfo("en-US", false)}, // Missing or invalid apostrophe or double prime chars
85-
new object[]{"1", new CultureInfo("en-US", false)},
86-
new object[]{"1`", new CultureInfo("en-US", false)},
87-
new object[]{"1^", new CultureInfo("en-US", false)},
88-
new object[]{"1' 1'", new CultureInfo("en-US", false)}, // Feet apostrophe twice
89-
new object[]{"1′ 1′", new CultureInfo("en-US", false)},
90-
new object[]{"1' 1", new CultureInfo("en-US", false)}, // No inches double prime
91-
new object[]{"1′ 1", new CultureInfo("en-US", false)},
92-
new object[]{"1′ 1`", new CultureInfo("en-US", false)}, // Invalid inches double prime
93-
new object[]{"1' 1`", new CultureInfo("en-US", false)},
94-
new object[]{"1'1'", new CultureInfo("en-US", false)}, // Same without space
95-
new object[]{"1′1′", new CultureInfo("en-US", false)},
96-
new object[]{"1'1", new CultureInfo("en-US", false)},
97-
new object[]{"1′1", new CultureInfo("en-US", false)},
98-
new object[]{"1′1`", new CultureInfo("en-US", false)},
99-
new object[]{"1'1`", new CultureInfo("en-US", false)}
86+
new object[]{"a", EnglishUs}, // Missing or invalid apostrophe or double prime chars
87+
new object[]{"1", EnglishUs},
88+
new object[]{"1`", EnglishUs},
89+
new object[]{"1^", EnglishUs},
90+
new object[]{"1' 1'", EnglishUs}, // Feet apostrophe twice
91+
new object[]{"1′ 1′", EnglishUs},
92+
new object[]{"1' 1", EnglishUs}, // No inches double prime
93+
new object[]{"1′ 1", EnglishUs},
94+
new object[]{"1′ 1`", EnglishUs}, // Invalid inches double prime
95+
new object[]{"1' 1`", EnglishUs},
96+
new object[]{"1'1'", EnglishUs}, // Same without space
97+
new object[]{"1′1′", EnglishUs},
98+
new object[]{"1'1", EnglishUs},
99+
new object[]{"1′1", EnglishUs},
100+
new object[]{"1′1`", EnglishUs},
101+
new object[]{"1'1`", EnglishUs}
100102
};
101103

102104
[Theory]

UnitsNet.Tests/CustomCode/ParseTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class ParseTests
2727
[InlineData("500,005 m", 500005)]
2828
public void ParseLengthToMetersUsEnglish(string s, double expected)
2929
{
30-
CultureInfo usEnglish = new CultureInfo("en-US");
30+
CultureInfo usEnglish = CultureInfo.GetCultureInfo("en-US");
3131
double actual = Length.Parse(s, usEnglish).Meters;
3232
Assert.Equal(expected, actual);
3333
}
@@ -41,7 +41,7 @@ public void ParseLengthToMetersUsEnglish(string s, double expected)
4141
[InlineData("1ft 1invalid", typeof(FormatException))] // Valid
4242
public void ParseLength_InvalidString_USEnglish_ThrowsException(string s, Type expectedExceptionType)
4343
{
44-
var usEnglish = new CultureInfo("en-US");
44+
var usEnglish = CultureInfo.GetCultureInfo("en-US");
4545
Assert.Throws(expectedExceptionType, () => Length.Parse(s, usEnglish));
4646
}
4747

@@ -117,7 +117,7 @@ public void ParseWithCultureUsingDotAsThousandSeparators_ThrowsExceptionOnInvali
117117
[InlineData("m", LengthUnit.Meter)]
118118
public void ParseLengthUnitUsEnglish(string s, LengthUnit expected)
119119
{
120-
CultureInfo usEnglish = new CultureInfo("en-US");
120+
CultureInfo usEnglish = CultureInfo.GetCultureInfo("en-US");
121121
LengthUnit actual = Length.ParseUnit(s, usEnglish);
122122
Assert.Equal(expected, actual);
123123
}
@@ -127,7 +127,7 @@ public void ParseLengthUnitUsEnglish(string s, LengthUnit expected)
127127
[InlineData(null, typeof(ArgumentNullException))]
128128
public void ParseLengthUnitUsEnglish_ThrowsExceptionOnInvalidString(string s, Type expectedExceptionType)
129129
{
130-
var usEnglish = new CultureInfo("en-US");
130+
var usEnglish = CultureInfo.GetCultureInfo("en-US");
131131
Assert.Throws(expectedExceptionType, () => Length.ParseUnit(s, usEnglish));
132132
}
133133

@@ -139,7 +139,7 @@ public void ParseLengthUnitUsEnglish_ThrowsExceptionOnInvalidString(string s, Ty
139139
[InlineData("foo", false)]
140140
public void TryParseLengthUnitUsEnglish(string s, bool expected)
141141
{
142-
CultureInfo usEnglish = new CultureInfo("en-US");
142+
CultureInfo usEnglish = CultureInfo.GetCultureInfo("en-US");
143143
bool actual = Length.TryParse(s, usEnglish, out Length _);
144144
Assert.Equal(expected, actual);
145145
}
@@ -153,7 +153,7 @@ public void TryParseLengthUnitUsEnglish(string s, bool expected)
153153
[InlineData("1 кг", "ru-RU", 1, MassUnit.Kilogram)]
154154
public void ParseMassWithPrefixUnits_GivenCulture_ReturnsQuantityWithSameUnitAndValue(string str, string cultureName, double expectedValue, Enum expectedUnit)
155155
{
156-
var actual = Mass.Parse(str, new CultureInfo(cultureName));
156+
var actual = Mass.Parse(str, CultureInfo.GetCultureInfo(cultureName));
157157

158158
Assert.Equal(expectedUnit, actual.Unit);
159159
Assert.Equal(expectedValue, actual.Value);
@@ -168,7 +168,7 @@ public void ParseMassWithPrefixUnits_GivenCulture_ReturnsQuantityWithSameUnitAnd
168168
[InlineData("1 км", "ru-RU", 1, LengthUnit.Kilometer)]
169169
public void ParseLengthWithPrefixUnits_GivenCulture_ReturnsQuantityWithSameUnitAndValue(string str, string cultureName, double expectedValue, Enum expectedUnit)
170170
{
171-
var actual = Length.Parse(str, new CultureInfo(cultureName));
171+
var actual = Length.Parse(str, CultureInfo.GetCultureInfo(cultureName));
172172

173173
Assert.Equal(expectedUnit, actual.Unit);
174174
Assert.Equal(expectedValue, actual.Value);
@@ -183,7 +183,7 @@ public void ParseLengthWithPrefixUnits_GivenCulture_ReturnsQuantityWithSameUnitA
183183
[InlineData("1 кН", "ru-RU", 1, ForceUnit.Kilonewton)]
184184
public void ParseForceWithPrefixUnits_GivenCulture_ReturnsQuantityWithSameUnitAndValue(string str, string cultureName, double expectedValue, Enum expectedUnit)
185185
{
186-
var actual = Force.Parse(str, new CultureInfo(cultureName));
186+
var actual = Force.Parse(str, CultureInfo.GetCultureInfo(cultureName));
187187

188188
Assert.Equal(expectedUnit, actual.Unit);
189189
Assert.Equal(expectedValue, actual.Value);
@@ -204,7 +204,7 @@ public void ParseForceWithPrefixUnits_GivenCulture_ReturnsQuantityWithSameUnitAn
204204
[InlineData("1 MiB", "ru-RU", 1, InformationUnit.Mebibyte)]
205205
public void ParseInformationWithPrefixUnits_GivenCulture_ReturnsQuantityWithSameUnitAndValue(string str, string cultureName, decimal expectedValue, Enum expectedUnit)
206206
{
207-
var actual = Information.Parse(str, new CultureInfo(cultureName));
207+
var actual = Information.Parse(str, CultureInfo.GetCultureInfo(cultureName));
208208

209209
Assert.Equal(expectedUnit, actual.Unit);
210210
Assert.Equal(expectedValue, actual.Value);

UnitsNet.Tests/CustomCode/StonePoundsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void StonePoundsToString_FormatsNumberInCurrentCulture()
4545
[InlineData("fr-FR")]
4646
public void StonePoundsToString_GivenCultureWithThinSpaceDigitGroup_ReturnsNumberWithThinSpaceDigitGroup(string cultureName)
4747
{
48-
var formatProvider = new CultureInfo(cultureName);
48+
var formatProvider = CultureInfo.GetCultureInfo(cultureName);
4949
Mass m = Mass.FromStonePounds(3500, 1);
5050
StonePounds stonePounds = m.StonePounds;
5151

0 commit comments

Comments
 (0)