diff --git a/src/Nox.Cron/Nox.Cron.csproj b/src/Nox.Cron/Nox.Cron.csproj index 5cfc5bd..9d3fbdd 100644 --- a/src/Nox.Cron/Nox.Cron.csproj +++ b/src/Nox.Cron/Nox.Cron.csproj @@ -1,12 +1,13 @@ - net8.0 - enable + netstandard2.0;net7.0 + 11.0 + disable enable Nox.Cron - 8.0.0.0 - 8.0.0.0 - 8.0.0 + 7.0.1 + 7.0.1 + 7.0.1 Andre Sharpe Creations Unlimited English Cron Extension String Convert diff --git a/src/Nox.Cron/Parser/CronParser.cs b/src/Nox.Cron/Parser/CronParser.cs index 8d5e4f1..45381c7 100644 --- a/src/Nox.Cron/Parser/CronParser.cs +++ b/src/Nox.Cron/Parser/CronParser.cs @@ -1,4 +1,7 @@ -using System.Text; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; namespace Nox.Cron { @@ -46,7 +49,7 @@ public static CronSchedule ToCronExpression(this string englishPhrase) // keep lowercase alpha, numbers, colon and plus - ignore rest for (var i = 0; i < sbPhrase.Length; i++) { - if (!" +:0123456789abcdefghijklmnopqrstuvwxyz".Contains(sbPhrase[i])) + if (" +:0123456789abcdefghijklmnopqrstuvwxyz".IndexOf(sbPhrase[i]) == -1) sbPhrase[i] = '_'; } sbPhrase.Replace("_", ""); @@ -74,7 +77,8 @@ public static CronSchedule ToCronExpression(this string englishPhrase) // Replace synonymns var words = sbPhrase.ToString() - .Split(' ', StringSplitOptions.RemoveEmptyEntries) + .Split(' ') + .Where(s => s.Length > 0) .Select(x => Synonymn(x)) .ToList(); @@ -86,7 +90,7 @@ public static CronSchedule ToCronExpression(this string englishPhrase) } // if there is a date implied, assume first phrase is a time and vice versa - if (!words[0].StartsWith('[')) + if (!words[0].StartsWith("[")) { if (words.Contains("[T]") && !words.Contains("[D]")) words.Insert(0, "[D]"); @@ -104,7 +108,7 @@ public static CronSchedule ToCronExpression(this string englishPhrase) var everyWordCount = 0; for (var i = everyStartPos; i < words.Count; i++) { - if (words[i].StartsWith('[')) break; + if (words[i].StartsWith("[")) break; everyWordCount++; sbEvery.Append(words[i]); sbEvery.Append(' '); @@ -160,8 +164,7 @@ public static CronSchedule ToCronExpression(this string englishPhrase) var everyParts = every.Split(' ').Reverse().ToArray(); var everyPartString = "*/"+ - string.Join(',', - everyParts.Skip(1).Reverse().Skip(1).ToArray() + string.Join(",", everyParts.Skip(1).Reverse().Skip(1).ToArray() ); isEveryHandled = true; @@ -342,14 +345,15 @@ public static CronSchedule ToCronExpression(this string englishPhrase) // handle anything unparsed that may be a time or day-of-month - words = string.Join(' ', words.ToArray()) + words = string.Join(" ", words.ToArray()) .Trim() .Replace("[D]","_") .Replace("[T]","_") - .Split('_', StringSplitOptions.RemoveEmptyEntries) + .Split('_') .Select(s => s.Trim()) .Where(s => s.Length > 0) .ToList(); + for (var i = words.Count - 1; i > -1; i--) { @@ -370,10 +374,14 @@ public static CronSchedule ToCronExpression(this string englishPhrase) if (!string.IsNullOrWhiteSpace(words[i]) && words[i].All(c => Char.IsWhiteSpace(c) || Char.IsDigit(c))) { - var elements = words[i].Split(' ', StringSplitOptions.RemoveEmptyEntries); - schedule.DayOfMonth = string.Join(',',elements); + var elements = words[i] + .Split(' ') + .Select(s => s.Trim()) + .Where(s => s.Length > 0) + .ToList(); + + schedule.DayOfMonth = string.Join(",", elements); words.RemoveAt(i); - continue; } } @@ -395,7 +403,7 @@ public static CronSchedule ToCronExpression(this string englishPhrase) if (string.IsNullOrWhiteSpace(schedule.DayOfWeek)) schedule.DayOfWeek = "*"; - schedule.Unparsed = string.Join(' ',words.ToArray()).Trim(); + schedule.Unparsed = string.Join(" ",words.ToArray()).Trim(); // done diff --git a/src/Nox.Cron/Parser/CronSchedule.cs b/src/Nox.Cron/Parser/CronSchedule.cs index cc6752e..37bc9b1 100644 --- a/src/Nox.Cron/Parser/CronSchedule.cs +++ b/src/Nox.Cron/Parser/CronSchedule.cs @@ -6,12 +6,12 @@ public CronSchedule() { } - public string Minutes { get; init; } = string.Empty; - public string Hours { get; init; } = string.Empty; - public string DayOfMonth { get; init; } = string.Empty; - public string Months { get; init; } = string.Empty; - public string DayOfWeek { get; init; } = string.Empty; - public string Unparsed { get; init; } = string.Empty; + public string Minutes { get; set; } = string.Empty; + public string Hours { get; set; } = string.Empty; + public string DayOfMonth { get; set; } = string.Empty; + public string Months { get; set; } = string.Empty; + public string DayOfWeek { get; set; } = string.Empty; + public string Unparsed { get; set; } = string.Empty; public override string ToString() => $"{Minutes} {Hours} {DayOfMonth} {Months} {DayOfWeek}"; public bool IsFullyParsed() => string.IsNullOrWhiteSpace(Unparsed);