Skip to content

Commit 5cda34e

Browse files
authored
Merge pull request #267 from KenCoder/master
Fix StackOverflowError in Utils.groupBy
2 parents 0477ab8 + 7e13d5d commit 5cda34e

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

scalariform/src/main/scala/scalariform/utils/Utils.scala

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package scalariform.utils
33
import java.io.FileOutputStream
44
import java.io.FileInputStream
55
import java.io.IOException
6+
import scala.collection.mutable.ListBuffer
67

78
object Utils {
89

@@ -31,14 +32,20 @@ object Utils {
3132
}
3233
}
3334

34-
def groupBy[A](eq: (A, A) Boolean, lst: List[A]): List[List[A]] =
35-
lst match {
36-
case Nil Nil
37-
case (x :: xs) {
38-
val (ys, zs) = xs span { eq(x, _) }
39-
(x :: ys) :: groupBy(eq, zs)
35+
def groupBy[A](eq: (A, A) Boolean, lst: List[A]): List[List[A]] = {
36+
val result = ListBuffer[List[A]]()
37+
@annotation.tailrec def loop(cursor: List[A]): Unit =
38+
cursor match {
39+
case Nil
40+
case (x :: xs) {
41+
val (ys, zs) = xs span { eq(x, _) }
42+
result += x :: ys
43+
loop(zs)
44+
}
4045
}
41-
}
46+
loop(lst)
47+
result.toList
48+
}
4249

4350
// File ------------------
4451

scalariform/src/test/scala/scalariform/parser/ParserTest.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class ParserTest extends FlatSpec with Matchers {
4848
""")
4949
}
5050

51+
// issue #265
52+
"Parser" should "handle deep complex expressions" in {
53+
parseExpression("o.p(a" + (",a" * 3000) + ")")
54+
}
55+
5156
private def parser(s: String) = new ScalaParser(ScalaLexer.tokenise(s).toArray)
5257
private def parseExpression(s: String) = parser(s).expr
5358
private def parseCompilationUnit(s: String) = parser(s).compilationUnit

0 commit comments

Comments
 (0)