Rename Pattern to Regex.
toRegex now converts string to our regex, and toPattern converts to JVM Pattern.
This commit is contained in:
@@ -121,7 +121,7 @@ public fun Node.previousElements(): List<Element> = previousSiblings().filterIsI
|
||||
public var Element.classSet: MutableSet<String>
|
||||
get() {
|
||||
val answer = LinkedHashSet<String>()
|
||||
val array = this.classes.split("""\s""".toRegex())
|
||||
val array = this.classes.split("""\s""".toPattern())
|
||||
for (s in array) {
|
||||
if (s.length() > 0) {
|
||||
answer.add(s)
|
||||
|
||||
@@ -95,7 +95,7 @@ public fun File.filePathComponents(): FilePathComponents {
|
||||
// Split not only by / or \, but also by //, ///, \\, \\\, etc.
|
||||
val list = if (rootName.length() > 0 && subPath.isEmpty()) listOf() else
|
||||
// Looks awful but we split just by /+ or \+ depending on OS
|
||||
subPath.split("""\Q${File.separatorChar}\E+""".toRegex()).toList().map { it -> File(it) }
|
||||
subPath.split("""\Q${File.separatorChar}\E+""".toPattern()).toList().map { it -> File(it) }
|
||||
return FilePathComponents(rootName, list)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package kotlin
|
||||
|
||||
import java.util.NoSuchElementException
|
||||
import kotlin.text.Pattern
|
||||
import kotlin.text.Regex
|
||||
|
||||
/** Returns the string with leading and trailing text matching the given string removed */
|
||||
deprecated("Use removeSurrounding(text, text) or removePrefix(text).removeSuffix(text)")
|
||||
@@ -910,7 +910,7 @@ public fun String.split(vararg delimiters: Char, ignoreCase: Boolean = false, li
|
||||
/**
|
||||
* Splits this string around matches of the given regular expression.
|
||||
*/
|
||||
public fun String.split(pattern: Pattern, limit: Int = 0): List<String> = pattern.split(this, limit)
|
||||
public fun String.split(pattern: Regex, limit: Int = 0): List<String> = pattern.split(this, limit)
|
||||
|
||||
/**
|
||||
* Splits this string to a sequence of lines delimited by any of the following character sequences: CRLF, LF or CR.
|
||||
|
||||
@@ -94,7 +94,7 @@ public fun String.split(regex: Pattern, limit: Int = 0): List<String> = regex.sp
|
||||
* Splits this string around matches of the given regular expression.
|
||||
*/
|
||||
deprecated("Convert an argument to regex with toRegex or use splitBy instead.")
|
||||
public fun String.split(regex: String): Array<String> = split(regex.toRegex()).toTypedArray()
|
||||
public fun String.split(regex: String): Array<String> = split(regex.toPattern()).toTypedArray()
|
||||
|
||||
|
||||
/**
|
||||
@@ -403,7 +403,7 @@ public fun CharSequence.slice(range: IntRange): CharSequence {
|
||||
* with the specified flags from [Pattern] or'd together
|
||||
* so that strings can be split or matched on.
|
||||
*/
|
||||
public fun String.toRegex(flags: Int = 0): java.util.regex.Pattern {
|
||||
public fun String.toPattern(flags: Int = 0): java.util.regex.Pattern {
|
||||
return java.util.regex.Pattern.compile(this, flags)
|
||||
}
|
||||
|
||||
@@ -480,7 +480,7 @@ public inline fun <T : Appendable> String.takeWhileTo(result: T, predicate: (Cha
|
||||
*/
|
||||
public fun String.replaceAll(regexp: String, body: (java.util.regex.MatchResult) -> String): String {
|
||||
val sb = StringBuilder(this.length())
|
||||
val p = regexp.toRegex()
|
||||
val p = regexp.toPattern()
|
||||
val m = p.matcher(this)
|
||||
|
||||
var lastIdx = 0
|
||||
|
||||
-4
@@ -30,7 +30,3 @@ public trait MatchResult {
|
||||
public fun next(): MatchResult?
|
||||
}
|
||||
|
||||
// maybe move them to kotlin package to be more discoverable
|
||||
public fun String.toPattern(vararg options: PatternOption): Pattern = Pattern(this, *options)
|
||||
public fun String.toPattern(options: Set<PatternOption>): Pattern = Pattern(this, options)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package kotlin
|
||||
|
||||
import kotlin.text.*
|
||||
|
||||
public fun String.toRegex(vararg options: RegexOption): Regex = Regex(this, *options)
|
||||
public fun String.toRegex(options: Set<RegexOption>): Regex = Regex(this, options)
|
||||
+16
-16
@@ -16,7 +16,7 @@
|
||||
|
||||
package kotlin.text
|
||||
|
||||
import java.util.regex.Pattern as NativePattern
|
||||
import java.util.regex.Pattern
|
||||
import java.util.regex.Matcher
|
||||
|
||||
private val TODO: Nothing get() = throw UnsupportedOperationException()
|
||||
@@ -31,32 +31,32 @@ public fun <T: FlagEnum> fromInt(value: Int, allValues: Array<T>): Set<T> =
|
||||
allValues.filter({ value and it.mask == it.value }).toSet()
|
||||
|
||||
|
||||
public enum class PatternOption(override val value: Int, override val mask: Int = value) : FlagEnum {
|
||||
public enum class RegexOption(override val value: Int, override val mask: Int = value) : FlagEnum {
|
||||
// common
|
||||
IGNORE_CASE : PatternOption(NativePattern.CASE_INSENSITIVE)
|
||||
MULTILINE : PatternOption(NativePattern.MULTILINE)
|
||||
IGNORE_CASE : RegexOption(Pattern.CASE_INSENSITIVE)
|
||||
MULTILINE : RegexOption(Pattern.MULTILINE)
|
||||
|
||||
//jvm-specific
|
||||
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)
|
||||
LITERAL : RegexOption(Pattern.LITERAL)
|
||||
UNICODE_CASE: RegexOption(Pattern.UNICODE_CASE)
|
||||
UNIX_LINES: RegexOption(Pattern.UNIX_LINES)
|
||||
COMMENTS: RegexOption(Pattern.COMMENTS)
|
||||
DOT_MATCHES_ALL: RegexOption(Pattern.DOTALL)
|
||||
CANON_EQ: RegexOption(Pattern.CANON_EQ)
|
||||
}
|
||||
|
||||
public data class MatchGroup(val value: String, val range: IntRange)
|
||||
|
||||
|
||||
public class Pattern ( /* visibility? */ val nativePattern: NativePattern) {
|
||||
public class Regex( /* visibility? */ val nativePattern: Pattern) {
|
||||
|
||||
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 constructor(pattern: String, options: Set<RegexOption>): this(Pattern.compile(pattern, options.toInt()))
|
||||
public constructor(pattern: String, vararg options: RegexOption) : this(pattern, options.toSet())
|
||||
|
||||
public val pattern: String
|
||||
get() = nativePattern.pattern()
|
||||
|
||||
public val options: Set<PatternOption> = fromInt(nativePattern.flags(), PatternOption.values())
|
||||
public val options: Set<RegexOption> = fromInt(nativePattern.flags(), RegexOption.values())
|
||||
|
||||
public fun matches(input: CharSequence): Boolean = nativePattern.matcher(input).matches()
|
||||
|
||||
@@ -72,8 +72,8 @@ public class Pattern ( /* visibility? */ val nativePattern: NativePattern) {
|
||||
public override fun toString(): String = nativePattern.toString()
|
||||
|
||||
companion object {
|
||||
public fun fromLiteral(literal: String): Pattern = Pattern(literal, PatternOption.LITERAL)
|
||||
public fun escape(literal: String): String = NativePattern.quote(literal)
|
||||
public fun fromLiteral(literal: String): Regex = Regex(literal, RegexOption.LITERAL)
|
||||
public fun escape(literal: String): String = Pattern.quote(literal)
|
||||
public fun escapeReplacement(literal: String): String = Matcher.quoteReplacement(literal)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user