private static int GetValue(ref string filter, CrontabFieldKind kind)
{
var maxValue = Constants.MaximumDateTimeValues[kind];
if (string.IsNullOrEmpty(filter))
throw new CrontabException("Expected number, but filter was empty.");
int i, value;
var isDigit = char.IsDigit(filter[0]);
var isLetter = char.IsLetter(filter[0]);
// Because this could either numbers, or letters, but not a combination,
// check each condition separately.
for (i = 0; i < filter.Length; i++)
if ((isDigit && !char.IsDigit(filter[i])) || (isLetter && !char.IsLetter(filter[i]))) break;
var valueToParse = filter.Substring(0, i);
if (int.TryParse(valueToParse, out value))
{
filter = filter.Substring(i);
var returnValue = value;
if (returnValue > maxValue)
throw new CrontabException(string.Format("Value for {0} filter exceeded maximum value of {1}", Enum.GetName(typeof(CrontabFieldKind), kind), maxValue));
return returnValue;
}
else
{
List<KeyValuePair<string, int>> replaceVal = null;
if (kind == CrontabFieldKind.DayOfWeek)
replaceVal = Constants.Days.Where(x => valueToParse.StartsWith(x.Key)).ToList();
else if (kind == CrontabFieldKind.Month)
replaceVal = Constants.Months.Where(x => valueToParse.StartsWith(x.Key)).ToList();
if (replaceVal != null && replaceVal.Count == 1)
{
// missingFilter addresses when a filter string of "SUNL" is passed in,
// which causes the isDigit/isLetter loop above to iterate through the end
// of the string. This catches the edge case, and re-appends L to the end.
var missingFilter = "";
if (filter.Length == i && filter.EndsWith("L") && kind == CrontabFieldKind.DayOfWeek)
missingFilter = "L";
filter = filter.Substring(i) + missingFilter;
var returnValue = replaceVal.First().Value;
if (returnValue > maxValue)
throw new CrontabException(string.Format("Value for {0} filter exceeded maximum value of {1}", Enum.GetName(typeof(CrontabFieldKind), kind), maxValue));
return returnValue;
}
}
throw new CrontabException("Filter does not contain expected number");
}