Scoop API

scoop:scoop.std:0.1.0

src/text/trim.scoop

Selected production source · src/text/trim.scoop

1package scoop.text
3private fun String.trimText(predicate: (Char) -> Boolean, trimLeft: Boolean, trimRight: Boolean): String {
4 val chars = toCharArray()
5 var start = 0L
6 var end = chars.size
7 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)
12/**
13 * Removes a contiguous run of matching scalars from both ends. Without a predicate, matching uses
14 * Unicode White_Space.
15 */
16public 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, matching
19 * uses Unicode White_Space.
20 */
21public 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 uses
24 * Unicode White_Space.
25 */
26public 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 uses
29 * Unicode White_Space.
30 */
31public fun String.trim(): String = trim(::isTextWhitespace)
32/**
33 * Removes a contiguous run of matching scalars from the beginning. Without a predicate, matching
34 * uses Unicode White_Space.
35 */
36public fun String.trimStart(): String = trimStart(::isTextWhitespace)
37/**
38 * Removes a contiguous run of matching scalars from the end. Without a predicate, matching uses
39 * Unicode White_Space.
40 */
41public fun String.trimEnd(): String = trimEnd(::isTextWhitespace)