Allow null input in TimeSpanParser.TryParse

This commit is contained in:
Oliver Booth 2022-12-22 12:28:35 +00:00
parent fdc9e64cbe
commit 94a841b2fc
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 9 additions and 6 deletions

View File

@ -108,6 +108,7 @@
- X10D.Unity: Added `WaitForTimeSpanRealtimeNoAlloc` yield instruction - X10D.Unity: Added `WaitForTimeSpanRealtimeNoAlloc` yield instruction
### Changed ### Changed
- X10D: `TimeSpanParser.TryParse` now accepts a nullable string, and returns false if this input is null or empty
- X10D.Unity: Obsolesced `Singleton<T>` - X10D.Unity: Obsolesced `Singleton<T>`
## [3.1.0] ## [3.1.0]

View File

@ -1,4 +1,6 @@
namespace X10D.Time; using System.Diagnostics.CodeAnalysis;
namespace X10D.Time;
/// <summary> /// <summary>
/// Represents a class which contains a <see cref="string" /> parser which converts into <see cref="TimeSpan" />. /// Represents a class which contains a <see cref="string" /> parser which converts into <see cref="TimeSpan" />.
@ -54,15 +56,15 @@ public static class TimeSpanParser
/// </param> /// </param>
/// <param name="result">When this method returns, contains the parsed result.</param> /// <param name="result">When this method returns, contains the parsed result.</param>
/// <returns><see langword="true" /> if the parse was successful, <see langword="false" /> otherwise.</returns> /// <returns><see langword="true" /> if the parse was successful, <see langword="false" /> otherwise.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception> public static bool TryParse([NotNullWhen(true)] string? value, out TimeSpan result)
public static bool TryParse(string value, out TimeSpan result)
{ {
if (value is null) result = TimeSpan.Zero;
if (string.IsNullOrWhiteSpace(value))
{ {
throw new ArgumentNullException(nameof(value)); return false;
} }
result = TimeSpan.Zero;
var unitValue = 0; var unitValue = 0;
for (var index = 0; index < value.Length; index++) for (var index = 0; index < value.Length; index++)