From 96b33a8bfdd7da59065bcecc846db88431c4fe9f Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 9 Oct 2015 19:00:18 +0300 Subject: [PATCH] Minor reindent optimizations --- libraries/stdlib/src/kotlin/text/Indent.kt | 27 +++++++++++-------- .../kotlin-stdlib-gen/src/templates/Engine.kt | 2 +- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/libraries/stdlib/src/kotlin/text/Indent.kt b/libraries/stdlib/src/kotlin/text/Indent.kt index fa87f7f1dfb..91649aa20e8 100644 --- a/libraries/stdlib/src/kotlin/text/Indent.kt +++ b/libraries/stdlib/src/kotlin/text/Indent.kt @@ -34,7 +34,7 @@ public fun String.replaceIndentByMargin(newIndent: String = "", marginPrefix: St require(marginPrefix.isNotBlank()) { "marginPrefix should be non blank string but it is '$marginPrefix'" } val lines = lines() - return lines.reindent(length() + newIndent.length() * lines.size(), getIndentFunction(newIndent)) { line -> + return lines.reindent(length() + newIndent.length() * lines.size(), getIndentFunction(newIndent), { line -> val firstNonWhitespaceIndex = line.indexOfFirst { !it.isWhitespace() } when { @@ -42,7 +42,7 @@ public fun String.replaceIndentByMargin(newIndent: String = "", marginPrefix: St line.startsWith(marginPrefix, firstNonWhitespaceIndex) -> line.substring(firstNonWhitespaceIndex + marginPrefix.length()) else -> null } - } + }) } /** @@ -80,7 +80,7 @@ public fun String.replaceIndent(newIndent: String = ""): String { .map { it.indentWidth() } .min() ?: 0 - return lines.reindent(length() + newIndent.length() * lines.size(), getIndentFunction(newIndent)) { line -> line.drop(minCommonIndent) } + return lines.reindent(length() + newIndent.length() * lines.size(), getIndentFunction(newIndent), { line -> line.drop(minCommonIndent) }) } /** @@ -105,14 +105,19 @@ private fun String.indentWidth(): Int = indexOfFirst { !it.isWhitespace() }.let private fun getIndentFunction(indent: String) = when { indent.isEmpty() -> { line: String -> line } - else -> { line: String -> "$indent$line" } + else -> { line: String -> indent.concat(line) } } -private inline fun List.reindent(resultSizeEstimate: Int, indentAddFunction: (String) -> String, indentCutFunction: (String) -> String?) = lastIndex.let { lastLineIndex -> - withIndex() - .dropWhile { it.index == 0 && it.value.isBlank() } - .takeWhile { it.index != lastLineIndex || it.value.isNotBlank() } - .map { indentCutFunction(it.value)?.let { cutted -> indentAddFunction(cutted) } ?: it.value } - .joinTo(StringBuilder(resultSizeEstimate), "\n") - .toString() +private inline fun List.reindent(resultSizeEstimate: Int, indentAddFunction: (String) -> String, indentCutFunction: (String) -> String?): String { + val lastIndex = lastIndex + // TODO: Use mapNotNullIndexed + return mapIndexed { index, value -> + if ((index == 0 || index == lastIndex) && value.isBlank()) + null + else + indentCutFunction(value)?.let { cutted -> indentAddFunction(cutted) } ?: value + } + .filterNotNull() + .joinTo(StringBuilder(resultSizeEstimate), "\n") + .toString() } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt index 5a9dd6d70a0..580c6e69432 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt @@ -399,7 +399,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") { val indent: Int = body.takeWhile { it == ' ' }.length() builder.append('\n') - StringReader(body).forEachLine { + body.lineSequence().forEach { var count = indent val line = it.dropWhile { count-- > 0 && it == ' ' }.renderType() if (!line.isEmpty()) {