From 81dbbdd9485c565271336bf8feda10683de7d428 Mon Sep 17 00:00:00 2001 From: Ivan Ivon Date: Fri, 11 Nov 2022 01:36:38 +0200 Subject: [PATCH] Add more numeric conversion extension methods. --- README.md | 193 ++++++++++--- .../CodeGen/CsvCSharpCodeGenerator.cs | 2 +- Src/CsvLINQPadDriver/Directory.Build.props | 3 +- .../Extensions/FileExtensions.cs | 7 +- .../Extensions/StringExtensions.cs | 273 ++++++++++++------ .../LPRun/Templates/Extensions.linq | 76 +++++ Tests/CsvLINQPadDriverTest/LPRunTests.cs | 6 + 7 files changed, 425 insertions(+), 135 deletions(-) diff --git a/README.md b/README.md index 70b562a..da097f1 100644 --- a/README.md +++ b/README.md @@ -361,133 +361,238 @@ Authors.First() Driver provides extension methods for converting `string` (and `ReadOnlySpan` for .NET Core/.NET) to `T?`. `CultureInfo.InvariantCulture` is used by default. ```csharp -// Bool. -bool? ToBool(CultureInfo? cultureInfo = null); +public static class Styles +{ + public const NumberStyles Integer = + NumberStyles.Integer | + NumberStyles.AllowThousands; + + public const NumberStyles Float = + NumberStyles.Float | + NumberStyles.AllowThousands; + + public const NumberStyles Decimal = + NumberStyles.Number; + + public const DateTimeStyles DateTimeOffset = + DateTimeStyles.None; + + public const DateTimeStyles DateTime = + DateTimeStyles.None; + + public const DateTimeStyles UtcDateTime = + DateTimeStyles.AdjustToUniversal | + DateTimeStyles.AssumeUniversal; + + public const TimeSpanStyles TimeSpan = + TimeSpanStyles.None; + + // .NET 6+ + public const DateTimeStyles DateOnly = + DateTimeStyles.None; + + // .NET 6+ + public const DateTimeStyles TimeOnly = + DateTimeStyles.None; +} + +// Bool +bool? ToBool( + NumberStyles style = Styles.Integer, + CultureInfo? cultureInfo = null); + +// SByte +sbyte? ToSByte( + NumberStyles style = Styles.Integer, + CultureInfo? cultureInfo = null); + +// Byte +byte? ToByte( + NumberStyles style = Styles.Integer, + CultureInfo? cultureInfo = null); + +// Short +short? ToShort( + NumberStyles style = Styles.Integer, + CultureInfo? cultureInfo = null); + +// UShort +ushort? ToUShort( + NumberStyles style = Styles.Integer, + CultureInfo? cultureInfo = null); + +// Int +int? ToInt( + NumberStyles style = Styles.Integer, + CultureInfo? cultureInfo = null); -// Int. -int? ToInt(CultureInfo? cultureInfo = null); +// UInt +uint? ToUInt( + NumberStyles style = Styles.Integer, + CultureInfo? cultureInfo = null); -// Long. -long? ToLong(CultureInfo? cultureInfo = null); +// Long +long? ToLong( + NumberStyles style = Styles.Integer, + CultureInfo? cultureInfo = null); -// Float. -float? ToFloat(CultureInfo? cultureInfo = null); +// ULong +ulong? ToULong( + NumberStyles style = Styles.Integer, + CultureInfo? cultureInfo = null); -// Double. -double? ToDouble(CultureInfo? cultureInfo = null); +// NInt: .NET 5+ +nint? ToNInt( + NumberStyles style = Styles.Integer, + CultureInfo? cultureInfo = null); -// Decimal. -decimal? ToDecimal(CultureInfo? cultureInfo = null); +// NUInt: .NET 5+ +nuint? ToNUInt( + NumberStyles style = Styles.Integer, + CultureInfo? cultureInfo = null); -// Guid. +// Float +float? ToFloat( + NumberStyles style = Styles.Float, + CultureInfo? cultureInfo = null); + +// Double +double? ToDouble( + NumberStyles style = Styles.Float, + CultureInfo? cultureInfo = null); + +// Decimal +decimal? ToDecimal( + NumberStyles style = Styles.Decimal, + CultureInfo? cultureInfo = null); + +// Half: .NET 5+ +Half? ToHalf( + NumberStyles style = Styles.Float, + CultureInfo? cultureInfo = null); + +// Int128: .NET 7+ +static Int128? ToInt128( + NumberStyles style = Styles.Integer, + CultureInfo? cultureInfo = null); + +// UInt128: .NET 7+ +static UInt128? ToUInt128( + NumberStyles style = Styles.Integer, + CultureInfo? cultureInfo = null); + +// Guid Guid? ToGuid(); Guid? ToGuid(string format); Guid? ToGuid(ReadOnlySpan format); Guid? ToGuid(string[] formats); -// DateTime. +// DateTime DateTime? ToDateTime( - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.DateTime, CultureInfo? cultureInfo = null); DateTime? ToDateTime( string format, - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.DateTime, CultureInfo? cultureInfo = null); -// .NET Core/.NET only. +// .NET Core/.NET DateTime? ToDateTime( ReadOnlySpan format, - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.DateTime, CultureInfo? cultureInfo = null); DateTime? ToDateTime( string[] formats, - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.DateTime, CultureInfo? cultureInfo = null); -DateTime? ToUtcDateTime(CultureInfo? cultureInfo = null); +DateTime? ToUtcDateTime( + DateTimeStyles style = Styles.UtcDateTime, + CultureInfo? cultureInfo = null); -// DateTimeOffset. +// DateTimeOffset DateTimeOffset? ToDateTimeOffset( - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.DateTimeOffset, CultureInfo? cultureInfo = null); DateTimeOffset? ToDateTimeOffset( string format, - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.DateTimeOffset, CultureInfo? cultureInfo = null); -// .NET Core/.NET only. +// .NET Core/.NET DateTimeOffset? ToDateTimeOffset( ReadOnlySpan format, - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.DateTimeOffset, CultureInfo? cultureInfo = null); DateTimeOffset? ToDateTimeOffset( string[] formats, - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.DateTimeOffset, CultureInfo? cultureInfo = null); -// TimeSpan. -TimeSpan? ToTimeSpan(CultureInfo? cultureInfo = null); +// TimeSpan +TimeSpan? ToTimeSpan( + CultureInfo? cultureInfo = null); TimeSpan? ToTimeSpan( string format, - TimeSpanStyles timeSpanStyles = TimeSpanStyles.None, + TimeSpanStyles style = Styles.TimeSpan, CultureInfo? cultureInfo = null); -// .NET Core/.NET only. +// .NET Core/.NET TimeSpan? ToTimeSpan( ReadOnlySpan format, - TimeSpanStyles timeSpanStyles = TimeSpanStyles.None, + TimeSpanStyles style = Styles.TimeSpan, CultureInfo? cultureInfo = null); TimeSpan? ToTimeSpan( string[] formats, - TimeSpanStyles timeSpanStyles = TimeSpanStyles.None, + TimeSpanStyles style = Styles.TimeSpan, CultureInfo? cultureInfo = null); -// .NET 6+: DateOnly. +// DateOnly: .NET 6+ DateOnly? ToDateOnly( - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.DateOnly, CultureInfo? cultureInfo = null); DateOnly? ToDateOnly( string format, - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.DateOnly, CultureInfo? cultureInfo = null); -// .NET Core/.NET only. +// .NET Core/.NET DateOnly? ToDateOnly( ReadOnlySpan format, - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.DateOnly, CultureInfo? cultureInfo = null); DateOnly? ToDateOnly( string[] formats, - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.DateOnly, CultureInfo? cultureInfo = null); -// .NET 6+: TimeOnly. +// TimeOnly: .NET 6+ TimeOnly? ToTimeOnly( - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.TimeOnly, CultureInfo? cultureInfo = null); TimeOnly? ToTimeOnly( string format, - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.TimeOnly, CultureInfo? cultureInfo = null); -// .NET Core/.NET only. +// .NET Core/.NET TimeOnly? ToTimeOnly( ReadOnlySpan format, - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.TimeOnly, CultureInfo? cultureInfo = null); TimeOnly? ToTimeOnly( string[] formats, - DateTimeStyles dateTimeStyles = DateTimeStyles.None, + DateTimeStyles style = Styles.TimeOnly, CultureInfo? cultureInfo = null); ``` diff --git a/Src/CsvLINQPadDriver/CodeGen/CsvCSharpCodeGenerator.cs b/Src/CsvLINQPadDriver/CodeGen/CsvCSharpCodeGenerator.cs index e1674bc..d7e1c02 100644 --- a/Src/CsvLINQPadDriver/CodeGen/CsvCSharpCodeGenerator.cs +++ b/Src/CsvLINQPadDriver/CodeGen/CsvCSharpCodeGenerator.cs @@ -296,7 +296,7 @@ public static string GetCodeRowClassName(this CsvTable table) static string ToClassName(string? name) => string.IsNullOrWhiteSpace(name) - ? throw new NullReferenceException("Name is null or empty") + ? throw new NullReferenceException("Class name is null or whitespace") : $"R{(name!.Length < 3 ? name : name.Singularize())}"; } diff --git a/Src/CsvLINQPadDriver/Directory.Build.props b/Src/CsvLINQPadDriver/Directory.Build.props index 98cc229..c3e451d 100644 --- a/Src/CsvLINQPadDriver/Directory.Build.props +++ b/Src/CsvLINQPadDriver/Directory.Build.props @@ -1,7 +1,8 @@  7.3.13 - .NET 7 build. + .NET 7 build. +More numeric conversion extension methods. diff --git a/Src/CsvLINQPadDriver/Extensions/FileExtensions.cs b/Src/CsvLINQPadDriver/Extensions/FileExtensions.cs index 91af2aa..2dab617 100644 --- a/Src/CsvLINQPadDriver/Extensions/FileExtensions.cs +++ b/Src/CsvLINQPadDriver/Extensions/FileExtensions.cs @@ -477,7 +477,6 @@ public static IEnumerable OrderFiles(this IEnumerable files, Fil var fileInfos = files.Select(static file => new FileInfo(file)); - // ReSharper disable once SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault fileInfos = filesOrderBy switch { FilesOrderBy.NameAsc => fileInfos.OrderBy(static fileInfo => fileInfo.Name, FileNameComparer), @@ -594,10 +593,10 @@ public record DeduceFileOrFolderResult(bool IsFile, string Path); public static DeduceFileOrFolderResult DeduceIsFileOrFolder(this string path, bool removeMask = false) => MatchDeduceIsFileOrFolderRegex().IsMatch(path) - ? new DeduceFileOrFolderResult(false, path) + ? new (false, path) : DeduceIsFileOrFolderRegex().IsMatch(Path.GetFileName(path)) - ? new DeduceFileOrFolderResult(true, removeMask ? Path.GetDirectoryName(path) ?? path : path) - : new DeduceFileOrFolderResult(SupportedFileExtensions.Contains(Path.GetExtension(path)), path); + ? new (true, removeMask ? Path.GetDirectoryName(path) ?? path : path) + : new (SupportedFileExtensions.Contains(Path.GetExtension(path)), path); public static void Add(this ICollection? exceptions, string file, Exception exception) => exceptions?.Add(file, $"processing failed: {exception.Message}"); diff --git a/Src/CsvLINQPadDriver/Extensions/StringExtensions.cs b/Src/CsvLINQPadDriver/Extensions/StringExtensions.cs index 2e4e176..1e7fe27 100644 --- a/Src/CsvLINQPadDriver/Extensions/StringExtensions.cs +++ b/Src/CsvLINQPadDriver/Extensions/StringExtensions.cs @@ -10,104 +10,207 @@ namespace CsvLINQPadDriver.Extensions { public static class StringExtensions { + public static class Styles + { + public const NumberStyles Integer = NumberStyles.Integer | NumberStyles.AllowThousands; + public const NumberStyles Float = NumberStyles.Float | NumberStyles.AllowThousands; + public const NumberStyles Decimal = NumberStyles.Number; + + public const DateTimeStyles DateTime = DateTimeStyles.None; + public const DateTimeStyles DateTimeOffset = DateTimeStyles.None; + public const DateTimeStyles UtcDateTime = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal; + public const TimeSpanStyles TimeSpan = TimeSpanStyles.None; + +#if NET6_0_OR_GREATER + public const DateTimeStyles DateOnly = DateTimeStyles.None; + public const DateTimeStyles TimeOnly = DateTimeStyles.None; +#endif + } + public static readonly CultureInfo DefaultCultureInfo = CultureInfo.InvariantCulture; - public static int? ToInt(this string? str, CultureInfo? cultureInfo = null) => - GetValueOrNull(int.TryParse(str, NumberStyles.Integer, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + public static sbyte? ToSByte(this string? str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(sbyte.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + +#if NETCOREAPP + public static sbyte? ToSByte(this ReadOnlySpan str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(sbyte.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); +#endif + + public static byte? ToByte(this string? str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(byte.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + +#if NETCOREAPP + public static byte? ToByte(this ReadOnlySpan str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(byte.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); +#endif + + public static short? ToShort(this string? str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(short.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #if NETCOREAPP - public static int? ToInt(this ReadOnlySpan str, CultureInfo? cultureInfo = null) => - GetValueOrNull(int.TryParse(str, NumberStyles.Integer, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + public static short? ToShort(this ReadOnlySpan str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(short.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #endif - public static long? ToLong(this string? str, CultureInfo? cultureInfo = null) => - GetValueOrNull(long.TryParse(str, NumberStyles.Integer, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + public static ushort? ToUShort(this string? str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(ushort.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #if NETCOREAPP - public static long? ToLong(this ReadOnlySpan str, CultureInfo? cultureInfo = null) => - GetValueOrNull(long.TryParse(str, NumberStyles.Integer, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + public static ushort? ToUShort(this ReadOnlySpan str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(ushort.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #endif - public static float? ToFloat(this string? str, CultureInfo? cultureInfo = null) => - GetValueOrNull(float.TryParse(str, NumberStyles.Float | NumberStyles.AllowThousands, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + public static int? ToInt(this string? str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(int.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #if NETCOREAPP - public static float? ToFloat(this ReadOnlySpan str, CultureInfo? cultureInfo = null) => - GetValueOrNull(float.TryParse(str, NumberStyles.Float | NumberStyles.AllowThousands, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + public static int? ToInt(this ReadOnlySpan str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(int.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #endif - public static double? ToDouble(this string? str, CultureInfo? cultureInfo = null) => - GetValueOrNull(double.TryParse(str, NumberStyles.Float | NumberStyles.AllowThousands, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + public static uint? ToUInt(this string? str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(uint.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #if NETCOREAPP - public static double? ToDouble(this ReadOnlySpan str, CultureInfo? cultureInfo = null) => - GetValueOrNull(double.TryParse(str, NumberStyles.Float | NumberStyles.AllowThousands, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + public static uint? ToUInt(this ReadOnlySpan str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(uint.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #endif - public static decimal? ToDecimal(this string? str, CultureInfo? cultureInfo = null) => - GetValueOrNull(decimal.TryParse(str, NumberStyles.Number, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + public static long? ToLong(this string? str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(long.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #if NETCOREAPP - public static decimal? ToDecimal(this ReadOnlySpan str, CultureInfo? cultureInfo = null) => - GetValueOrNull(decimal.TryParse(str, NumberStyles.Number, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + public static long? ToLong(this ReadOnlySpan str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(long.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #endif - public static DateTime? ToDateTime(this string? str, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTime.TryParse(str, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static ulong? ToULong(this string? str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(ulong.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #if NETCOREAPP - public static DateTime? ToDateTime(this ReadOnlySpan str, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTime.TryParse(str, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static ulong? ToULong(this ReadOnlySpan str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(ulong.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); +#endif + +#if NET5_0_OR_GREATER + public static nint? ToNInt(this string? str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(nint.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + +#if NET6_0_OR_GREATER + public static nint? ToNInt(this ReadOnlySpan str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(nint.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); +#endif + + public static nuint? ToNUInt(this string? str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(nuint.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + +#if NET6_0_OR_GREATER + public static nuint? ToNUInt(this ReadOnlySpan str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(nuint.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); +#endif #endif - public static DateTime? ToDateTime(this string? str, string format, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTime.TryParseExact(str, format, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static float? ToFloat(this string? str, NumberStyles style = Styles.Float, CultureInfo? cultureInfo = null) => + GetValueOrNull(float.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #if NETCOREAPP - public static DateTime? ToDateTime(this ReadOnlySpan str, ReadOnlySpan format, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTime.TryParseExact(str, format, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static float? ToFloat(this ReadOnlySpan str, NumberStyles style = Styles.Float, CultureInfo? cultureInfo = null) => + GetValueOrNull(float.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #endif - public static DateTime? ToDateTime(this string? str, string[] formats, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTime.TryParseExact(str, formats, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static double? ToDouble(this string? str, NumberStyles style = Styles.Float, CultureInfo? cultureInfo = null) => + GetValueOrNull(double.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #if NETCOREAPP - public static DateTime? ToDateTime(this ReadOnlySpan str, string[] formats, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTime.TryParseExact(str, formats, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static double? ToDouble(this ReadOnlySpan str, NumberStyles style = Styles.Float, CultureInfo? cultureInfo = null) => + GetValueOrNull(double.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #endif - private const DateTimeStyles UtcDateTimeStyles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal; + public static decimal? ToDecimal(this string? str, NumberStyles style = Styles.Decimal, CultureInfo? cultureInfo = null) => + GetValueOrNull(decimal.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + +#if NETCOREAPP + public static decimal? ToDecimal(this ReadOnlySpan str, NumberStyles style = Styles.Decimal, CultureInfo? cultureInfo = null) => + GetValueOrNull(decimal.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); +#endif + +#if NET5_0_OR_GREATER + public static Half? ToHalf(this string? str, NumberStyles style = Styles.Float, CultureInfo? cultureInfo = null) => + GetValueOrNull(Half.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + + public static Half? ToHalf(this ReadOnlySpan str, NumberStyles style = Styles.Float, CultureInfo? cultureInfo = null) => + GetValueOrNull(Half.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); +#endif + +#if NET7_0_OR_GREATER + public static Int128? ToInt128(this string? str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(Int128.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + + public static Int128? ToInt128(this ReadOnlySpan str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(Int128.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + + public static UInt128? ToUInt128(this string? str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(UInt128.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); + + public static UInt128? ToUInt128(this ReadOnlySpan str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) => + GetValueOrNull(UInt128.TryParse(str, style, SelectCulture(cultureInfo), out var parsedValue), parsedValue); +#endif + + public static DateTime? ToDateTime(this string? str, DateTimeStyles style = Styles.DateTime, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTime.TryParse(str, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); + +#if NETCOREAPP + public static DateTime? ToDateTime(this ReadOnlySpan str, DateTimeStyles style = Styles.DateTime, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTime.TryParse(str, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); +#endif + + public static DateTime? ToDateTime(this string? str, string format, DateTimeStyles style = Styles.DateTime, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTime.TryParseExact(str, format, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); + +#if NETCOREAPP + public static DateTime? ToDateTime(this ReadOnlySpan str, ReadOnlySpan format, DateTimeStyles style = Styles.DateTime, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTime.TryParseExact(str, format, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); +#endif + + public static DateTime? ToDateTime(this string? str, string[] formats, DateTimeStyles style = Styles.DateTime, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTime.TryParseExact(str, formats, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); + +#if NETCOREAPP + public static DateTime? ToDateTime(this ReadOnlySpan str, string[] formats, DateTimeStyles style = Styles.DateTime, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTime.TryParseExact(str, formats, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); +#endif - public static DateTime? ToUtcDateTime(this string? str, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTime.TryParse(str, SelectCulture(cultureInfo), UtcDateTimeStyles, out var parsedValue), parsedValue); + public static DateTime? ToUtcDateTime(this string? str, DateTimeStyles style = Styles.UtcDateTime, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTime.TryParse(str, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); #if NETCOREAPP - public static DateTime? ToUtcDateTime(this ReadOnlySpan str, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTime.TryParse(str, SelectCulture(cultureInfo), UtcDateTimeStyles, out var parsedValue), parsedValue); + public static DateTime? ToUtcDateTime(this ReadOnlySpan str, DateTimeStyles style = Styles.UtcDateTime, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTime.TryParse(str, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); #endif - public static DateTimeOffset? ToDateTimeOffset(this string? str, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTimeOffset.TryParse(str, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static DateTimeOffset? ToDateTimeOffset(this string? str, DateTimeStyles style = Styles.DateTimeOffset, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTimeOffset.TryParse(str, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); #if NETCOREAPP - public static DateTimeOffset? ToDateTimeOffset(this ReadOnlySpan str, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTimeOffset.TryParse(str, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static DateTimeOffset? ToDateTimeOffset(this ReadOnlySpan str, DateTimeStyles style = Styles.DateTimeOffset, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTimeOffset.TryParse(str, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); #endif - public static DateTimeOffset? ToDateTimeOffset(this string? str, string format, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTimeOffset.TryParseExact(str, format, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static DateTimeOffset? ToDateTimeOffset(this string? str, string format, DateTimeStyles style = Styles.DateTimeOffset, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTimeOffset.TryParseExact(str, format, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); #if NETCOREAPP - public static DateTimeOffset? ToDateTimeOffset(this ReadOnlySpan str, ReadOnlySpan format, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTimeOffset.TryParseExact(str, format, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static DateTimeOffset? ToDateTimeOffset(this ReadOnlySpan str, ReadOnlySpan format, DateTimeStyles style = Styles.DateTimeOffset, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTimeOffset.TryParseExact(str, format, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); #endif - public static DateTimeOffset? ToDateTimeOffset(this string? str, string[] formats, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTimeOffset.TryParseExact(str, formats, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static DateTimeOffset? ToDateTimeOffset(this string? str, string[] formats, DateTimeStyles style = Styles.DateTimeOffset, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTimeOffset.TryParseExact(str, formats, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); #if NETCOREAPP - public static DateTimeOffset? ToDateTimeOffset(this ReadOnlySpan str, string[] formats, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateTimeOffset.TryParseExact(str, formats, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static DateTimeOffset? ToDateTimeOffset(this ReadOnlySpan str, string[] formats, DateTimeStyles style = Styles.DateTimeOffset, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateTimeOffset.TryParseExact(str, formats, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); #endif public static TimeSpan? ToTimeSpan(this string? str, CultureInfo? cultureInfo = null) => @@ -118,58 +221,58 @@ public static class StringExtensions GetValueOrNull(TimeSpan.TryParse(str, SelectCulture(cultureInfo), out var parsedValue), parsedValue); #endif - public static TimeSpan? ToTimeSpan(this string? str, string format, TimeSpanStyles timeSpanStyles = TimeSpanStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(TimeSpan.TryParseExact(str, format, SelectCulture(cultureInfo), timeSpanStyles, out var parsedValue), parsedValue); + public static TimeSpan? ToTimeSpan(this string? str, string format, TimeSpanStyles style = Styles.TimeSpan, CultureInfo? cultureInfo = null) => + GetValueOrNull(TimeSpan.TryParseExact(str, format, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); #if NETCOREAPP - public static TimeSpan? ToTimeSpan(this ReadOnlySpan str, ReadOnlySpan format, TimeSpanStyles timeSpanStyles = TimeSpanStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(TimeSpan.TryParseExact(str, format, SelectCulture(cultureInfo), timeSpanStyles, out var parsedValue), parsedValue); + public static TimeSpan? ToTimeSpan(this ReadOnlySpan str, ReadOnlySpan format, TimeSpanStyles style = Styles.TimeSpan, CultureInfo? cultureInfo = null) => + GetValueOrNull(TimeSpan.TryParseExact(str, format, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); #endif - public static TimeSpan? ToTimeSpan(this string? str, string[] formats, TimeSpanStyles timeSpanStyles = TimeSpanStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(TimeSpan.TryParseExact(str, formats, SelectCulture(cultureInfo), timeSpanStyles, out var parsedValue), parsedValue); + public static TimeSpan? ToTimeSpan(this string? str, string[] formats, TimeSpanStyles style = Styles.TimeSpan, CultureInfo? cultureInfo = null) => + GetValueOrNull(TimeSpan.TryParseExact(str, formats, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); #if NETCOREAPP - public static TimeSpan? ToTimeSpan(this ReadOnlySpan str, string[] formats, TimeSpanStyles timeSpanStyles = TimeSpanStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(TimeSpan.TryParseExact(str, formats, SelectCulture(cultureInfo), timeSpanStyles, out var parsedValue), parsedValue); + public static TimeSpan? ToTimeSpan(this ReadOnlySpan str, string[] formats, TimeSpanStyles style = Styles.TimeSpan, CultureInfo? cultureInfo = null) => + GetValueOrNull(TimeSpan.TryParseExact(str, formats, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); #endif #if NET6_0_OR_GREATER - public static DateOnly? ToDateOnly(this string? str, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateOnly.TryParse(str, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static DateOnly? ToDateOnly(this string? str, DateTimeStyles style = Styles.DateOnly, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateOnly.TryParse(str, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); - public static DateOnly? ToDateOnly(this ReadOnlySpan str, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateOnly.TryParse(str, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static DateOnly? ToDateOnly(this ReadOnlySpan str, DateTimeStyles style = Styles.DateOnly, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateOnly.TryParse(str, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); - public static DateOnly? ToDateOnly(this string? str, string format, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateOnly.TryParseExact(str, format, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static DateOnly? ToDateOnly(this string? str, string format, DateTimeStyles style = Styles.DateOnly, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateOnly.TryParseExact(str, format, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); - public static DateOnly? ToDateOnly(this ReadOnlySpan str, ReadOnlySpan format, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateOnly.TryParseExact(str, format, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static DateOnly? ToDateOnly(this ReadOnlySpan str, ReadOnlySpan format, DateTimeStyles style = Styles.DateOnly, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateOnly.TryParseExact(str, format, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); - public static DateOnly? ToDateOnly(this string? str, string[] formats, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateOnly.TryParseExact(str, formats, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static DateOnly? ToDateOnly(this string? str, string[] formats, DateTimeStyles style = Styles.DateOnly, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateOnly.TryParseExact(str, formats, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); - public static DateOnly? ToDateOnly(this ReadOnlySpan str, string[] formats, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(DateOnly.TryParseExact(str, formats, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static DateOnly? ToDateOnly(this ReadOnlySpan str, string[] formats, DateTimeStyles style = Styles.DateOnly, CultureInfo? cultureInfo = null) => + GetValueOrNull(DateOnly.TryParseExact(str, formats, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); - public static TimeOnly? ToTimeOnly(this string? str, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(TimeOnly.TryParse(str, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static TimeOnly? ToTimeOnly(this string? str, DateTimeStyles style = Styles.TimeOnly, CultureInfo? cultureInfo = null) => + GetValueOrNull(TimeOnly.TryParse(str, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); - public static TimeOnly? ToTimeOnly(this ReadOnlySpan str, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(TimeOnly.TryParse(str, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static TimeOnly? ToTimeOnly(this ReadOnlySpan str, DateTimeStyles style = Styles.TimeOnly, CultureInfo? cultureInfo = null) => + GetValueOrNull(TimeOnly.TryParse(str, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); - public static TimeOnly? ToTimeOnly(this string? str, string format, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(TimeOnly.TryParseExact(str, format, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static TimeOnly? ToTimeOnly(this string? str, string format, DateTimeStyles style = Styles.TimeOnly, CultureInfo? cultureInfo = null) => + GetValueOrNull(TimeOnly.TryParseExact(str, format, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); - public static TimeOnly? ToTimeOnly(this ReadOnlySpan str, ReadOnlySpan format, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(TimeOnly.TryParseExact(str, format, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static TimeOnly? ToTimeOnly(this ReadOnlySpan str, ReadOnlySpan format, DateTimeStyles style = Styles.TimeOnly, CultureInfo? cultureInfo = null) => + GetValueOrNull(TimeOnly.TryParseExact(str, format, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); - public static TimeOnly? ToTimeOnly(this string? str, string[] formats, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(TimeOnly.TryParseExact(str, formats, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static TimeOnly? ToTimeOnly(this string? str, string[] formats, DateTimeStyles style = Styles.TimeOnly, CultureInfo? cultureInfo = null) => + GetValueOrNull(TimeOnly.TryParseExact(str, formats, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); - public static TimeOnly? ToTimeOnly(this ReadOnlySpan str, string[] formats, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) => - GetValueOrNull(TimeOnly.TryParseExact(str, formats, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue); + public static TimeOnly? ToTimeOnly(this ReadOnlySpan str, string[] formats, DateTimeStyles style = Styles.TimeOnly, CultureInfo? cultureInfo = null) => + GetValueOrNull(TimeOnly.TryParseExact(str, formats, SelectCulture(cultureInfo), style, out var parsedValue), parsedValue); #endif public static Guid? ToGuid(this string? str) => @@ -210,9 +313,9 @@ public static class StringExtensions } #endif - public static bool? ToBool(this string? str, CultureInfo? cultureInfo = null) + public static bool? ToBool(this string? str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) { - var longValue = str.ToLong(cultureInfo); + var longValue = str.ToLong(style, cultureInfo); return longValue.HasValue ? longValue.Value != 0 @@ -220,9 +323,9 @@ public static class StringExtensions } #if NETCOREAPP - public static bool? ToBool(this ReadOnlySpan str, CultureInfo? cultureInfo = null) + public static bool? ToBool(this ReadOnlySpan str, NumberStyles style = Styles.Integer, CultureInfo? cultureInfo = null) { - var longValue = str.ToLong(cultureInfo); + var longValue = str.ToLong(style, cultureInfo); return longValue.HasValue ? longValue.Value != 0 diff --git a/Tests/CsvLINQPadDriverTest/LPRun/Templates/Extensions.linq b/Tests/CsvLINQPadDriverTest/LPRun/Templates/Extensions.linq index 2e26477..87a4748 100644 --- a/Tests/CsvLINQPadDriverTest/LPRun/Templates/Extensions.linq +++ b/Tests/CsvLINQPadDriverTest/LPRun/Templates/Extensions.linq @@ -13,18 +13,94 @@ string nullString = null; invalidString.ToBool().Should().BeNull(Reason()); nullString.ToBool().Should().BeNull(Reason()); +// SByte. +((ReadOnlySpan)"1").ToSByte().Should().Be((sbyte)1, Reason()); +"1".ToSByte().Should().Be((sbyte)1, Reason()); +invalidString.ToSByte().Should().BeNull(Reason()); +nullString.ToSByte().Should().BeNull(Reason()); + +// Byte. +((ReadOnlySpan)"1").ToByte().Should().Be((byte)1, Reason()); +"1".ToByte().Should().Be((byte)1, Reason()); +invalidString.ToByte().Should().BeNull(Reason()); +nullString.ToByte().Should().BeNull(Reason()); + +// Short. +((ReadOnlySpan)"1").ToShort().Should().Be((short)1, Reason()); +"1".ToShort().Should().Be((short)1, Reason()); +invalidString.ToShort().Should().BeNull(Reason()); +nullString.ToShort().Should().BeNull(Reason()); + +// UShort. +((ReadOnlySpan)"1").ToUShort().Should().Be((ushort)1, Reason()); +"1".ToUShort().Should().Be((ushort)1, Reason()); +invalidString.ToUShort().Should().BeNull(Reason()); +nullString.ToUShort().Should().BeNull(Reason()); + // Int. ((ReadOnlySpan)"1").ToInt().Should().Be(1, Reason()); "1".ToInt().Should().Be(1, Reason()); invalidString.ToInt().Should().BeNull(Reason()); nullString.ToInt().Should().BeNull(Reason()); +// UInt. +((ReadOnlySpan)"1").ToUInt().Should().Be(1u, Reason()); +"1".ToUInt().Should().Be(1u, Reason()); +invalidString.ToUInt().Should().BeNull(Reason()); +nullString.ToUInt().Should().BeNull(Reason()); + // Long. ((ReadOnlySpan)"1").ToLong().Should().Be(1L, Reason()); "1".ToLong().Should().Be(1L, Reason()); invalidString.ToLong().Should().BeNull(Reason()); nullString.ToLong().Should().BeNull(Reason()); +// ULong. +((ReadOnlySpan)"1").ToULong().Should().Be(1UL, Reason()); +"1".ToULong().Should().Be(1UL, Reason()); +invalidString.ToULong().Should().BeNull(Reason()); +nullString.ToULong().Should().BeNull(Reason()); + +#if NET5_0_OR_GREATER +// NInt. +#if NET6_0_OR_GREATER +((ReadOnlySpan)"1").ToNInt().Should().Be((nint)1, Reason()); +#endif +"1".ToNInt().Should().Be((nint)1, Reason()); +invalidString.ToNInt().Should().BeNull(Reason()); +nullString.ToNInt().Should().BeNull(Reason()); + +// NUInt. +#if NET6_0_OR_GREATER +((ReadOnlySpan)"1").ToNUInt().Should().Be((nuint)1, Reason()); +#endif +"1".ToNUInt().Should().Be((nuint)1, Reason()); +invalidString.ToNUInt().Should().BeNull(Reason()); +nullString.ToNUInt().Should().BeNull(Reason()); +#endif + +#if NET7_0_OR_GREATER +// Int128. +((ReadOnlySpan)"1").ToInt128().Should().Be((Int128)1, Reason()); +"1".ToInt128().Should().Be((Int128)1, Reason()); +invalidString.ToInt128().Should().BeNull(Reason()); +nullString.ToInt128().Should().BeNull(Reason()); + +// UInt128. +((ReadOnlySpan)"1").ToUInt128().Should().Be((UInt128)1, Reason()); +"1".ToUInt128().Should().Be((UInt128)1, Reason()); +invalidString.ToUInt128().Should().BeNull(Reason()); +nullString.ToUInt128().Should().BeNull(Reason()); +#endif + +#if NET5_0_OR_GREATER +// Half. +((ReadOnlySpan)"1.23").ToHalf().Should().Be((Half)1.23, Reason()); +"1.23".ToHalf().Should().Be((Half)1.23, Reason()); +invalidString.ToHalf().Should().BeNull(Reason()); +nullString.ToHalf().Should().BeNull(Reason()); +#endif + // Float. ((ReadOnlySpan)"1.23").ToFloat().Should().Be(1.23f, Reason()); "1.23".ToFloat().Should().Be(1.23f, Reason()); diff --git a/Tests/CsvLINQPadDriverTest/LPRunTests.cs b/Tests/CsvLINQPadDriverTest/LPRunTests.cs index e5074b2..1def8ea 100644 --- a/Tests/CsvLINQPadDriverTest/LPRunTests.cs +++ b/Tests/CsvLINQPadDriverTest/LPRunTests.cs @@ -145,8 +145,14 @@ IEnumerable> GetTestData() linqFile, noContext, defaultCsvDataContextDriverProperties +#if NET5_0_OR_GREATER + , "NET5_0_OR_GREATER" +#endif #if NET6_0_OR_GREATER , "NET6_0_OR_GREATER" +#endif +#if NET7_0_OR_GREATER + , "NET7_0_OR_GREATER" #endif ));