private void Filter() { const string COMMENT_PREFIX = "XXX-"; const string DIGITS = "0123456789"; SortedDictionary <string, string> comments = new SortedDictionary <string, string>(); StringBuilder sb = new StringBuilder(); string[] lines = rtxOutput.Lines; foreach (string line in lines) { string[] parts = line.Split(new string[] { COMMENT_PREFIX }, StringSplitOptions.RemoveEmptyEntries); foreach (string part in parts) { string prefix = string.Empty; string numeric = string.Empty; string remainder = string.Empty; string comment = string.Empty; for (int ptr = 0; ptr < part.Length; ptr++) { string digit = part.Substring(ptr, 1); if (DIGITS.Contains(digit)) { numeric += digit; } else { //If number ends before end of line. if (numeric.Length > 0) { prefix = COMMENT_PREFIX + numeric; remainder = part.Substring(ptr); remainder = remainder.Trim(); comment = prefix + " " + remainder; if (comments.ContainsKey(prefix)) { string existing = comments[prefix]; int existingHyphens = CountLetter(existing, "-"); int candidateHyphens = CountLetter(comment, "-"); int commentLength = comment.Length; if (candidateHyphens < existingHyphens) { commentLength += 12; } string candidate = comment; if (candidateHyphens > 1) { candidate = MakeCommentFromBranchName(comment); } if (commentLength > comments[prefix].Length) { comments[prefix] = candidate; } } else { int candidateHyphens = CountLetter(comment, "-"); string candidate = comment; if (candidateHyphens > 1) { candidate = MakeCommentFromBranchName(comment); } comments[prefix] = candidate; } } break; } } //If number ends at end of line. if (numeric.Length > 0) { prefix = COMMENT_PREFIX + numeric; remainder = string.Empty; comment = prefix; if (comments.ContainsKey(prefix)) { if (comment.Length > comments[prefix].Length) { comments[prefix] = comment; } } else { comments.Add(prefix, comment); } } } } foreach (KeyValuePair <string, string> pair in comments) { sb.AppendLine(pair.Key); } foreach (KeyValuePair <string, string> pair in comments) { sb.AppendLine(pair.Value); } if (comments.Count > 0) { rtxOutput.Text = sb.ToString(); } }
private static string MakeCommentFromBranchName(string text) { const int ZERO = 0; const string SPACE = " "; const string HYPHEN = "-"; const string DIGITS = "0123456789"; StringBuilder comment = new StringBuilder(); text = text.Trim() + SPACE; int pos1 = text.LastIndexOf(HYPHEN); if (pos1 >= ZERO) { int pos2 = text.IndexOf(SPACE, pos1); if (pos2 >= ZERO) { text = text.Substring(ZERO, pos2); } bool firstHyphenReached = false; bool firstDigit = false; bool endOfPrefix = false; bool firstDescription = false; for (int pos = ZERO; pos < text.Length; pos++) { String letter = text.Substring(pos, 1); if (letter == HYPHEN) { firstHyphenReached = true; if (!endOfPrefix) { comment.Append(HYPHEN); endOfPrefix = true; } else { comment.Append(SPACE); } } else if (DIGITS.Contains(letter)) { comment.Append(letter); } else { if (!firstHyphenReached) { comment.Append(letter.ToUpper()); } else { if (!firstDescription) { if (letter != SPACE) { firstDescription = true; } if (letter != HYPHEN) { comment.Append(letter.ToUpper()); } } else { comment.Append(letter); } } } } } return(comment.ToString()); }