/** * @param buffer * @param fromIndex index should be -1 for "at end" or the index just after the search start pos (so equivalent to buffer.lenght()) * @return */ public static int getNumberOfEmptyLinesAtEnd(StringBuilder buffer, int fromIndex) { if (fromIndex < 0) { fromIndex = buffer.Length; } //don't count the last blank line, since it will probably have data on it int count = 0; bool firstCRFound = false; int i = fromIndex - 1; while (i >= 0) { char c = buffer[i]; if (!AntlrUtilities.isASWhitespace(c)) { return(count); } if (c == '\n') { if (!firstCRFound) { firstCRFound = true; } else { count++; }; } i--; } return(count); }
public static bool isOnlyWhitespaceOnLastLine(StringBuilder buffer) { int i = buffer.Length - 1; while (i >= 0) { char c = buffer[i]; if (!AntlrUtilities.isASWhitespace(c)) { return(false); } if (c == '\n') { return(true); } i--; } return(true); }
public static bool validateNonWhitespaceIdentical(String s1, String s2) { String newBuffer1 = ""; String newBuffer2 = ""; for (int i = 0; i < s1.Length; i++) { char c = s1[i]; if (!AntlrUtilities.isASWhitespace(c)) { newBuffer1 += c; } } for (int i = 0; i < s2.Length; i++) { char c = s2[i]; if (!AntlrUtilities.isASWhitespace(c)) { newBuffer2 += c; } } return(newBuffer1 == newBuffer2); }
/** * This is a weaker validation that just checks to make sure that the number of occurrences of each character * is identical. * @param buffer * @param originalSource * @return */ public static bool validateNonWhitespaceCharCounts(String buffer, String originalSource) { //some reasonable way of validating. Just count non-whitespace and make sure that we have at least as many //chars as before. Could improve to keep counts of each char so that ordering doesn't matter. Dictionary <char, Int32> originalCharMap = new Dictionary <char, Int32>(); Dictionary <char, Int32> newCharMap = new Dictionary <char, Int32>(); int originalCharCount = 0; for (int i = 0; i < originalSource.Length; i++) { char c = originalSource[i]; if (!AntlrUtilities.isASWhitespace(c)) { originalCharCount++; try { int count = originalCharMap[c]; originalCharMap[c] = count + 1; } catch (KeyNotFoundException) { originalCharMap.Add(c, 1); } } } int newCharCount = 0; for (int i = 0; i < buffer.Length; i++) { char c = buffer[i]; if (!AntlrUtilities.isASWhitespace(buffer[i])) { newCharCount++; try { int count = newCharMap[c]; newCharMap[c] = count + 1; } catch (KeyNotFoundException) { newCharMap.Add(c, 1); } } } if (newCharMap.Count != originalCharMap.Count) { return(false); } foreach (char charAsInt in originalCharMap.Keys) { Int32 origCount = originalCharMap[charAsInt]; Int32 newCount = newCharMap[charAsInt]; if (origCount != newCount) { return(false); } } if (newCharCount != originalCharCount) { return(false); } return(true); }