Improve performance for DefaultParsers.validID(String)

KeyIndex can call this method hundreds of thousands of times, so this
change makes a significant difference in loading time.
This commit is contained in:
Paul Draper 2016-04-10 17:03:19 -06:00 committed by Dale Wijnand
parent a58b184195
commit da1bdba0f8
1 changed files with 15 additions and 1 deletions

View File

@ -264,5 +264,19 @@ object DefaultParsers extends Parsers with ParserMain {
apply(p)(s).resultEmpty.isValid
/** Returns `true` if `s` parses successfully according to [[ID]].*/
def validID(s: String): Boolean = matches(ID, s)
def validID(s: String): Boolean = {
// matches(ID, s) would be simple but KeyIndex can call this method 100k's of times
val length = s.length
if (length == 0 || !s(0).isLetter) {
return false
}
var i = 1
while (i < length) {
if (!isIDChar(s(i))) {
return false
}
i += 1
}
true
}
}