/// <remarks> /// The normal Router() method has been brought inline into this ExpandMethodBlocks() method /// as the router complexity is not as great as usual. /// </remarks> protected string ExpandJavaMethodBlocks(int position, ref int level) { int quoteCount = 0; //Quote count is zero at the start of every level. string prevCode = string.Empty; string code = string.Empty; string inner = string.Empty; bool goback = false; int startPos = position; int prevPos = position; int length = _sourceCode.Length; StringBuilder prevBlockCommentMarker = new StringBuilder(); int prevBlockCommentPos = -1; int prevNewLinePos = 0; BlockVector blockVector = new BlockVector(); int lineEndingLength = 1; if (length > 0) { do { //Begin router code. goback = false; prevCode = code; code = _sourceCode.Substring(position, 1); switch (code) { case "/": prevBlockCommentMarker.Append(code); break; case "*": prevBlockCommentMarker.Append(code); if (prevBlockCommentMarker.ToString() == "/**") { prevBlockCommentPos = prevNewLinePos; } break; case "\n": if (prevCode == "\r") { //Windows line ending. lineEndingLength = 2; } else { //Unix line ending. lineEndingLength = 1; } // -1 is based on unix line endings being 1 characters long. // -2 is based on Windows line endings being 2 characters long. prevNewLinePos = position; prevBlockCommentMarker = new StringBuilder(); break; case "\"": quoteCount++; break; case "{": if (level == 0) { //Block from start of file up to line containing class declaration. blockVector = new BlockVector(); blockVector.EndPosition = GetStartOfBlockPosition(prevBlockCommentPos, prevNewLinePos); _blockVectors.Add(blockVector); prevBlockCommentPos = -1; } if (level == 1) { //Block from start of class declaration to start of line containing start of first method. blockVector = new BlockVector(); blockVector.EndPosition = GetStartOfBlockPosition(prevBlockCommentPos, prevNewLinePos); _blockVectors.Add(blockVector); prevBlockCommentPos = -1; } if (quoteCount % 2 == 0) { level++; inner = ExpandJavaMethodBlocks(position + 1, ref level); position += inner.Length; prevPos = position; } break; case "}": inner = _sourceCode.Substring(startPos, position - startPos + 1); if (quoteCount % 2 == 0) { level--; goback = true; } if (level == 0) { //This is the end of the class block. blockVector = new BlockVector(); blockVector.EndPosition = position + lineEndingLength; _blockVectors.Add(blockVector); } if (level == 1) { //This is the end of all method blocks inside the class. blockVector = new BlockVector(); blockVector.EndPosition = position + lineEndingLength; _blockVectors.Add(blockVector); } break; default: //Any character which is not a control character. break; } //End router code. position++; } while (position < length && !goback); } return(inner); }
/// <summary> /// Split all method blocks in the specified sourceCode into a list of string blocks. /// </summary> protected List <BlockVector> SplitMethodBlocks(string sourceCode) { _sourceCode = sourceCode; int position = 0; int level = 0; string inner = string.Empty; switch (_methodBlockLanguage) { case LanguageEnum.Cpp: inner = ExpandJavaMethodBlocks(position, ref level); break; case LanguageEnum.CSharp: inner = ExpandCSharpMethodBlocks(position, ref level); break; case LanguageEnum.Java: inner = ExpandJavaMethodBlocks(position, ref level); break; case LanguageEnum.Other: inner = ExpandJavaMethodBlocks(position, ref level); break; defaut: inner = ExpandJavaMethodBlocks(position, ref level); break; } int startAt = 0; int lineNumber = 1; int blockNumber = 0; BlockVector blockVector = new BlockVector(); for (int ptr = 0; ptr < _blockVectors.Count; ptr++) { blockNumber++; blockVector = _blockVectors[ptr]; blockVector.BlockNumber = blockNumber; blockVector.StartPosition = startAt; int endAt = _blockVectors[ptr].EndPosition; int length = endAt - startAt; string segment = _sourceCode.Substring(startAt, length); string[] parts = segment.Split('\n'); blockVector.StartLinekNumber = lineNumber; lineNumber += parts.Length; blockVector.Content = segment; startAt = endAt + 1; } if (startAt <= _sourceCode.Length) { blockNumber++; blockVector = new BlockVector(); blockVector.BlockNumber = blockNumber; blockVector.StartLinekNumber = lineNumber; blockVector.StartPosition = startAt; blockVector.EndPosition = _sourceCode.Length - 1; string segment = _sourceCode.Substring(startAt); blockVector.Content = segment; _blockVectors.Add(blockVector); } return(_blockVectors); }