Skip to content

Commit 3088586

Browse files
committed
Add runnable samples for trimIndent and trimMargin.
#KT-9786 Fixed
1 parent 579238c commit 3088586

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

libraries/stdlib/samples/test/samples/text/strings.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,31 @@ class Strings {
2424
assertPrints("Word".repeat(0), "")
2525
}
2626

27+
@Sample
28+
fun trimIndent() {
29+
val withoutIndent =
30+
"""
31+
ABC
32+
123
33+
456
34+
""".trimIndent()
35+
assertPrints(withoutIndent, "ABC\n123\n456")
36+
}
37+
38+
@Sample
39+
fun trimMargin() {
40+
val withoutMargin1 = """ABC
41+
|123
42+
|456""".trimMargin()
43+
assertPrints(withoutMargin1, "ABC\n123\n456")
44+
45+
val withoutMargin2 = """
46+
#XYZ
47+
#foo
48+
#bar
49+
""".trimMargin("#")
50+
assertPrints(withoutMargin2, "XYZ\nfoo\nbar")
51+
}
52+
53+
2754
}

libraries/stdlib/src/kotlin/text/Indent.kt

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,25 @@ package kotlin.text
55

66
/**
77
* Trims leading whitespace characters followed by [marginPrefix] from every line of a source string and removes
8-
* first and last lines if they are blank (notice difference blank vs empty).
8+
* the first and the last lines if they are blank (notice difference blank vs empty).
99
*
10-
* Doesn't affect line if it doesn't contain [marginPrefix] except first and last blank lines.
10+
* Doesn't affect a line if it doesn't contain [marginPrefix] except the first and the last blank lines.
1111
*
12-
* Doesn't preserve original line endings.
12+
* Doesn't preserve the original line endings.
1313
*
14-
* Example
15-
* ```kotlin
16-
* assertEquals("ABC\n123\n456", """ABC
17-
* |123
18-
* |456""".trimMargin())
19-
* ```
14+
* @param marginPrefix non-blank string, which is used as a margin delimiter. Default is `|` (pipe character).
2015
*
21-
* @param marginPrefix non-blank string, characters to be used as a margin delimiter. Default is `|` (pipe character).
22-
* @return deindented String
16+
* @sample samples.text.Strings.trimMargin
17+
* @see trimIndent
2318
* @see kotlin.text.isWhitespace
2419
*/
2520
public fun String.trimMargin(marginPrefix: String = "|"): String =
2621
replaceIndentByMargin("", marginPrefix)
2722

2823
/**
2924
* Detects indent by [marginPrefix] as it does [trimMargin] and replace it with [newIndent].
25+
*
26+
* @param marginPrefix non-blank string, which is used as a margin delimiter. Default is `|` (pipe character).
3027
*/
3128
public fun String.replaceIndentByMargin(newIndent: String = "", marginPrefix: String = "|"): String {
3229
require(marginPrefix.isNotBlank()) { "marginPrefix must be non-blank string." }
@@ -44,25 +41,18 @@ public fun String.replaceIndentByMargin(newIndent: String = "", marginPrefix: St
4441
}
4542

4643
/**
47-
* Detects a common minimal indent of all the input lines, removes it from every line and also removes first and last
44+
* Detects a common minimal indent of all the input lines, removes it from every line and also removes the first and the last
4845
* lines if they are blank (notice difference blank vs empty).
4946
*
50-
* Note that blank lines do not affect detected indent level.
51-
*
52-
* Please keep in mind that if there are non-blank lines with no leading whitespace characters (no indent at all) then the
53-
* common indent is 0 so this function may do nothing so it is recommended to keep first line empty (will be dropped).
47+
* Note that blank lines do not affect the detected indent level.
5448
*
55-
* Doesn't preserve original line endings.
49+
* In case if there are non-blank lines with no leading whitespace characters (no indent at all) then the
50+
* common indent is 0, and therefore this function doesn't change the indentation.
5651
*
57-
* Example
58-
* ```kotlin
59-
* assertEquals("ABC\n123\n456", """
60-
* ABC
61-
* 123
62-
* 456""".trimIndent())
63-
* ```
52+
* Doesn't preserve the original line endings.
6453
*
65-
* @return deindented String
54+
* @sample samples.text.Strings.trimIndent
55+
* @see trimMargin
6656
* @see kotlin.text.isBlank
6757
*/
6858
public fun String.trimIndent(): String = replaceIndent("")
@@ -84,7 +74,7 @@ public fun String.replaceIndent(newIndent: String = ""): String {
8474
/**
8575
* Prepends [indent] to every line of the original string.
8676
*
87-
* Doesn't preserve original line endings.
77+
* Doesn't preserve the original line endings.
8878
*/
8979
public fun String.prependIndent(indent: String = " "): String =
9080
lineSequence()

0 commit comments

Comments
 (0)