Skip to content

Instantly share code, notes, and snippets.

@rubyu
Created July 13, 2014 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubyu/871c22b13cf7060a2175 to your computer and use it in GitHub Desktop.
Save rubyu/871c22b13cf7060a2175 to your computer and use it in GitHub Desktop.
ScalaのRegexParsersで書いたパーサのチューニング方法 #2 ref: http://qiita.com/rubyu/items/de843a441859cf81af48
(access count: A,31)
(access count: B,40)
//適当なCSVパーサの定義
//lazy val field = "[a-z]+".r
lazy val A = field ~ ( rep( "," ~ field ) ).?
lazy val B = ( rep( field ~ "," ) ).? ~ field
var count = 0
class DebugCharSequence(seq: CharSequence) extends java.lang.CharSequence {
def charAt(n: Int) = { count += 1; seq.charAt(n) }
def subSequence(s: Int, e: Int) = new DebugCharSequence(seq.subSequence(s, e))
def length = seq.length
override def toString = seq.toString
}
val dseq = new DebugCharSequence("a,b,c")
parser.parse(parser.A, dseq)
println("access count: A", count)
count = 0
parser.parse(parser.B, dseq)
println("access count: B", count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment