From 7bc1e8178abb1bb1cc5419219428e55a203fecec Mon Sep 17 00:00:00 2001 From: Jiri Kostiha Date: Thu, 10 Oct 2024 16:05:18 +0200 Subject: [PATCH] style: namespace without brackets --- src/Atin/AtinParser.cs | 229 ++++++++++++++++----------------- src/Atin/TimeSpanExtensions.cs | 99 +++++++------- 2 files changed, 163 insertions(+), 165 deletions(-) diff --git a/src/Atin/AtinParser.cs b/src/Atin/AtinParser.cs index 6225958..ab56ada 100644 --- a/src/Atin/AtinParser.cs +++ b/src/Atin/AtinParser.cs @@ -2,52 +2,121 @@ using System; using System.Globalization; -namespace Atin +namespace Atin; + +/// +/// A static class that provides methods to parse time span input strings. +/// +public static class AtinParser { /// - /// A static class that provides methods to parse time span input strings. + /// Regular expression pattern used to match time units (W, D, H, M, S) and corresponding quantities. + /// + private const string _regexPattern = @"([WwDdHhMmSs])(\d+)"; + + /// + /// Compiled regular expression based on the . + /// + private static readonly Regex _regex = new(_regexPattern, RegexOptions.Compiled); + + /// + /// Parses a time string and returns the equivalent object. /// - public static class AtinParser + /// A string representing time intervals (e.g., "2W3D4H"). + /// A representing the total time. + /// Thrown when the input is null. + /// Thrown when the input format is invalid or the last character is not a digit. + public static TimeSpan Parse(string input) { - /// - /// Regular expression pattern used to match time units (W, D, H, M, S) and corresponding quantities. - /// - private const string _regexPattern = @"([WwDdHhMmSs])(\d+)"; - - /// - /// Compiled regular expression based on the . - /// - private static readonly Regex _regex = new(_regexPattern, RegexOptions.Compiled); - - /// - /// Parses a time string and returns the equivalent object. - /// - /// A string representing time intervals (e.g., "2W3D4H"). - /// A representing the total time. - /// Thrown when the input is null. - /// Thrown when the input format is invalid or the last character is not a digit. - public static TimeSpan Parse(string input) + if (input is null) { - if (input is null) - { - throw new ArgumentNullException(nameof(input)); - } + throw new ArgumentNullException(nameof(input)); + } - if (input == string.Empty) - { - return TimeSpan.Zero; - } + if (input == string.Empty) + { + return TimeSpan.Zero; + } + + if (!char.IsDigit(input[input.Length - 1])) + { + throw new ArgumentException("The last character of the input must be a digit."); + } + + var matches = _regex.Matches(input); + + if (matches.Count == 0) + { + throw new ArgumentException("Input format is invalid."); + } + + int totalSeconds = 0; - if (!char.IsDigit(input[input.Length - 1])) + foreach (Match match in matches) + { + string unit = match.Groups[1].Value; + int quantity = int.Parse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture); + + switch (unit) { - throw new ArgumentException("The last character of the input must be a digit."); + case "W": + totalSeconds += quantity * 604800; // Convert weeks to seconds (7 days) + break; + case "D": + totalSeconds += quantity * 86400; // Convert days to seconds (24 hours) + break; + case "H": + totalSeconds += quantity * 3600; // Convert hours to seconds (60 minutes) + break; + case "M": + totalSeconds += quantity * 60; // Convert minutes to seconds + break; + case "S": + totalSeconds += quantity; // Seconds + break; + default: + throw new ArgumentException($"Invalid time unit: {unit}"); } + } + + return TimeSpan.FromSeconds(totalSeconds); + } + + /// + /// Tries to parse the time string and returns a boolean indicating success or failure. + /// + /// A string representing time intervals (e.g., "2W3D4H"). + /// The output if parsing succeeds. + /// true if the input string was successfully parsed; otherwise, false. + public static bool TryParse(string input, out TimeSpan result) + { + if (input is null) + { + result = TimeSpan.Zero; + return false; + } + + if (input == string.Empty) + { + result = TimeSpan.Zero; + return true; + } + if (!char.IsDigit(input[input.Length - 1])) + { + result = TimeSpan.Zero; + return false; + } + + result = TimeSpan.Zero; + + try + { var matches = _regex.Matches(input); if (matches.Count == 0) { - throw new ArgumentException("Input format is invalid."); + return false; } int totalSeconds = 0; @@ -55,7 +124,13 @@ public static TimeSpan Parse(string input) foreach (Match match in matches) { string unit = match.Groups[1].Value; - int quantity = int.Parse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture); + int quantity; + + if (!int.TryParse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out quantity)) + { + result = TimeSpan.Zero; + return false; + } switch (unit) { @@ -75,94 +150,18 @@ public static TimeSpan Parse(string input) totalSeconds += quantity; // Seconds break; default: - throw new ArgumentException($"Invalid time unit: {unit}"); + result = TimeSpan.Zero; + return false; // Invalid time unit } } - return TimeSpan.FromSeconds(totalSeconds); + result = TimeSpan.FromSeconds(totalSeconds); + return true; } - - /// - /// Tries to parse the time string and returns a boolean indicating success or failure. - /// - /// A string representing time intervals (e.g., "2W3D4H"). - /// The output if parsing succeeds. - /// true if the input string was successfully parsed; otherwise, false. - public static bool TryParse(string input, out TimeSpan result) + catch { - if (input is null) - { - result = TimeSpan.Zero; - return false; - } - - if (input == string.Empty) - { - result = TimeSpan.Zero; - return true; - } - - if (!char.IsDigit(input[input.Length - 1])) - { - result = TimeSpan.Zero; - return false; - } - result = TimeSpan.Zero; - - try - { - var matches = _regex.Matches(input); - - if (matches.Count == 0) - { - return false; - } - - int totalSeconds = 0; - - foreach (Match match in matches) - { - string unit = match.Groups[1].Value; - int quantity; - - if (!int.TryParse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out quantity)) - { - result = TimeSpan.Zero; - return false; - } - - switch (unit) - { - case "W": - totalSeconds += quantity * 604800; // Convert weeks to seconds (7 days) - break; - case "D": - totalSeconds += quantity * 86400; // Convert days to seconds (24 hours) - break; - case "H": - totalSeconds += quantity * 3600; // Convert hours to seconds (60 minutes) - break; - case "M": - totalSeconds += quantity * 60; // Convert minutes to seconds - break; - case "S": - totalSeconds += quantity; // Seconds - break; - default: - result = TimeSpan.Zero; - return false; // Invalid time unit - } - } - - result = TimeSpan.FromSeconds(totalSeconds); - return true; - } - catch - { - result = TimeSpan.Zero; - return false; - } + return false; } } } diff --git a/src/Atin/TimeSpanExtensions.cs b/src/Atin/TimeSpanExtensions.cs index fc0f8f9..6591b55 100644 --- a/src/Atin/TimeSpanExtensions.cs +++ b/src/Atin/TimeSpanExtensions.cs @@ -1,70 +1,69 @@ using System; using System.Text; -namespace Atin +namespace Atin; + +/// +/// Provides extension methods for the class. +/// +public static class TimeSpanExtensions { /// - /// Provides extension methods for the class. + /// Converts a object to a custom formatted string in the "Atin" format. + /// The format includes weeks (W), days (D), hours (H), minutes (M), and seconds (S). /// - public static class TimeSpanExtensions + /// The instance to convert. + /// A string representing the time span in the "Atin" format (e.g., "W2D3H4M5S"). + public static string ToAtin(this TimeSpan timeSpan) { - /// - /// Converts a object to a custom formatted string in the "Atin" format. - /// The format includes weeks (W), days (D), hours (H), minutes (M), and seconds (S). - /// - /// The instance to convert. - /// A string representing the time span in the "Atin" format (e.g., "W2D3H4M5S"). - public static string ToAtin(this TimeSpan timeSpan) - { - int totalSeconds = (int)timeSpan.TotalSeconds; + int totalSeconds = (int)timeSpan.TotalSeconds; - // Calculate weeks, days, hours, minutes, and seconds from the total seconds. - int weeks = totalSeconds / 604800; // 604800 seconds in a week - totalSeconds %= 604800; + // Calculate weeks, days, hours, minutes, and seconds from the total seconds. + int weeks = totalSeconds / 604800; // 604800 seconds in a week + totalSeconds %= 604800; - int days = totalSeconds / 86400; // 86400 seconds in a day - totalSeconds %= 86400; + int days = totalSeconds / 86400; // 86400 seconds in a day + totalSeconds %= 86400; - int hours = totalSeconds / 3600; // 3600 seconds in an hour - totalSeconds %= 3600; + int hours = totalSeconds / 3600; // 3600 seconds in an hour + totalSeconds %= 3600; - int minutes = totalSeconds / 60; // 60 seconds in a minute - int seconds = totalSeconds % 60; // remaining seconds + int minutes = totalSeconds / 60; // 60 seconds in a minute + int seconds = totalSeconds % 60; // remaining seconds - // Use StringBuilder to construct the output string. - var result = new StringBuilder(); + // Use StringBuilder to construct the output string. + var result = new StringBuilder(); - // Append weeks, if any. - if (weeks > 0) - { - result.Append($"W{weeks}"); - } - - // Append days, if any. - if (days > 0) - { - result.Append($"D{days}"); - } + // Append weeks, if any. + if (weeks > 0) + { + result.Append($"W{weeks}"); + } - // Append hours, if any. - if (hours > 0) - { - result.Append($"H{hours}"); - } + // Append days, if any. + if (days > 0) + { + result.Append($"D{days}"); + } - // Append minutes, if any. - if (minutes > 0) - { - result.Append($"M{minutes}"); - } + // Append hours, if any. + if (hours > 0) + { + result.Append($"H{hours}"); + } - // Append seconds or default to seconds if no other units were added. - if (seconds > 0 || result.Length == 0) // Always include seconds if no other units were added. - { - result.Append($"S{seconds}"); - } + // Append minutes, if any. + if (minutes > 0) + { + result.Append($"M{minutes}"); + } - return result.ToString(); + // Append seconds or default to seconds if no other units were added. + if (seconds > 0 || result.Length == 0) // Always include seconds if no other units were added. + { + result.Append($"S{seconds}"); } + + return result.ToString(); } }