private static IImmutableSet <DayOfWeekSpecified> GetWeekOfDaySet(string values, bool isMonth) { if (string.IsNullOrEmpty(values)) { return(ImmutableHashSet <DayOfWeekSpecified> .Empty.AddRange(GetAllEnums <DayOfWeek>().Select(day => new DayOfWeekSpecified(day, 0)))); } var seed = ReductionState <DayOfWeekSpecified> .Default(); // https://regex101.com/r/lbGStS/2 RegexOptions options = RegexOptions.IgnoreCase; string input = values.Substring(1, values.Length - 2); string pattern = $"([+])|([Mo|Tu|We|Th|Fr|Sa|Su]+{GetSpecifierRegexPattern(isMonth)})"; return(Regex.Matches(input, pattern, options) .Cast <Match>() .Select(match => match.Value) .Aggregate(seed, (accu, current) => { if (current == "+") { return new ReductionState <DayOfWeekSpecified>(accu.Reduction, Operator.Add); } var operand = DayOfWeekSpecifiedFromValue(current, isMonth); return new ReductionState <DayOfWeekSpecified>(accu.Reduction.Add(operand), accu.Op); }) .Reduction); }
private static IImmutableSet <int> GetTimesSet(string values, int start, int end) { if (string.IsNullOrEmpty(values)) { return(ImmutableHashSet <int> .Empty.AddRange(Enumerable.Range(start, end - start + 1))); } var seed = ReductionState <int> .Default(); // https://regex101.com/r/P8yGDm/2 RegexOptions options = RegexOptions.IgnoreCase; string input = values.Substring(1, values.Length - 2); string pattern = @"([+\-])|(\d{1,4})"; return(Regex.Matches(input, pattern, options) .Cast <Match>() .Select(match => match.Value) .Aggregate(seed, (accu, current) => { if (current == "+" || current == "-") { return new ReductionState <int>(accu.Reduction, (current == "+") ? Operator.Add : Operator.Range); } var operand = Convert.ToInt32(current); if (accu.Op == Operator.Add) { return new ReductionState <int>(accu.Reduction.Add(operand), accu.Op); } else if (accu.Reduction.Count == 0) { return new ReductionState <int>(accu.Reduction.Add(-operand), Operator.Add); } var lastOperand = accu.Reduction.Last(); var range = GetRange(lastOperand, operand); return new ReductionState <int>(accu.Reduction.AddRange(range), Operator.Add); }) .Reduction); }
private static IImmutableSet <DayOfWeek> GetWeekOfDaySet(string values) { if (string.IsNullOrEmpty(values)) { return(ImmutableHashSet <DayOfWeek> .Empty.AddRange(GetAllEnums <DayOfWeek>())); } var seed = ReductionState <DayOfWeek> .Default(); // https://regex101.com/r/4NRHND/2 RegexOptions options = RegexOptions.IgnoreCase; string input = values.Substring(1, values.Length - 2); string pattern = @"([+\-])|(Mo|Tu|We|Th|Fr|Sa|Su)"; return(Regex.Matches(input, pattern, options) .Cast <Match>() .Select(match => match.Value) .Aggregate(seed, (accu, current) => { if (current == "+" || current == "-") { return new ReductionState <DayOfWeek>(accu.Reduction, (current == "+") ? Operator.Add : Operator.Range); } var operand = GetDayOfWeekFrom(current); if (accu.Op == Operator.Add) { return new ReductionState <DayOfWeek>(accu.Reduction.Add(operand), accu.Op); } var lastOperand = accu.Reduction.Last(); var range = GetRange(lastOperand, operand); return new ReductionState <DayOfWeek>(accu.Reduction.AddRange(range), Operator.Add); }) .Reduction); }