Skip to content

Commit

Permalink
Slightly better.
Browse files Browse the repository at this point in the history
  • Loading branch information
Amos Wenger committed Apr 18, 2012
1 parent 725f493 commit 4ca8db9
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/main/scala/readability/ExtractionComponent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ abstract class ExtractionComponent(plugin : Plugin) extends PluginComponent {

def fix(source: SourceFile) {
val totalLines = source.offsetToLine(source.length - 1)
println("In " + source + ", total lines = " + totalLines)
while(!isBalanced(source) && maxLine < totalLines) {
maxLine += 1
}
Expand All @@ -58,16 +57,18 @@ abstract class ExtractionComponent(plugin : Plugin) extends PluginComponent {
def isBalanced0(chars: List[Char], stack: List[Char]): Boolean = chars match {
case x :: xs => {
if (opens.contains(x)) {
isBalanced0(xs, x :: stack)
isBalanced0(xs, x :: stack) // consume one char and push on stack
} else if (closes.contains(x)) {
stack match {
case y :: ys =>
if (x == braceMap(y)) isBalanced0(xs, ys) else false
case _ => false // ran out of stack, too many closings
if (x == braceMap(y)) {
isBalanced0(xs, ys) // consume one char and pop stack
} else false // closing brace didn't match last open brace
case _ => false // tried to close when nothing was open
}
} else isBalanced0(xs, stack)
} else isBalanced0(xs, stack) // consume one char, leave stack alone
}
case _ => stack.isEmpty
case _ => stack.isEmpty // only balanced if nothing is left open
}

isBalanced0(code.toList, Nil)
Expand Down

0 comments on commit 4ca8db9

Please sign in to comment.