Skip to content
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

Enable Nullable ref types #1437

Merged
merged 44 commits into from
Feb 23, 2024
Merged
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
8c5af5e
Update ArticlePrefixSort.cs
SimonCropp Feb 22, 2024
cc4b5d3
Update Benchmarks.csproj
SimonCropp Feb 22, 2024
b9d7db5
.
SimonCropp Feb 22, 2024
6130a45
.
SimonCropp Feb 22, 2024
c32ecae
.
SimonCropp Feb 22, 2024
966d0ef
.
SimonCropp Feb 22, 2024
10a5a10
Merge branch 'main' into nullable
SimonCropp Feb 22, 2024
a7d9740
.
SimonCropp Feb 22, 2024
8569ce2
.
SimonCropp Feb 22, 2024
a06be6c
.
SimonCropp Feb 22, 2024
776ae08
.
SimonCropp Feb 22, 2024
64cf66e
.
SimonCropp Feb 22, 2024
b4360ea
Merge branch 'main' into nullable
SimonCropp Feb 22, 2024
a82c56e
.
SimonCropp Feb 22, 2024
e20ca2e
.
SimonCropp Feb 22, 2024
aa83c9c
.
SimonCropp Feb 22, 2024
4abeb7a
.
SimonCropp Feb 22, 2024
42c4361
.
SimonCropp Feb 22, 2024
cb72216
Merge branch 'main' into nullable
SimonCropp Feb 22, 2024
eacb34a
.
SimonCropp Feb 22, 2024
f00b237
.
SimonCropp Feb 22, 2024
05a03dc
.
SimonCropp Feb 22, 2024
0a45b8e
.
SimonCropp Feb 22, 2024
9be3145
.
SimonCropp Feb 22, 2024
4fc3d81
.
SimonCropp Feb 22, 2024
6e3a272
Merge branch 'main' into nullable
SimonCropp Feb 22, 2024
6a761ae
.
SimonCropp Feb 22, 2024
6bf9fd2
.
SimonCropp Feb 22, 2024
f5d913d
.
SimonCropp Feb 22, 2024
8ab984f
.
SimonCropp Feb 22, 2024
7554861
.
SimonCropp Feb 22, 2024
5589486
.
SimonCropp Feb 22, 2024
6c4c12f
Update Vocabulary.cs
SimonCropp Feb 22, 2024
cc4a84d
Update RomanianCardinalNumberConverter.cs
SimonCropp Feb 22, 2024
81a08e9
Update IndianNumberToWordsConverter.cs
SimonCropp Feb 22, 2024
8cfe3ad
Update NorwegianBokmalNumberToWordsConverter.cs
SimonCropp Feb 22, 2024
508cb80
Update EnumCache.cs
SimonCropp Feb 22, 2024
9e7d770
.
SimonCropp Feb 22, 2024
4954080
.
SimonCropp Feb 22, 2024
6401df4
.
SimonCropp Feb 22, 2024
6df6649
Merge branch 'main' into nullable
SimonCropp Feb 22, 2024
b87e36f
Update HeadingExtensions.cs
SimonCropp Feb 22, 2024
ee7d4f3
.
SimonCropp Feb 22, 2024
02a964b
.
SimonCropp Feb 22, 2024
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
Prev Previous commit
Next Next commit
.
SimonCropp committed Feb 22, 2024
commit 4abeb7a04b182477eec43a2b0f5c382bc8357b36
8 changes: 4 additions & 4 deletions src/Humanizer/Bytes/ByteSize.cs
Original file line number Diff line number Diff line change
@@ -205,7 +205,7 @@ public string ToString(IFormatProvider provider)
public string ToString(string format) =>
ToString(format, NumberFormatInfo.CurrentInfo);

public string ToString(string format, IFormatProvider provider) =>
public string ToString(string? format, IFormatProvider? provider) =>
ToString(format, provider, toSymbol: true);

string ToString(string? format, IFormatProvider? provider, bool toSymbol)
@@ -392,7 +392,7 @@ public ByteSize Subtract(ByteSize bs) =>
public static bool TryParse(string s, out ByteSize result) =>
TryParse(s, null, out result);

public static bool TryParse(string s, IFormatProvider formatProvider, out ByteSize result)
public static bool TryParse(string s, IFormatProvider? formatProvider, out ByteSize result)
{
// Arg checking
if (string.IsNullOrWhiteSpace(s))
@@ -491,7 +491,7 @@ public static bool TryParse(string s, IFormatProvider formatProvider, out ByteSi
return true;
}

static NumberFormatInfo GetNumberFormatInfo(IFormatProvider formatProvider)
static NumberFormatInfo GetNumberFormatInfo(IFormatProvider? formatProvider)
{
if (formatProvider is NumberFormatInfo numberFormat)
return numberFormat;
@@ -504,7 +504,7 @@ static NumberFormatInfo GetNumberFormatInfo(IFormatProvider formatProvider)
public static ByteSize Parse(string s) =>
Parse(s, null);

public static ByteSize Parse(string s, IFormatProvider formatProvider)
public static ByteSize Parse(string s, IFormatProvider? formatProvider)
{
if (s == null)
{
11 changes: 6 additions & 5 deletions src/Humanizer/Configuration/LocaliserRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
namespace Humanizer
#nullable enable
namespace Humanizer
{
/// <summary>
/// A registry of localised system components with their associated locales
/// </summary>
public class LocaliserRegistry<TLocaliser>
where TLocaliser : class
{
readonly IDictionary<string, Func<CultureInfo, TLocaliser>> _localisers = new Dictionary<string, Func<CultureInfo, TLocaliser>>();
readonly Func<CultureInfo, TLocaliser> _defaultLocaliser;
readonly IDictionary<string, Func<CultureInfo?, TLocaliser>> _localisers = new Dictionary<string, Func<CultureInfo, TLocaliser>>();
readonly Func<CultureInfo?, TLocaliser> _defaultLocaliser;

/// <summary>
/// Creates a localiser registry with the default localiser set to the provided value
@@ -18,7 +19,7 @@ public LocaliserRegistry(TLocaliser defaultLocaliser) =>
/// <summary>
/// Creates a localiser registry with the default localiser factory set to the provided value
/// </summary>
public LocaliserRegistry(Func<CultureInfo, TLocaliser> defaultLocaliser) =>
public LocaliserRegistry(Func<CultureInfo?, TLocaliser> defaultLocaliser) =>
_defaultLocaliser = defaultLocaliser;

/// <summary>
@@ -46,7 +47,7 @@ public void Register(string localeCode, TLocaliser localiser) =>
public void Register(string localeCode, Func<CultureInfo, TLocaliser> localiser) =>
_localisers[localeCode] = localiser;

Func<CultureInfo, TLocaliser> FindLocaliser(CultureInfo culture)
Func<CultureInfo?, TLocaliser> FindLocaliser(CultureInfo culture)
{
for (var c = culture; !string.IsNullOrEmpty(c?.Name); c = c.Parent)
{
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ static class DateTimeHumanizeAlgorithms
/// <summary>
/// Returns localized &amp; humanized distance of time between two dates; given a specific precision.
/// </summary>
public static string PrecisionHumanize(DateTime input, DateTime comparisonBase, double precision, CultureInfo culture)
public static string PrecisionHumanize(DateTime input, DateTime comparisonBase, double precision, CultureInfo? culture)
{
var ts = new TimeSpan(Math.Abs(comparisonBase.Ticks - input.Ticks));
var tense = input > comparisonBase ? Tense.Future : Tense.Past;
@@ -42,7 +42,7 @@ public static string PrecisionHumanize(TimeOnly input, TimeOnly comparisonBase,
return PrecisionHumanize(ts, tense, precision, culture);
}
#endif
static string PrecisionHumanize(TimeSpan ts, Tense tense, double precision, CultureInfo culture)
static string PrecisionHumanize(TimeSpan ts, Tense tense, double precision, CultureInfo? culture)
{
int seconds = ts.Seconds, minutes = ts.Minutes, hours = ts.Hours, days = ts.Days;
int years = 0, months = 0;
@@ -133,7 +133,7 @@ static string PrecisionHumanize(TimeSpan ts, Tense tense, double precision, Cult
/// <summary>
/// Calculates the distance of time in words between two provided dates
/// </summary>
public static string DefaultHumanize(DateTime input, DateTime comparisonBase, CultureInfo culture)
public static string DefaultHumanize(DateTime input, DateTime comparisonBase, CultureInfo? culture)
{
var tense = input > comparisonBase ? Tense.Future : Tense.Past;
var ts = new TimeSpan(Math.Abs(comparisonBase.Ticks - input.Ticks));
@@ -174,7 +174,7 @@ public static string DefaultHumanize(TimeOnly input, TimeOnly comparisonBase, Cu
}
#endif

static string DefaultHumanize(TimeSpan ts, bool sameMonth, int days, Tense tense, CultureInfo culture)
static string DefaultHumanize(TimeSpan ts, bool sameMonth, int days, Tense tense, CultureInfo? culture)
{
var formatter = Configurator.GetFormatter(culture);

Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ public class DefaultDateTimeHumanizeStrategy : IDateTimeHumanizeStrategy
/// <summary>
/// Calculates the distance of time in words between two provided dates
/// </summary>
public string Humanize(DateTime input, DateTime comparisonBase, CultureInfo culture) =>
public string Humanize(DateTime input, DateTime comparisonBase, CultureInfo? culture) =>
DateTimeHumanizeAlgorithms.DefaultHumanize(input, comparisonBase, culture);
}
}
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ public class DefaultDateTimeOffsetHumanizeStrategy : IDateTimeOffsetHumanizeStra
/// <summary>
/// Calculates the distance of time in words between two provided dates
/// </summary>
public string Humanize(DateTimeOffset input, DateTimeOffset comparisonBase, CultureInfo culture) =>
public string Humanize(DateTimeOffset input, DateTimeOffset comparisonBase, CultureInfo? culture) =>
DateTimeHumanizeAlgorithms.DefaultHumanize(input.UtcDateTime, comparisonBase.UtcDateTime, culture);
}
}
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ public PrecisionDateTimeHumanizeStrategy(double precision = .75) =>
/// <summary>
/// Returns localized &amp; humanized distance of time between two dates; given a specific precision.
/// </summary>
public string Humanize(DateTime input, DateTime comparisonBase, CultureInfo culture) =>
public string Humanize(DateTime input, DateTime comparisonBase, CultureInfo? culture) =>
DateTimeHumanizeAlgorithms.PrecisionHumanize(input, comparisonBase, _precision, culture);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Humanizer
#nullable enable
namespace Humanizer
{
class DefaultCollectionFormatter : ICollectionFormatter
{
@@ -53,7 +54,7 @@ public string Humanize<T>(IEnumerable<T> collection, Func<T, object> objectForma
separator);
}

string HumanizeDisplayStrings(IEnumerable<string> strings, string separator)
string HumanizeDisplayStrings(IEnumerable<string?> strings, string separator)
{
var itemsArray = strings
.Select(item => item == null ? string.Empty : item.Trim())
10 changes: 4 additions & 6 deletions src/Humanizer/TimeSpanHumanizeExtensions.cs
Original file line number Diff line number Diff line change
@@ -58,12 +58,10 @@ public static string ToAge(this TimeSpan timeSpan, CultureInfo? culture = null,
var timeSpanExpression = timeSpan.Humanize(culture: culture, maxUnit: maxUnit, toWords: toWords);

var cultureFormatter = Configurator.GetFormatter(culture);
var ageExpression = string.Format(cultureFormatter.TimeSpanHumanize_Age(), timeSpanExpression);

return ageExpression;
return string.Format(cultureFormatter.TimeSpanHumanize_Age(), timeSpanExpression);
}

static IEnumerable<string> CreateTheTimePartsWithUpperAndLowerLimits(TimeSpan timespan, CultureInfo? culture, TimeUnit maxUnit, TimeUnit minUnit, bool toWords = false)
static IEnumerable<string?> CreateTheTimePartsWithUpperAndLowerLimits(TimeSpan timespan, CultureInfo? culture, TimeUnit maxUnit, TimeUnit minUnit, bool toWords = false)
{
var cultureFormatter = Configurator.GetFormatter(culture);
var firstValueFound = false;
@@ -184,13 +182,13 @@ static int GetNormalCaseTimeAsInteger(int timeNumberOfUnits, double totalTimeNum
? cultureFormatter.TimeSpanHumanize(timeUnitType, Math.Abs(amountOfTimeUnits), toWords)
: null;

static List<string> CreateTimePartsWithNoTimeValue(string noTimeValue) =>
static List<string?> CreateTimePartsWithNoTimeValue(string noTimeValue) =>
[noTimeValue];

static bool IsContainingOnlyNullValue(IEnumerable<string?> timeParts) =>
timeParts.Count(x => x != null) == 0;

static IEnumerable<string> SetPrecisionOfTimeSpan(IEnumerable<string> timeParts, int precision, bool countEmptyUnits)
static IEnumerable<string> SetPrecisionOfTimeSpan(IEnumerable<string?> timeParts, int precision, bool countEmptyUnits)
{
if (!countEmptyUnits)
{