Skip to content

Commit

Permalink
Add more numeric conversion extension methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
i2van committed Nov 11, 2022
1 parent 2795be6 commit 81dbbdd
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 135 deletions.
193 changes: 149 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,133 +361,238 @@ Authors.First()
Driver provides extension methods for converting `string` (and `ReadOnlySpan<char>` 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<char> 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<char> 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<char> 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<char> 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<char> 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<char> 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);
```

Expand Down
2 changes: 1 addition & 1 deletion Src/CsvLINQPadDriver/CodeGen/CsvCSharpCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())}";
}

Expand Down
3 changes: 2 additions & 1 deletion Src/CsvLINQPadDriver/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project>
<PropertyGroup>
<Version>7.3.13</Version>
<PackageReleaseNotes>.NET 7 build.</PackageReleaseNotes>
<PackageReleaseNotes>.NET 7 build.
More numeric conversion extension methods.</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup>
Expand Down
7 changes: 3 additions & 4 deletions Src/CsvLINQPadDriver/Extensions/FileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,6 @@ public static IEnumerable<string> OrderFiles(this IEnumerable<string> 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),
Expand Down Expand Up @@ -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<Exception>? exceptions, string file, Exception exception) =>
exceptions?.Add(file, $"processing failed: {exception.Message}");
Expand Down
Loading

0 comments on commit 81dbbdd

Please sign in to comment.