/// <summary> /// Parse a day info declaration. /// <example> /// Valid strings are: /// [[]] is a day info without a description and a working time /// [[day description]] is a day info with a description and without a working time /// [[day description]] 00:00-01:00,22:00-23:00 is a day info with a description and a working time /// [[]] 00:00-01:00 is a day info without a description and a working time /// </example> /// </summary> /// <returns></returns> public static bool TryParse(string value, out DayInfo dayInfo) { dayInfo = default; IEnumerable <TimePeriod> timePeriods = default; if (string.IsNullOrEmpty(value)) { return(false); } var m = ParseRegex.Match(value); if (!m.Success) { return(false); } var description = m.Groups["description"].Value; var strTimePeriods = m.Groups["timePeriods"].Value; if (!string.IsNullOrWhiteSpace(strTimePeriods) && !TimePeriod.TryParseMulti(strTimePeriods, ",", out timePeriods)) { return(false); } if (string.IsNullOrWhiteSpace(description)) { description = null; } dayInfo = new DayInfo(description, timePeriods); return(true); }
public static PackArrangement TryParse(string value) { Match m = ParseRegex.Match(value); if (m.Success) { return(new PackArrangement(int.Parse(m.Result("${i1}")), int.Parse(m.Result("${i2}")), int.Parse(m.Result("${i3}")))); } else { throw new ArgumentException("Failed parsing int[3] from " + value); } }
private void CreateErrorMessageAndStacktrace(ref string errorMessage, out string stackTrace, int msgId = 0) { Match match = ParseRegex.Match(errorMessage); if (!match.Success) { stackTrace = ""; return; } string fullFileName = match.Groups[1].Value; string fileName = Path.GetFileName(fullFileName); string lineNumber = match.Groups[4].Value; if (string.IsNullOrEmpty(lineNumber)) { lineNumber = match.Groups[6].Value; } string msgReference = msgId == 0 ? "" : $"#{msgId} - "; stackTrace = CreateStackTraceEntry($"{msgReference}{fileName}:{lineNumber}", fullFileName, lineNumber); errorMessage = errorMessage.Replace(match.Value, "").Trim(); match = ScopedTraceStartRegex.Match(errorMessage); if (match.Success) { string scopedTraces = errorMessage.Substring(match.Index + match.Value.Length); errorMessage = errorMessage.Substring(0, match.Index).Trim(); MatchCollection matches = ScopedTraceRegex.Matches(scopedTraces); foreach (Match traceMatch in matches) { fullFileName = traceMatch.Groups[1].Value; lineNumber = traceMatch.Groups[2].Value; string traceMessage = traceMatch.Groups[3].Value.Trim(); stackTrace += CreateStackTraceEntry($"-->{traceMessage}", fullFileName, lineNumber); } } }
public static ParseResult Parse(string value, Type resultType) { if (string.IsNullOrEmpty(value)) { throw new ArgumentNullException(); } var m = ParseRegex.Match(value); if (!m.Success) { throw new ArgumentException($"Unable to convert \'{value}\' into {resultType}"); } var minus = m.Groups[1].Value == "-"; var number = decimal.Parse(m.Groups[2].Value, CultureInfo.InvariantCulture); if (minus) { number = -number; } var unit = m.Groups[6].Value.Trim(); return(new ParseResult(number, unit)); }