Add IFormatProvider to FileSize.TryParse(...) and PartitionLimit.Parse(...)

pull/558/head
Tyrrrz 4 years ago
parent 3c5beeba79
commit 2ddfb46c31

@ -1,6 +1,4 @@
using System;
using System.Globalization;
using System.Text.RegularExpressions;
namespace DiscordChatExporter.Core.Discord.Data.Common
{
@ -12,19 +10,11 @@ namespace DiscordChatExporter.Core.Discord.Data.Common
public double TotalKiloBytes => TotalBytes / 1024.0;
public double TotalMegaBytes => TotalKiloBytes / 1024.0;
public double TotalGigaBytes => TotalMegaBytes / 1024.0;
public double TotalTeraBytes => TotalGigaBytes / 1024.0;
public double TotalPetaBytes => TotalTeraBytes / 1024.0;
public FileSize(long bytes) => TotalBytes = bytes;
private double GetLargestWholeNumberValue()
{
if (Math.Abs(TotalPetaBytes) >= 1)
return TotalPetaBytes;
if (Math.Abs(TotalTeraBytes) >= 1)
return TotalTeraBytes;
if (Math.Abs(TotalGigaBytes) >= 1)
return TotalGigaBytes;
@ -39,12 +29,6 @@ namespace DiscordChatExporter.Core.Discord.Data.Common
private string GetLargestWholeNumberSymbol()
{
if (Math.Abs(TotalPetaBytes) >= 1)
return "PB";
if (Math.Abs(TotalTeraBytes) >= 1)
return "TB";
if (Math.Abs(TotalGigaBytes) >= 1)
return "GB";
@ -63,37 +47,5 @@ namespace DiscordChatExporter.Core.Discord.Data.Common
public partial struct FileSize
{
public static FileSize FromBytes(long bytes) => new(bytes);
public static FileSize? TryParse(string value)
{
var match = Regex.Match(value, @"^(\d+[\.,]?\d*)\s*(\w)?b$", RegexOptions.IgnoreCase);
// Number part
if (!double.TryParse(
match.Groups[1].Value,
NumberStyles.Float,
CultureInfo.InvariantCulture,
out var number))
{
return null;
}
// Magnitude part
var magnitude = match.Groups[2].Value.ToUpperInvariant() switch
{
"G" => 1_000_000_000,
"M" => 1_000_000,
"K" => 1_000,
"" => 1,
_ => -1
};
if (magnitude < 0)
{
return null;
}
return FromBytes((long) (number * magnitude));
}
}
}

@ -1,4 +1,6 @@
using DiscordChatExporter.Core.Discord.Data.Common;
using System;
using System.Globalization;
using System.Text.RegularExpressions;
namespace DiscordChatExporter.Core.Exporting.Partitioning
{
@ -9,14 +11,51 @@ namespace DiscordChatExporter.Core.Exporting.Partitioning
public partial class PartitionLimit
{
public static PartitionLimit Parse(string value)
private static long? TryParseFileSizeBytes(string value, IFormatProvider? formatProvider = null)
{
var fileSize = FileSize.TryParse(value);
if (fileSize is not null)
return new FileSizePartitionLimit(fileSize.Value.TotalBytes);
var match = Regex.Match(value, @"^\s*(\d+[\.,]?\d*)\s*(\w)?b\s*$", RegexOptions.IgnoreCase);
var messageCount = int.Parse(value);
return new MessageCountPartitionLimit(messageCount);
// Number part
if (!double.TryParse(
match.Groups[1].Value,
NumberStyles.Float,
formatProvider,
out var number))
{
return null;
}
// Magnitude part
var magnitude = match.Groups[2].Value.ToUpperInvariant() switch
{
"G" => 1_000_000_000,
"M" => 1_000_000,
"K" => 1_000,
"" => 1,
_ => -1
};
if (magnitude < 0)
{
return null;
}
return (long) (number * magnitude);
}
public static PartitionLimit? TryParse(string value, IFormatProvider? formatProvider = null)
{
var fileSizeLimit = TryParseFileSizeBytes(value, formatProvider);
if (fileSizeLimit is not null)
return new FileSizePartitionLimit(fileSizeLimit.Value);
if (int.TryParse(value, NumberStyles.Integer, formatProvider, out var messageCountLimit))
return new MessageCountPartitionLimit(messageCountLimit);
return null;
}
public static PartitionLimit Parse(string value, IFormatProvider? formatProvider = null) =>
TryParse(value, formatProvider) ?? throw new FormatException($"Invalid partition limit '{value}'.");
}
}
Loading…
Cancel
Save