From 9866b9b0dd76f82b31909ce81990ee90022e08f4 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Fri, 20 Dec 2024 18:31:19 +0100 Subject: [PATCH] Use buildString method for some StringBuilders --- .../formatter/TwoCycleFormatterImpl.kt | 2 +- .../logging/NoOperationFormatter.kt | 2 +- .../formatbuilder/SOSLSv2FormatBuilder.kt | 22 ++++++++----------- .../engine/base/logging/CrashHandler.kt | 22 ++++++++----------- .../engine/base/utility/FileAccess.kt | 22 +++++++++---------- .../de/staropensource/engine/testapp/Main.kt | 2 ++ 6 files changed, 32 insertions(+), 40 deletions(-) diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementable/formatter/TwoCycleFormatterImpl.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementable/formatter/TwoCycleFormatterImpl.kt index b7213de08..34055ecbc 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/implementable/formatter/TwoCycleFormatterImpl.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementable/formatter/TwoCycleFormatterImpl.kt @@ -39,7 +39,7 @@ import kotlin.text.iterator * - `ATTRIBUTE:` (example `ATTRIBUTE:BOLD`) * * @see TwoCycleFormatter - * @see de.staropensource.engine.logging.implementable.Formatter + * @see Formatter * @since v1-alpha10 */ abstract class TwoCycleFormatterImpl: TwoCycleFormatter() { diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/NoOperationFormatter.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/NoOperationFormatter.kt index a5bbc7c2b..f845e839e 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/NoOperationFormatter.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/NoOperationFormatter.kt @@ -49,9 +49,9 @@ class NoOperationFormatter private constructor(): OneCycleFormatter() { @Suppress("NestedBlockDepth") override fun parseAndFormat(message: String): String { val output: StringBuilder = StringBuilder() + var currentComponent: StringBuilder = StringBuilder() var escaped: Boolean = false var inTag: Boolean = false - var currentComponent: StringBuilder = StringBuilder() // This iteration loop should // be readable enough to not diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/formatbuilder/SOSLSv2FormatBuilder.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/formatbuilder/SOSLSv2FormatBuilder.kt index c1c8a3ea6..19145ed7d 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/formatbuilder/SOSLSv2FormatBuilder.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/formatbuilder/SOSLSv2FormatBuilder.kt @@ -58,27 +58,23 @@ open class SOSLSv2FormatBuilder(call: Call) : FormatBuilder(call) { * @return finalized format * @since v1-alpha10 */ - fun format(message: Boolean): String { - val format: StringBuilder = StringBuilder() - - addRuntime(format) - addDateTime(format) + fun format(message: Boolean): String = buildString { + addRuntime(this) + addDateTime(this) // Add level, origin & other metadata if (enabledFeatures.contains(Feature.LEVEL) || enabledFeatures.contains(Feature.ORIGIN)) { - format.append("[") - addLevel(format) + append("[") + addLevel(this) if (enabledFeatures.contains(Feature.LEVEL) && enabledFeatures.contains(Feature.ORIGIN)) - format.append(" ") - addOriginAndMetadata(format) - format.append("] ") + append(" ") + addOriginAndMetadata(this) + append("] ") } // Message if (message) - format.append(this.message) - - return format.toString() + append(this@SOSLSv2FormatBuilder.message) } /** diff --git a/base/src/main/kotlin/de/staropensource/engine/base/logging/CrashHandler.kt b/base/src/main/kotlin/de/staropensource/engine/base/logging/CrashHandler.kt index 7a1ef60d6..49d9d5f47 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/logging/CrashHandler.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/logging/CrashHandler.kt @@ -97,8 +97,7 @@ class CrashHandler private constructor() { * @return compiled output * @since v1-alpha10 */ - private fun compileCategory(map: LinkedHashMap<*, *>, indent: Int = 1): String { - val builder: StringBuilder = StringBuilder() + private fun compileCategory(map: LinkedHashMap<*, *>, indent: Int = 1): String = buildString { var entryString: String? = null // Iterate over all entries @@ -107,12 +106,12 @@ class CrashHandler private constructor() { if (entry !is String) continue - builder.append("\n${" ".repeat(indent)}-> ${entry}") + append("\n${" ".repeat(indent)}-> ${entry}") if (map[entry] == null) // Value is null true else if (map[entry] is LinkedHashMap<*, *>) // Value is a map - builder.append( + append( compileCategory( map[entry] as LinkedHashMap<*, *>, indent = indent + 3 // increase the 2nd addend to change the indent size during recursion @@ -123,19 +122,16 @@ class CrashHandler private constructor() { // Put on separate line if contains newline if (entryString.contains("\n")) - builder - .append("\n${entryString}" - .replace( - "\n", - "\n ${" ".repeat(indent)}" - ) + append("\n${entryString}" + .replace( + "\n", + "\n ${" ".repeat(indent)}" ) + ) else - builder.append(": ${entryString}") + append(": ${entryString}") } } - - return builder.toString() } } } diff --git a/base/src/main/kotlin/de/staropensource/engine/base/utility/FileAccess.kt b/base/src/main/kotlin/de/staropensource/engine/base/utility/FileAccess.kt index c6c7c8698..a1f444d77 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/utility/FileAccess.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/utility/FileAccess.kt @@ -448,20 +448,18 @@ class FileAccess { try { return PosixFilePermissions.toString(Files.getPosixFilePermissions(path)) } catch (exception: UnsupportedOperationException) { - if (fakeOnUnsupported) { - val builder: StringBuilder = StringBuilder() + return if (fakeOnUnsupported) { + buildString { + // Add permissions + if (isReadable()) append("r") + if (isWritable()) append("w") + if (isExecutable()) append("x") - // Add permissions - if (isReadable()) builder.append("r") - if (isWritable()) builder.append("w") - if (isExecutable()) builder.append("x") - - // Repeat two times to match the format - builder.repeat(2) - - return builder.toString() + // Repeat two times to match the format + repeat(2) + } } else - return null + null } catch (exception: Exception) { throw IOAccessException(exception.message, exception.cause) } diff --git a/testapp/src/main/kotlin/de/staropensource/engine/testapp/Main.kt b/testapp/src/main/kotlin/de/staropensource/engine/testapp/Main.kt index bf2230584..eeede52b6 100644 --- a/testapp/src/main/kotlin/de/staropensource/engine/testapp/Main.kt +++ b/testapp/src/main/kotlin/de/staropensource/engine/testapp/Main.kt @@ -20,7 +20,9 @@ package de.staropensource.engine.testapp import de.staropensource.engine.base.Engine +import de.staropensource.engine.base.EngineConfiguration import de.staropensource.engine.base.logging.Logger +import de.staropensource.engine.base.type.logging.Level /** * Testing program for the StarOpenSource Engine.