Skip to content

Instantly share code, notes, and snippets.

@aya-eiya
Created July 23, 2013 13:57
Show Gist options
  • Save aya-eiya/6062535 to your computer and use it in GitHub Desktop.
Save aya-eiya/6062535 to your computer and use it in GitHub Desktop.
ちょっとしたことがGroovy「あっという間に死んでしまう正規表現を検証する」 ref: http://qiita.com/aya_eiya/items/5aa5aac1c00d81772dd6
(パターン+)+
assert ('abcd' ==~ /.+d/)
assert !('abcd' ==~ /.++d/)
@Grab(group='org.spockframework', module='spock-core', version='0.7-groovy-2.0')
import spock.lang.*
import java.util.regex.Matcher
import java.util.regex.Pattern
class SlowRegexTest extends Specification {
def stopWatch = {c->
def s = System.nanoTime()
c()
def w = System.nanoTime() - s
if(w < 1000000)
"${w / 1000} [microsec]"
else if(w < 1000000000)
"${w / 1000000} [millsec]"
else
"${w / 1000000000} [sec]"
}
def p = Pattern.compile(/(([0-9a-zA-Z]+[\s.]*)+)$/)
def trim = { name ->
def matcher = p.matcher(name)
println stopWatch { matcher.find() }
println matcher.start()
name.substring(0, matcher.start())
}
@Timeout(10)
def '末尾のトリムに10秒以上かからないか'(){
expect:
trim(target) == result
where:
target | result
"******${"A"* 1}BC DEF GHI JKL MNO PQR STU_ VWX." | "******${"A"* 1}BC DEF GHI JKL MNO PQR STU_ "
"******${"A"* 3}BC DEF GHI JKL MNO PQR STU_ VWX." | "******${"A"* 3}BC DEF GHI JKL MNO PQR STU_ "
"******${"A"* 5}BC DEF GHI JKL MNO PQR STU_ VWX." | "******${"A"* 5}BC DEF GHI JKL MNO PQR STU_ "
"******${"A"* 7}BC DEF GHI JKL MNO PQR STU_ VWX." | "******${"A"* 7}BC DEF GHI JKL MNO PQR STU_ "
"******${"A"* 9}BC DEF GHI JKL MNO PQR STU_ VWX." | "******${"A"* 9}BC DEF GHI JKL MNO PQR STU_ "
"******${"A"*11}BC DEF GHI JKL MNO PQR STU_ VWX." | "******${"A"*11}BC DEF GHI JKL MNO PQR STU_ "
}
}
def p = Pattern.compile(/(([0-9a-zA-Z]++[\s.]*)+)$/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment