From 2ddfb46c31d349cb8fbc1c828ddfbf5b0cccf540 Mon Sep 17 00:00:00 2001 From: Tyrrrz Date: Sat, 17 Apr 2021 14:07:07 +0300 Subject: [PATCH] Add IFormatProvider to FileSize.TryParse(...) and PartitionLimit.Parse(...) --- .../Discord/Data/Common/FileSize.cs | 48 ----------------- .../Exporting/Partitioning/PartitionLimit.cs | 53 ++++++++++++++++--- 2 files changed, 46 insertions(+), 55 deletions(-) diff --git a/DiscordChatExporter.Core/Discord/Data/Common/FileSize.cs b/DiscordChatExporter.Core/Discord/Data/Common/FileSize.cs index 0335ceb..9f94156 100644 --- a/DiscordChatExporter.Core/Discord/Data/Common/FileSize.cs +++ b/DiscordChatExporter.Core/Discord/Data/Common/FileSize.cs @@ -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)); - } } } \ No newline at end of file diff --git a/DiscordChatExporter.Core/Exporting/Partitioning/PartitionLimit.cs b/DiscordChatExporter.Core/Exporting/Partitioning/PartitionLimit.cs index 3b8eceb..1a82259 100644 --- a/DiscordChatExporter.Core/Exporting/Partitioning/PartitionLimit.cs +++ b/DiscordChatExporter.Core/Exporting/Partitioning/PartitionLimit.cs @@ -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}'."); } } \ No newline at end of file