Skip to content

Commit 1ea4423

Browse files
committed
codegen: Use DefaultCulture in ToString()
As fallback if no culture is specified. This allows the user to specify a culture other than CurrentUICulture and fixes tests where rely on that to test default behavior.
1 parent c004773 commit 1ea4423

File tree

1 file changed

+135
-36
lines changed

1 file changed

+135
-36
lines changed

UnitsNet/Scripts/Include-GenerateQuantitySourceCode.ps1

Lines changed: 135 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@ using System.Linq;
7575
using JetBrains.Annotations;
7676
using UnitsNet.Units;
7777
78-
// Windows Runtime Component does not support CultureInfo type, so use culture name string instead for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
79-
#if WINDOWS_UWP
80-
using Culture = System.String;
81-
#else
82-
using Culture = System.IFormatProvider;
83-
#endif
84-
8578
// ReSharper disable once CheckNamespace
8679
8780
namespace UnitsNet
@@ -293,12 +286,29 @@ namespace UnitsNet
293286
/// Get unit abbreviation string.
294287
/// </summary>
295288
/// <param name="unit">Unit to get abbreviation for.</param>
296-
/// <param name="culture">Culture to use for localization. Defaults to Thread.CurrentUICulture.</param>
289+
#if WINDOWS_UWP
290+
/// <param name="cultureName">Name of culture (ex: "en-US") to use for localization. Defaults to <see cref="UnitSystem" />'s default culture.</param>
291+
#else
292+
/// <param name="provider">Format to use for localization. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
293+
#endif
297294
/// <returns>Unit abbreviation string.</returns>
298295
[UsedImplicitly]
299-
public static string GetAbbreviation($unitEnumName unit, [CanBeNull] Culture culture)
296+
public static string GetAbbreviation(
297+
$unitEnumName unit,
298+
#if WINDOWS_UWP
299+
[CanBeNull] string cultureName)
300+
#else
301+
[CanBeNull] IFormatProvider provider)
302+
#endif
300303
{
301-
return UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit);
304+
#if WINDOWS_UWP
305+
// Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
306+
IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName);
307+
#else
308+
provider = provider ?? UnitSystem.DefaultCulture;
309+
#endif
310+
311+
return UnitSystem.GetCached(provider).GetDefaultAbbreviation(unit);
302312
}
303313
304314
#endregion
@@ -498,7 +508,11 @@ namespace UnitsNet
498508
/// Parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
499509
/// </summary>
500510
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
501-
/// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
511+
#if WINDOWS_UWP
512+
/// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="UnitSystem" />'s default culture.</param>
513+
#else
514+
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
515+
#endif
502516
/// <example>
503517
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
504518
/// </example>
@@ -517,17 +531,24 @@ namespace UnitsNet
517531
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
518532
/// Units.NET exceptions from other exceptions.
519533
/// </exception>
520-
public static $quantityName Parse(string str, [CanBeNull] Culture culture)
534+
public static $quantityName Parse(
535+
string str,
536+
#if WINDOWS_UWP
537+
[CanBeNull] string cultureName)
538+
#else
539+
[CanBeNull] IFormatProvider provider)
540+
#endif
521541
{
522542
if (str == null) throw new ArgumentNullException("str");
523543
524-
// Windows Runtime Component does not support CultureInfo type, so use culture name string for public methods instead: https://msdn.microsoft.com/en-us/library/br230301.aspx
525544
#if WINDOWS_UWP
526-
IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture);
545+
// Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
546+
IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName);
527547
#else
528-
IFormatProvider formatProvider = culture;
548+
provider = provider ?? UnitSystem.DefaultCulture;
529549
#endif
530-
return QuantityParser.Parse<$quantityName, $unitEnumName>(str, formatProvider,
550+
551+
return QuantityParser.Parse<$quantityName, $unitEnumName>(str, provider,
531552
delegate(string value, string unit, IFormatProvider formatProvider2)
532553
{
533554
double parsedValue = double.Parse(value, formatProvider2);
@@ -553,16 +574,41 @@ namespace UnitsNet
553574
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
554575
/// </summary>
555576
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
556-
/// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
577+
#if WINDOWS_UWP
578+
/// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="UnitSystem" />'s default culture.</param>
579+
#else
580+
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
581+
#endif
557582
/// <param name="result">Resulting unit quantity if successful.</param>
558583
/// <example>
559584
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
560585
/// </example>
561-
public static bool TryParse([CanBeNull] string str, [CanBeNull] Culture culture, out $quantityName result)
586+
public static bool TryParse(
587+
[CanBeNull] string str,
588+
#if WINDOWS_UWP
589+
[CanBeNull] string cultureName,
590+
#else
591+
[CanBeNull] IFormatProvider provider,
592+
#endif
593+
out $quantityName result)
562594
{
595+
#if WINDOWS_UWP
596+
// Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
597+
IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName);
598+
#else
599+
provider = provider ?? UnitSystem.DefaultCulture;
600+
#endif
563601
try
564602
{
565-
result = Parse(str, culture);
603+
604+
result = Parse(
605+
str,
606+
#if WINDOWS_UWP
607+
cultureName);
608+
#else
609+
provider);
610+
#endif
611+
566612
return true;
567613
}
568614
catch
@@ -575,6 +621,7 @@ namespace UnitsNet
575621
/// <summary>
576622
/// Parse a unit string.
577623
/// </summary>
624+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
578625
/// <example>
579626
/// Length.ParseUnit("m", new CultureInfo("en-US"));
580627
/// </example>
@@ -588,11 +635,14 @@ namespace UnitsNet
588635
/// <summary>
589636
/// Parse a unit string.
590637
/// </summary>
638+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
639+
/// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="UnitSystem" />'s default culture.</param>
591640
/// <example>
592641
/// Length.ParseUnit("m", new CultureInfo("en-US"));
593642
/// </example>
594643
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
595644
/// <exception cref="UnitsNetException">Error parsing string.</exception>
645+
[Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from other .NET targets.")]
596646
public static $unitEnumName ParseUnit(string str, [CanBeNull] string cultureName)
597647
{
598648
return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName));
@@ -601,6 +651,8 @@ namespace UnitsNet
601651
/// <summary>
602652
/// Parse a unit string.
603653
/// </summary>
654+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
655+
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
604656
/// <example>
605657
/// Length.ParseUnit("m", new CultureInfo("en-US"));
606658
/// </example>
@@ -613,18 +665,18 @@ namespace UnitsNet
613665
#else
614666
public
615667
#endif
616-
static $unitEnumName ParseUnit(string str, IFormatProvider formatProvider = null)
668+
static $unitEnumName ParseUnit(string str, IFormatProvider provider = null)
617669
{
618670
if (str == null) throw new ArgumentNullException("str");
619671
620-
var unitSystem = UnitSystem.GetCached(formatProvider);
672+
var unitSystem = UnitSystem.GetCached(provider);
621673
var unit = unitSystem.Parse<$unitEnumName>(str.Trim());
622674
623675
if (unit == $unitEnumName.Undefined)
624676
{
625677
var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized $unitEnumName.");
626678
newEx.Data["input"] = str;
627-
newEx.Data["formatprovider"] = formatProvider?.ToString() ?? "(null)";
679+
newEx.Data["provider"] = provider?.ToString() ?? "(null)";
628680
throw newEx;
629681
}
630682
@@ -633,7 +685,7 @@ namespace UnitsNet
633685
634686
#endregion
635687
636-
[Obsolete("This should no longer be used. ToString() uses the unit this quantity value was constructed with as default. Pass argument BaseUnit to preserve old behavior.")]
688+
[Obsolete("This is no longer used since we will instead use the quantity's Unit value as default.")]
637689
/// <summary>
638690
/// Set the default unit used by ToString(). Default is $baseUnitSingularName
639691
/// </summary>
@@ -662,52 +714,99 @@ namespace UnitsNet
662714
/// Get string representation of value and unit. Using two significant digits after radix.
663715
/// </summary>
664716
/// <param name="unit">Unit representation to use.</param>
665-
/// <param name="culture">Culture to use for localization and number formatting.</param>
717+
#if WINDOWS_UWP
718+
/// <param name="cultureName">Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to <see cref="UnitSystem" />'s default culture.</param>
719+
#else
720+
/// <param name="provider">Format to use for localization and number formatting. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
721+
#endif
666722
/// <returns>String representation.</returns>
667-
public string ToString($unitEnumName unit, [CanBeNull] Culture culture)
723+
public string ToString(
724+
$unitEnumName unit,
725+
#if WINDOWS_UWP
726+
[CanBeNull] string cultureName)
727+
#else
728+
[CanBeNull] IFormatProvider provider)
729+
#endif
668730
{
669-
return ToString(unit, culture, 2);
731+
return ToString(
732+
unit,
733+
#if WINDOWS_UWP
734+
cultureName,
735+
#else
736+
provider,
737+
#endif
738+
2);
670739
}
671740
672741
/// <summary>
673742
/// Get string representation of value and unit.
674743
/// </summary>
675744
/// <param name="unit">Unit representation to use.</param>
676-
/// <param name="culture">Culture to use for localization and number formatting.</param>
745+
#if WINDOWS_UWP
746+
/// <param name="cultureName">Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to <see cref="UnitSystem" />'s default culture.</param>
747+
#else
748+
/// <param name="provider">Format to use for localization and number formatting. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
749+
#endif
677750
/// <param name="significantDigitsAfterRadix">The number of significant digits after the radix point.</param>
678751
/// <returns>String representation.</returns>
679752
[UsedImplicitly]
680-
public string ToString($unitEnumName unit, [CanBeNull] Culture culture, int significantDigitsAfterRadix)
753+
public string ToString(
754+
$unitEnumName unit,
755+
#if WINDOWS_UWP
756+
[CanBeNull] string cultureName,
757+
#else
758+
[CanBeNull] IFormatProvider provider,
759+
#endif
760+
int significantDigitsAfterRadix)
681761
{
682762
double value = As(unit);
683763
string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);
684-
return ToString(unit, culture, format);
764+
return ToString(
765+
unit,
766+
#if WINDOWS_UWP
767+
cultureName,
768+
#else
769+
provider,
770+
#endif
771+
format);
685772
}
686773
687774
/// <summary>
688775
/// Get string representation of value and unit.
689776
/// </summary>
690-
/// <param name="culture">Culture to use for localization and number formatting.</param>
777+
#if WINDOWS_UWP
778+
/// <param name="cultureName">Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to <see cref="UnitSystem" />'s default culture.</param>
779+
#else
780+
/// <param name="provider">Format to use for localization and number formatting. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
781+
#endif
691782
/// <param name="unit">Unit representation to use.</param>
692783
/// <param name="format">String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively."</param>
693784
/// <param name="args">Arguments for string format. Value and unit are implictly included as arguments 0 and 1.</param>
694785
/// <returns>String representation.</returns>
695786
[UsedImplicitly]
696-
public string ToString($unitEnumName unit, [CanBeNull] Culture culture, [NotNull] string format,
787+
public string ToString(
788+
$unitEnumName unit,
789+
#if WINDOWS_UWP
790+
[CanBeNull] string cultureName,
791+
#else
792+
[CanBeNull] IFormatProvider provider,
793+
#endif
794+
[NotNull] string format,
697795
[NotNull] params object[] args)
698796
{
699797
if (format == null) throw new ArgumentNullException(nameof(format));
700798
if (args == null) throw new ArgumentNullException(nameof(args));
701799
702-
// Windows Runtime Component does not support CultureInfo type, so use culture name string for public methods instead: https://msdn.microsoft.com/en-us/library/br230301.aspx
703800
#if WINDOWS_UWP
704-
IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture);
801+
// Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
802+
IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName);
705803
#else
706-
IFormatProvider formatProvider = culture;
804+
provider = provider ?? UnitSystem.DefaultCulture;
707805
#endif
806+
708807
double value = As(unit);
709-
object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, formatProvider, args);
710-
return string.Format(formatProvider, format, formatArgs);
808+
object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args);
809+
return string.Format(provider, format, formatArgs);
711810
}
712811
713812
/// <summary>

0 commit comments

Comments
 (0)