scoop:scoop.std:0.1.0
src/text/trim.scoop
Selected production source · src/text/trim.scoop
1
package scoop.text3
private fun String.trimText(predicate: (Char) -> Boolean, trimLeft: Boolean, trimRight: Boolean): String {4
val chars = toCharArray()5
var start = 0L6
var end = chars.size7
if (trimLeft) { while (start < end && predicate(chars[start])) start += 1L }8
if (trimRight) { while (end > start && predicate(chars[end - 1L])) end -= 1L }9
return textRange(chars, start, end)10
}12
/**13
* Removes a contiguous run of matching scalars from both ends. Without a predicate, matching uses14
* Unicode White_Space.15
*/16
public fun String.trim(predicate: (Char) -> Boolean): String = trimText(predicate, true, true)17
/**18
* Removes a contiguous run of matching scalars from the beginning. Without a predicate, matching19
* uses Unicode White_Space.20
*/21
public fun String.trimStart(predicate: (Char) -> Boolean): String = trimText(predicate, true, false)22
/**23
* Removes a contiguous run of matching scalars from the end. Without a predicate, matching uses24
* Unicode White_Space.25
*/26
public fun String.trimEnd(predicate: (Char) -> Boolean): String = trimText(predicate, false, true)27
/**28
* Removes a contiguous run of matching scalars from both ends. Without a predicate, matching uses29
* Unicode White_Space.30
*/31
public fun String.trim(): String = trim(::isTextWhitespace)32
/**33
* Removes a contiguous run of matching scalars from the beginning. Without a predicate, matching34
* uses Unicode White_Space.35
*/36
public fun String.trimStart(): String = trimStart(::isTextWhitespace)37
/**38
* Removes a contiguous run of matching scalars from the end. Without a predicate, matching uses39
* Unicode White_Space.40
*/41
public fun String.trimEnd(): String = trimEnd(::isTextWhitespace)