Scoop API

scoop:scoop.std:0.1.0

src/text/padding.scoop

Selected production source · src/text/padding.scoop

1package scoop.text
3/**
4 * Concatenates this string a nonnegative number of times; zero yields an empty string and size
5 * overflow throws.
6 */
7public fun String.repeat(count: Long): String {
8 if (count < 0L) throw IllegalArgumentException(Some("negative repeat count"))
9 if (count == 0L || byteLength == 0L) return ""
10 checkedTextProduct(byteLength, count)
11 val result = StringBuilder()
12 var part = this
13 var remaining = count
14 while (remaining > 0L) {
15 if (remaining % 2L != 0L) result.append(part)
16 remaining /= 2L
17 if (remaining > 0L) part = part + part
18 }
19 return result.toString()
22private fun String.padding(length: Long, paddingChar: Char): String {
23 if (length < 0L) throw IllegalArgumentException(Some("negative padding length"))
24 val count = length - this.charLength
25 if (count <= 0L) return ""
26 checkedTextSum(byteLength, checkedTextProduct(charByteLength(paddingChar), count))
27 return paddingChar.toString().repeat(count)
30/**
31 * Prepends the padding scalar until the requested nonnegative character length is reached; longer
32 * input is preserved.
33 */
34public fun String.padStart(length: Long, paddingChar: Char = ' '): String = padding(length, paddingChar) + this
35/**
36 * Appends the padding scalar until the requested nonnegative character length is reached; longer
37 * input is preserved.
38 */
39public fun String.padEnd(length: Long, paddingChar: Char = ' '): String = this + padding(length, paddingChar)