Skip to content

Commit

Permalink
♻️Use GetCultureInfo instead of creating a new instance each time
Browse files Browse the repository at this point in the history
Saves some unnecessary allocations.
  • Loading branch information
angularsen committed Jun 18, 2023
1 parent d59bfe6 commit 6bd05bf
Show file tree
Hide file tree
Showing 130 changed files with 1,050 additions and 1,048 deletions.
14 changes: 7 additions & 7 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ private void GenerateStaticParseMethods()
/// </summary>
/// <param name=""str"">String to parse. Typically in the form: {{number}} {{unit}}</param>
/// <example>
/// Length.Parse(""5.5 m"", new CultureInfo(""en-US""));
/// Length.Parse(""5.5 m"", CultureInfo.GetCultureInfo(""en-US""));
/// </example>
/// <exception cref=""ArgumentNullException"">The value of 'str' cannot be null. </exception>
/// <exception cref=""ArgumentException"">
Expand All @@ -462,7 +462,7 @@ private void GenerateStaticParseMethods()
/// </summary>
/// <param name=""str"">String to parse. Typically in the form: {{number}} {{unit}}</param>
/// <example>
/// Length.Parse(""5.5 m"", new CultureInfo(""en-US""));
/// Length.Parse(""5.5 m"", CultureInfo.GetCultureInfo(""en-US""));
/// </example>
/// <exception cref=""ArgumentNullException"">The value of 'str' cannot be null. </exception>
/// <exception cref=""ArgumentException"">
Expand Down Expand Up @@ -494,7 +494,7 @@ private void GenerateStaticParseMethods()
/// <param name=""str"">String to parse. Typically in the form: {{number}} {{unit}}</param>
/// <param name=""result"">Resulting unit quantity if successful.</param>
/// <example>
/// Length.Parse(""5.5 m"", new CultureInfo(""en-US""));
/// Length.Parse(""5.5 m"", CultureInfo.GetCultureInfo(""en-US""));
/// </example>
public static bool TryParse(string? str, out {_quantity.Name} result)
{{
Expand All @@ -508,7 +508,7 @@ public static bool TryParse(string? str, out {_quantity.Name} result)
/// <param name=""result"">Resulting unit quantity if successful.</param>
/// <returns>True if successful, otherwise false.</returns>
/// <example>
/// Length.Parse(""5.5 m"", new CultureInfo(""en-US""));
/// Length.Parse(""5.5 m"", CultureInfo.GetCultureInfo(""en-US""));
/// </example>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public static bool TryParse(string? str, IFormatProvider? provider, out {_quantity.Name} result)
Expand All @@ -525,7 +525,7 @@ public static bool TryParse(string? str, IFormatProvider? provider, out {_quanti
/// </summary>
/// <param name=""str"">String to parse. Typically in the form: {{number}} {{unit}}</param>
/// <example>
/// Length.ParseUnit(""m"", new CultureInfo(""en-US""));
/// Length.ParseUnit(""m"", CultureInfo.GetCultureInfo(""en-US""));
/// </example>
/// <exception cref=""ArgumentNullException"">The value of 'str' cannot be null. </exception>
/// <exception cref=""UnitsNetException"">Error parsing string.</exception>
Expand All @@ -540,7 +540,7 @@ public static bool TryParse(string? str, IFormatProvider? provider, out {_quanti
/// <param name=""str"">String to parse. Typically in the form: {{number}} {{unit}}</param>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
/// <example>
/// Length.ParseUnit(""m"", new CultureInfo(""en-US""));
/// Length.ParseUnit(""m"", CultureInfo.GetCultureInfo(""en-US""));
/// </example>
/// <exception cref=""ArgumentNullException"">The value of 'str' cannot be null. </exception>
/// <exception cref=""UnitsNetException"">Error parsing string.</exception>
Expand All @@ -562,7 +562,7 @@ public static bool TryParseUnit(string str, out {_unitEnumName} unit)
/// <param name=""unit"">The parsed unit if successful.</param>
/// <returns>True if successful, otherwise false.</returns>
/// <example>
/// Length.TryParseUnit(""m"", new CultureInfo(""en-US""));
/// Length.TryParseUnit(""m"", CultureInfo.GetCultureInfo(""en-US""));
/// </example>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public static bool TryParseUnit(string str, IFormatProvider? provider, out {_unitEnumName} unit)
Expand Down
2 changes: 1 addition & 1 deletion UnitsNet.Tests/CustomCode/DurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void DurationTimesVolumeFlowEqualsVolume()
[InlineData("1000 мсек", 1, "ru-RU")]
public void DurationFromStringUsingMultipleAbbreviationsParsedCorrectly(string textValue, double expectedSeconds, string? culture = null)
{
var cultureInfo = culture == null ? CultureInfo.InvariantCulture : new CultureInfo(culture);
var cultureInfo = culture == null ? CultureInfo.InvariantCulture : CultureInfo.GetCultureInfo(culture);

AssertEx.EqualTolerance(expectedSeconds, Duration.Parse(textValue, cultureInfo).Seconds, SecondsTolerance);
}
Expand Down
106 changes: 54 additions & 52 deletions UnitsNet.Tests/CustomCode/LengthTests.FeetInches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace UnitsNet.Tests
{
public class FeetInchesTests
{
private static readonly CultureInfo EnglishUs = new("en-US", useUserOverride: false);
private static readonly CultureInfo GermanSwitzerland = new("de-CH", useUserOverride: false);
private const double FeetInOneMeter = 3.28084;
private const double InchesInOneMeter = 39.37007874;
private const double FeetTolerance = 1e-5;
Expand All @@ -33,42 +35,42 @@ public void FeetInchesRoundTrip()

public static IEnumerable<object[]> ValidData => new List<object[]>
{
new object[]{"1'", 1, new CultureInfo("en-US", false)}, // Feet only
new object[]{"1′", 1, new CultureInfo("en-US", false)}, // Feet only
new object[]{"1,000′", 1000, new CultureInfo("en-US", false)}, // Feet only, with seperator
new object[]{"1e3'", 1000, new CultureInfo("en-US", false)}, // Feet only, exponential notation
new object[]{"1\"", 0.08333333, new CultureInfo("en-US", false)}, // Inches only
new object[]{"1″", 0.08333333, new CultureInfo("en-US", false)}, // Inches only
new object[]{"0' 1\"", 0.08333333, new CultureInfo("en-US", false)}, // Inches only
new object[]{"0' 1″", 0.08333333, new CultureInfo("en-US", false)}, // Inches only
new object[]{"0′ 1\"", 0.08333333, new CultureInfo("en-US", false)}, // Inches only
new object[]{"0′ 1″", 0.08333333, new CultureInfo("en-US", false)}, // Inches only
new object[]{"1' 1\"", 1.08333333, new CultureInfo("en-US", false)}, // Normal form
new object[]{"1′ 1″", 1.08333333, new CultureInfo("en-US", false)}, // Normal form
new object[]{" 1′ 1″ ", 1.08333333, new CultureInfo("en-US", false)}, // Normal form, requires trimming
new object[]{"1'1\"", 1.08333333, new CultureInfo("en-US", false)}, // Without space
new object[]{"1′1″", 1.08333333, new CultureInfo("en-US", false)}, // Without space
new object[]{"1 ft 1 in", 1.08333333, new CultureInfo("en-US", false)},
new object[]{"1ft 1in", 1.08333333, new CultureInfo("en-US", false)},
new object[]{"-1'", -1, new CultureInfo("en-US", false)}, // Feet only
new object[]{"-1′", -1, new CultureInfo("en-US", false)}, // Feet only
new object[]{"-1,000′", -1000, new CultureInfo("en-US", false)}, // Feet only, with seperator
new object[]{"-1e3'", -1000, new CultureInfo("en-US", false)}, // Feet only, exponential notation
new object[]{"-1\"", -0.08333333, new CultureInfo("en-US", false)}, // Inches only
new object[]{"-1″", -0.08333333, new CultureInfo("en-US", false)}, // Inches only
new object[]{"-0' 1\"", -0.08333333, new CultureInfo("en-US", false)}, // Inches only
new object[]{"-0' 1″", -0.08333333, new CultureInfo("en-US", false)}, // Inches only
new object[]{"-0′ 1\"", -0.08333333, new CultureInfo("en-US", false)}, // Inches only
new object[]{"-0′ 1″", -0.08333333, new CultureInfo("en-US", false)}, // Inches only
new object[]{"-1' 1\"", -1.08333333, new CultureInfo("en-US", false)}, // Normal form
new object[]{"-1′ 1″", -1.08333333, new CultureInfo("en-US", false)}, // Normal form
new object[]{" -1′ 1″ ", -1.08333333, new CultureInfo("en-US", false)}, // Normal form, requires trimming
new object[]{"-1'1\"", -1.08333333, new CultureInfo("en-US", false)}, // Without space
new object[]{"-1′1″", -1.08333333, new CultureInfo("en-US", false)}, // Without space
new object[]{"-1 ft 1 in", -1.08333333, new CultureInfo("en-US", false)},
new object[]{"-1ft 1in", -1.08333333, new CultureInfo("en-US", false)},
new object[]{"1’000′", 1000, new CultureInfo("de-CH", false)}, // Feet only, with seperator
new object[]{"1’000′ 6\"", 1000.5, new CultureInfo("de-CH", false)}, // Normal form, using separators for culture
new object[]{"1'", 1, EnglishUs}, // Feet only
new object[]{"1′", 1, EnglishUs}, // Feet only
new object[]{"1,000′", 1000, EnglishUs}, // Feet only, with seperator
new object[]{"1e3'", 1000, EnglishUs}, // Feet only, exponential notation
new object[]{"1\"", 0.08333333, EnglishUs}, // Inches only
new object[]{"1″", 0.08333333, EnglishUs}, // Inches only
new object[]{"0' 1\"", 0.08333333, EnglishUs}, // Inches only
new object[]{"0' 1″", 0.08333333, EnglishUs}, // Inches only
new object[]{"0′ 1\"", 0.08333333, EnglishUs}, // Inches only
new object[]{"0′ 1″", 0.08333333, EnglishUs}, // Inches only
new object[]{"1' 1\"", 1.08333333, EnglishUs}, // Normal form
new object[]{"1′ 1″", 1.08333333, EnglishUs}, // Normal form
new object[]{" 1′ 1″ ", 1.08333333, EnglishUs}, // Normal form, requires trimming
new object[]{"1'1\"", 1.08333333, EnglishUs}, // Without space
new object[]{"1′1″", 1.08333333, EnglishUs}, // Without space
new object[]{"1 ft 1 in", 1.08333333, EnglishUs},
new object[]{"1ft 1in", 1.08333333, EnglishUs},
new object[]{"-1'", -1, EnglishUs}, // Feet only
new object[]{"-1′", -1, EnglishUs}, // Feet only
new object[]{"-1,000′", -1000, EnglishUs}, // Feet only, with seperator
new object[]{"-1e3'", -1000, EnglishUs}, // Feet only, exponential notation
new object[]{"-1\"", -0.08333333, EnglishUs}, // Inches only
new object[]{"-1″", -0.08333333, EnglishUs}, // Inches only
new object[]{"-0' 1\"", -0.08333333, EnglishUs}, // Inches only
new object[]{"-0' 1″", -0.08333333, EnglishUs}, // Inches only
new object[]{"-0′ 1\"", -0.08333333, EnglishUs}, // Inches only
new object[]{"-0′ 1″", -0.08333333, EnglishUs}, // Inches only
new object[]{"-1' 1\"", -1.08333333, EnglishUs}, // Normal form
new object[]{"-1′ 1″", -1.08333333, EnglishUs}, // Normal form
new object[]{" -1′ 1″ ", -1.08333333, EnglishUs}, // Normal form, requires trimming
new object[]{"-1'1\"", -1.08333333, EnglishUs}, // Without space
new object[]{"-1′1″", -1.08333333, EnglishUs}, // Without space
new object[]{"-1 ft 1 in", -1.08333333, EnglishUs},
new object[]{"-1ft 1in", -1.08333333, EnglishUs},
new object[]{"1’000′", 1000, GermanSwitzerland}, // Feet only, with seperator
new object[]{"1’000′ 6\"", 1000.5, GermanSwitzerland}, // Normal form, using separators for culture
};

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

public static IEnumerable<object[]> InvalidData => new List<object[]>
{
new object[]{"a", new CultureInfo("en-US", false)}, // Missing or invalid apostrophe or double prime chars
new object[]{"1", new CultureInfo("en-US", false)},
new object[]{"1`", new CultureInfo("en-US", false)},
new object[]{"1^", new CultureInfo("en-US", false)},
new object[]{"1' 1'", new CultureInfo("en-US", false)}, // Feet apostrophe twice
new object[]{"1′ 1′", new CultureInfo("en-US", false)},
new object[]{"1' 1", new CultureInfo("en-US", false)}, // No inches double prime
new object[]{"1′ 1", new CultureInfo("en-US", false)},
new object[]{"1′ 1`", new CultureInfo("en-US", false)}, // Invalid inches double prime
new object[]{"1' 1`", new CultureInfo("en-US", false)},
new object[]{"1'1'", new CultureInfo("en-US", false)}, // Same without space
new object[]{"1′1′", new CultureInfo("en-US", false)},
new object[]{"1'1", new CultureInfo("en-US", false)},
new object[]{"1′1", new CultureInfo("en-US", false)},
new object[]{"1′1`", new CultureInfo("en-US", false)},
new object[]{"1'1`", new CultureInfo("en-US", false)}
new object[]{"a", EnglishUs}, // Missing or invalid apostrophe or double prime chars
new object[]{"1", EnglishUs},
new object[]{"1`", EnglishUs},
new object[]{"1^", EnglishUs},
new object[]{"1' 1'", EnglishUs}, // Feet apostrophe twice
new object[]{"1′ 1′", EnglishUs},
new object[]{"1' 1", EnglishUs}, // No inches double prime
new object[]{"1′ 1", EnglishUs},
new object[]{"1′ 1`", EnglishUs}, // Invalid inches double prime
new object[]{"1' 1`", EnglishUs},
new object[]{"1'1'", EnglishUs}, // Same without space
new object[]{"1′1′", EnglishUs},
new object[]{"1'1", EnglishUs},
new object[]{"1′1", EnglishUs},
new object[]{"1′1`", EnglishUs},
new object[]{"1'1`", EnglishUs}
};

[Theory]
Expand Down
18 changes: 9 additions & 9 deletions UnitsNet.Tests/CustomCode/ParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ParseTests
[InlineData("500,005 m", 500005)]
public void ParseLengthToMetersUsEnglish(string s, double expected)
{
CultureInfo usEnglish = new CultureInfo("en-US");
CultureInfo usEnglish = CultureInfo.GetCultureInfo("en-US");
double actual = Length.Parse(s, usEnglish).Meters;
Assert.Equal(expected, actual);
}
Expand All @@ -41,7 +41,7 @@ public void ParseLengthToMetersUsEnglish(string s, double expected)
[InlineData("1ft 1invalid", typeof(FormatException))] // Valid
public void ParseLength_InvalidString_USEnglish_ThrowsException(string s, Type expectedExceptionType)
{
var usEnglish = new CultureInfo("en-US");
var usEnglish = CultureInfo.GetCultureInfo("en-US");
Assert.Throws(expectedExceptionType, () => Length.Parse(s, usEnglish));
}

Expand Down Expand Up @@ -117,7 +117,7 @@ public void ParseWithCultureUsingDotAsThousandSeparators_ThrowsExceptionOnInvali
[InlineData("m", LengthUnit.Meter)]
public void ParseLengthUnitUsEnglish(string s, LengthUnit expected)
{
CultureInfo usEnglish = new CultureInfo("en-US");
CultureInfo usEnglish = CultureInfo.GetCultureInfo("en-US");
LengthUnit actual = Length.ParseUnit(s, usEnglish);
Assert.Equal(expected, actual);
}
Expand All @@ -127,7 +127,7 @@ public void ParseLengthUnitUsEnglish(string s, LengthUnit expected)
[InlineData(null, typeof(ArgumentNullException))]
public void ParseLengthUnitUsEnglish_ThrowsExceptionOnInvalidString(string s, Type expectedExceptionType)
{
var usEnglish = new CultureInfo("en-US");
var usEnglish = CultureInfo.GetCultureInfo("en-US");
Assert.Throws(expectedExceptionType, () => Length.ParseUnit(s, usEnglish));
}

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

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

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

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

Assert.Equal(expectedUnit, actual.Unit);
Assert.Equal(expectedValue, actual.Value);
Expand Down
2 changes: 1 addition & 1 deletion UnitsNet.Tests/CustomCode/StonePoundsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void StonePoundsToString_FormatsNumberInCurrentCulture()
[InlineData("fr-FR")]
public void StonePoundsToString_GivenCultureWithThinSpaceDigitGroup_ReturnsNumberWithThinSpaceDigitGroup(string cultureName)
{
var formatProvider = new CultureInfo(cultureName);
var formatProvider = CultureInfo.GetCultureInfo(cultureName);
Mass m = Mass.FromStonePounds(3500, 1);
StonePounds stonePounds = m.StonePounds;

Expand Down
Loading

0 comments on commit 6bd05bf

Please sign in to comment.