Pattern implementation: JVM and partially JS.

Common tests.
This commit is contained in:
Ilya Gorbunov
2015-04-08 19:26:31 +03:00
parent 459e95d1d9
commit da1641e0f8
4 changed files with 231 additions and 38 deletions
@@ -33,18 +33,18 @@ public fun <T: FlagEnum> fromInt(value: Int, allValues: Array<T>): Set<T> =
allValues.filter({ value and it.mask == it.value }).toSet()
public enum class PatternOptions(override val value: Int, override val mask: Int = value) : FlagEnum {
public enum class PatternOption(override val value: Int, override val mask: Int = value) : FlagEnum {
// common
IGNORE_CASE : PatternOptions(NativePattern.CASE_INSENSITIVE)
MULTILINE : PatternOptions(NativePattern.MULTILINE)
IGNORE_CASE : PatternOption(NativePattern.CASE_INSENSITIVE)
MULTILINE : PatternOption(NativePattern.MULTILINE)
//jvm-specific
LITERAL : PatternOptions(NativePattern.LITERAL)
UNICODE_CASE: PatternOptions(NativePattern.UNICODE_CASE)
UNIX_LINES: PatternOptions(NativePattern.UNIX_LINES)
COMMENTS: PatternOptions(NativePattern.COMMENTS)
DOTALL: PatternOptions(NativePattern.DOTALL)
CANON_EQ: PatternOptions(NativePattern.CANON_EQ)
LITERAL : PatternOption(NativePattern.LITERAL)
UNICODE_CASE: PatternOption(NativePattern.UNICODE_CASE)
UNIX_LINES: PatternOption(NativePattern.UNIX_LINES)
COMMENTS: PatternOption(NativePattern.COMMENTS)
DOT_MATCHES_ALL: PatternOption(NativePattern.DOTALL)
CANON_EQ: PatternOption(NativePattern.CANON_EQ)
}
/* in JS
@@ -54,26 +54,31 @@ public enum class PatternOptions(val value: String) {
}
*/
public trait MatchGroup {
public val range: IntRange
public val value: String
public data class MatchGroup(val value: String, val range: IntRange)
public trait MatchGroupCollection : Collection<MatchGroup?> {
public fun get(index: Int): MatchGroup?
}
public trait MatchResult : MatchGroup {
public val groups: List<MatchGroup?>
public trait MatchResult {
public val range: IntRange
public val value: String
public val groups: MatchGroupCollection
// TODO: Should we have groupCount (equals groups.size()-1)?
public fun next(): MatchResult?
}
public class Pattern ( /* visibility? */ val nativePattern: NativePattern) {
public constructor(pattern: String, options: Set<PatternOptions>): this(NativePattern.compile(pattern, options.toInt()))
public constructor(pattern: String, vararg options: PatternOptions) : this(pattern, options.toSet())
public constructor(pattern: String, options: Set<PatternOption>): this(NativePattern.compile(pattern, options.toInt()))
public constructor(pattern: String, vararg options: PatternOption) : this(pattern, options.toSet())
public val pattern: String
get() = nativePattern.pattern()
public val options: Set<PatternOptions> = fromInt(nativePattern.flags(), PatternOptions.values())
public val options: Set<PatternOption> = fromInt(nativePattern.flags(), PatternOption.values())
public fun matches(input: CharSequence): Boolean = nativePattern.matcher(input).matches()
@@ -86,16 +91,19 @@ public class Pattern ( /* visibility? */ val nativePattern: NativePattern) {
public fun split(input: CharSequence, limit: Int = 0): List<String> = nativePattern.split(input, limit).asList() // TODO: require(limit>=0)
public override fun toString(): String = nativePattern.toString()
companion object {
public fun fromLiteral(literal: String): Pattern = Pattern(literal, PatternOptions.LITERAL)
public fun fromLiteral(literal: String): Pattern = Pattern(literal, PatternOption.LITERAL)
public fun escape(literal: String): String = NativePattern.quote(literal)
public fun escapeReplacement(literal: String): String = Matcher.quoteReplacement(literal)
}
}
public fun String.toPattern(vararg options: PatternOptions): Pattern = Pattern(this, *options)
public fun String.toPattern(options: Set<PatternOptions>): Pattern = Pattern(this, options)
public fun String.toPattern(vararg options: PatternOption): Pattern = Pattern(this, *options)
public fun String.toPattern(options: Set<PatternOption>): Pattern = Pattern(this, options)
@@ -103,34 +111,39 @@ private fun Matcher.findNext(from: Int): MatchResult? {
if (!find(from))
return null
var matchResult = this as java.util.regex.MatchResult
// TODO: If we need MatchResult to be thread safe we must lock everything, or call this.toMatchResult early
var matchResult: java.util.regex.MatchResult = this
return object: MatchResult {
override val range: IntRange
get() = matchResult.start()..matchResult.end()-1
get() = matchResult.range()
override val value: String
get() = matchResult.group()
override val groups: List<MatchGroup?> by Delegates.lazy {
// TODO: wrap
val groups = ArrayList<MatchGroup?>(matchResult.groupCount())
for (groupIndex in 1..groupCount()) {
val range = matchResult.start(groupIndex)..matchResult.end(groupIndex)-1
if (range.start >= 0)
groups.add(object: MatchGroup {
override val range: IntRange = range
override val value: String
get() = matchResult.group(groupIndex)
})
override val groups: MatchGroupCollection = object : MatchGroupCollection {
override fun size(): Int = matchResult.groupCount() + 1
override fun isEmpty(): Boolean = false
override fun contains(o: Any?): Boolean = o is MatchGroup? && this.any({ it == o })
override fun containsAll(c: Collection<Any?>): Boolean = c.all({contains(it)})
override fun iterator(): Iterator<MatchGroup?> = indices.sequence().map { this[it] }.iterator()
override fun get(index: Int): MatchGroup? {
val range = matchResult.range(index)
return if (range.start >= 0)
MatchGroup(matchResult.group(index), range)
else
groups.add(null)
null
}
groups
}
override fun next(): MatchResult? {
matchResult = this@findNext.toMatchResult()
return this@findNext.findNext(matchResult.end()) // TODO: advance next
if (this@findNext === matchResult)
matchResult = this@findNext.toMatchResult()
return this@findNext.findNext(matchResult.end())
}
}
}
private fun java.util.regex.MatchResult.range(): IntRange = start()..end()-1
private fun java.util.regex.MatchResult.range(groupIndex: Int): IntRange = start(groupIndex)..end(groupIndex)-1