private static ValidationFailure TryParseItem(string formatItem, out FormatStringItem formatStringItem) { formatStringItem = null; var matchResult = StringFormatItemRegex.Match(formatItem); if (!matchResult.Success) { return(ValidationFailure.FormatItemMalformed); } var index = int.Parse(matchResult.Groups["Index"].Value); var alignment = matchResult.Groups["Alignment"].Success ? (int?)int.Parse(matchResult.Groups["Alignment"].Value) : null; var formatString = matchResult.Groups["Format"].Success ? matchResult.Groups["Format"].Value : null; formatStringItem = new FormatStringItem(index, alignment, formatString); return(null); }
private static ValidationFailure TryParseItem(string formatItem, out FormatStringItem formatStringItem) { formatStringItem = null; var indexOfComma = formatItem.IndexOf(','); var indexOfColon = formatItem.IndexOf(':'); var split = formatItem.Split(',', ':'); if (indexOfComma >= 0 && indexOfColon >= 0 && indexOfColon < indexOfComma || split.Length > 3) { return(ValidationFailure.FormatItemMalformed); } int index; int? alignment = null; string formatString = null; if (!int.TryParse(split[0], out index)) { return(ValidationFailure.FormatItemIndexIsNaN); } if (indexOfComma >= 0) { int localAlignment; if (!int.TryParse(split[1], out localAlignment)) { return(ValidationFailure.FormatItemAlignmentIsNaN); } alignment = localAlignment; } if (indexOfColon >= 0) { formatString = indexOfComma >= 0 ? split[2] : split[1]; } formatStringItem = new FormatStringItem(index, alignment, formatString); return(null); }