Pattern implementation for JS.

This commit is contained in:
Ilya Gorbunov
2015-04-08 20:32:22 +03:00
parent da1641e0f8
commit a4784dfa78
8 changed files with 174 additions and 146 deletions
+8 -17
View File
@@ -16,6 +16,8 @@
package kotlin.text
private val TODO: Nothing get() = throw java.lang.UnsupportedOperationException()
public enum class PatternOption(val value: String) {
IGNORE_CASE : PatternOption("i")
@@ -25,24 +27,11 @@ public enum class PatternOption(val value: String) {
public data class MatchGroup(val value: String)
public trait MatchGroupCollection : Collection<MatchGroup?> {
public fun get(index: Int): MatchGroup?
}
public trait MatchResult {
public val range: IntRange
public val value: String
public val groups: MatchGroupCollection
public class Pattern (pattern: String, options: Set<PatternOption>) {
public fun next(): MatchResult?
}
public class Pattern(public val pattern: String, options_: Set<PatternOption>) {
public constructor(pattern: String, vararg options: PatternOption) : this(pattern, options.toSet())
public val options: Set<PatternOption> = options_.toSet()
public val pattern: String = pattern
public val options: Set<PatternOption> = options.toSet()
private val nativePattern: RegExp = RegExp(pattern, options.map { it.value }.joinToString() + "g")
@@ -60,7 +49,7 @@ public class Pattern(public val pattern: String, options_: Set<PatternOption>) {
public fun split(input: CharSequence, limit: Int = 0): List<String> = TODO
public fun toString(): String = nativePattern.toString()
public override fun toString(): String = nativePattern.toString()
companion object {
public fun fromLiteral(literal: String): Pattern = Pattern(escape(literal))
@@ -69,6 +58,8 @@ public class Pattern(public val pattern: String, options_: Set<PatternOption>) {
}
}
public fun Pattern(pattern: String, vararg options: PatternOption): Pattern = Pattern(pattern, options.toSet())
private fun RegExp.findNext(input: String, from: Int): MatchResult? {
this.lastIndex = from
+9 -6
View File
@@ -19,17 +19,19 @@ public fun String.nativeStartsWith(s: String, position: Int): Boolean = noImpl
native("endsWith")
public fun String.nativeEndsWith(s: String): Boolean = noImpl
deprecated("Use split(Pattern) instead.")
library("splitString")
public fun String.splitWithRegex(regex: String): Array<String> = noImpl
deprecated("Use split(Pattern) instead.")
library("splitString")
public fun String.splitWithRegex(regex: String, limit: Int): Array<String> = noImpl
deprecated("Use splitWithRegex (temporary)")
deprecated("Use split(Pattern) instead.")
library("splitString")
public fun String.split(regex: String): Array<String> = noImpl
deprecated("Use splitWithRegex (temporary)")
deprecated("Use split(Pattern) instead.")
library("splitString")
public fun String.split(regex: String, limit: Int): Array<String> = noImpl
@@ -52,12 +54,13 @@ public fun CharSequence.isEmpty(): Boolean = noImpl
// TODO: internal
// native because we need to escape newValue
//native("replace")
//public fun String.nativeReplace(oldValue: String, newValue: String): String = noImpl
// TODO: internal
native("replace")
private fun String.nativeReplace(oldValue: String, newValue: String): String = noImpl
native("replace")
private fun String.nativeReplace(pattern: RegExp, replacement: String): String = noImpl
public fun String.nativeReplace(pattern: RegExp, replacement: String): String = noImpl
/*
+2 -2
View File
@@ -81,7 +81,7 @@ public inline fun String.decapitalize(): String {
public fun String.replace(oldValue: String, newValue: String): String =
nativeReplace(new RegExp(Pattern.escape(oldValue),"g"), Pattern.escapeReplacement(newValue))
nativeReplace(RegExp(kotlin.text.Pattern.escape(oldValue),"g"), kotlin.text.Pattern.escapeReplacement(newValue))
public fun String.replace(oldChar: Char, newChar: Char): String =
nativeReplace(new RegExp(Pattern.escape(oldChar.toString()),"g"), newChar.toString())
nativeReplace(RegExp(kotlin.text.Pattern.escape(oldChar.toString()),"g"), newChar.toString())