Add Regex splitToSequence samples

This commit is contained in:
Abduqodiri Qurbonzoda
2021-07-23 13:29:04 +03:00
committed by Space
parent e691de6e4c
commit d7b10f31b4
8 changed files with 30 additions and 0 deletions
@@ -339,6 +339,7 @@ public actual class Regex internal constructor(internal val nativePattern: Patte
*
* @param limit Non-negative value specifying the maximum number of substrings the string can be split to.
* Zero by default means no limit is set.
* @sample samples.text.Regexps.splitToSequence
*/
@SinceKotlin("1.5")
@ExperimentalStdlibApi
@@ -77,6 +77,7 @@ expect class Regex {
*
* @param limit Non-negative value specifying the maximum number of substrings the string can be split to.
* Zero by default means no limit is set.
* @sample samples.text.Regexps.splitToSequence
*/
@SinceKotlin("1.5")
@ExperimentalStdlibApi
@@ -211,6 +211,7 @@ public actual class Regex actual constructor(pattern: String, options: Set<Regex
*
* @param limit Non-negative value specifying the maximum number of substrings the string can be split to.
* Zero by default means no limit is set.
* @sample samples.text.Regexps.splitToSequence
*/
@SinceKotlin("1.5")
@ExperimentalStdlibApi
@@ -230,6 +230,7 @@ internal constructor(private val nativePattern: Pattern) : Serializable {
*
* @param limit Non-negative value specifying the maximum number of substrings the string can be split to.
* Zero by default means no limit is set.
* @sample samples.text.Regexps.splitToSequence
*/
@SinceKotlin("1.5")
@ExperimentalStdlibApi
@@ -52,4 +52,16 @@ class Regexps {
val names = matches.map { it.groupValues[1] }.joinToString()
assertPrints(names, "Alice, Bob, Eve")
}
@Sample
fun splitToSequence() {
val colors = "green, red , brown&blue, orange, pink&green"
val regex = "[,\\s]+".toRegex()
val mixedColor = regex.splitToSequence(colors)
.onEach { println(it) }
.firstOrNull { it.contains('&') }
assertPrints(mixedColor, "brown&blue")
}
}
@@ -495,4 +495,16 @@ class Strings {
assertPrints("abc".toBooleanStrictOrNull(), "null")
}
@Sample
fun splitToSequence() {
val colors = "green, red , brown&blue, orange, pink&green"
val regex = "[,\\s]+".toRegex()
val mixedColor = colors.splitToSequence(regex)
.onEach { println(it) }
.firstOrNull { it.contains('&') }
assertPrints(mixedColor, "brown&blue")
}
}
@@ -1379,6 +1379,7 @@ public inline fun CharSequence.split(regex: Regex, limit: Int = 0): List<String>
*
* @param limit Non-negative value specifying the maximum number of substrings to return.
* Zero by default means no limit is set.
* @sample samples.text.Strings.splitToSequence
*/
@SinceKotlin("1.5")
@ExperimentalStdlibApi
+1
View File
@@ -52,6 +52,7 @@ actual class Regex {
*
* @param limit Non-negative value specifying the maximum number of substrings the string can be split to.
* Zero by default means no limit is set.
* @sample samples.text.Regexps.splitToSequence
*/
public actual fun splitToSequence(input: CharSequence, limit: Int): Sequence<String> = TODO("Wasm stdlib: Text")