Use buildString method for some StringBuilders

This commit is contained in:
JeremyStar™ 2024-12-20 18:31:19 +01:00
parent 6eca780ebf
commit 9866b9b0dd
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
6 changed files with 32 additions and 40 deletions

View file

@ -39,7 +39,7 @@ import kotlin.text.iterator
* - `ATTRIBUTE:` (example `ATTRIBUTE:BOLD`) * - `ATTRIBUTE:` (example `ATTRIBUTE:BOLD`)
* *
* @see TwoCycleFormatter * @see TwoCycleFormatter
* @see de.staropensource.engine.logging.implementable.Formatter * @see Formatter
* @since v1-alpha10 * @since v1-alpha10
*/ */
abstract class TwoCycleFormatterImpl: TwoCycleFormatter() { abstract class TwoCycleFormatterImpl: TwoCycleFormatter() {

View file

@ -49,9 +49,9 @@ class NoOperationFormatter private constructor(): OneCycleFormatter() {
@Suppress("NestedBlockDepth") @Suppress("NestedBlockDepth")
override fun parseAndFormat(message: String): String { override fun parseAndFormat(message: String): String {
val output: StringBuilder = StringBuilder() val output: StringBuilder = StringBuilder()
var currentComponent: StringBuilder = StringBuilder()
var escaped: Boolean = false var escaped: Boolean = false
var inTag: Boolean = false var inTag: Boolean = false
var currentComponent: StringBuilder = StringBuilder()
// This iteration loop should // This iteration loop should
// be readable enough to not // be readable enough to not

View file

@ -58,27 +58,23 @@ open class SOSLSv2FormatBuilder(call: Call) : FormatBuilder(call) {
* @return finalized format * @return finalized format
* @since v1-alpha10 * @since v1-alpha10
*/ */
fun format(message: Boolean): String { fun format(message: Boolean): String = buildString {
val format: StringBuilder = StringBuilder() addRuntime(this)
addDateTime(this)
addRuntime(format)
addDateTime(format)
// Add level, origin & other metadata // Add level, origin & other metadata
if (enabledFeatures.contains(Feature.LEVEL) || enabledFeatures.contains(Feature.ORIGIN)) { if (enabledFeatures.contains(Feature.LEVEL) || enabledFeatures.contains(Feature.ORIGIN)) {
format.append("[") append("[")
addLevel(format) addLevel(this)
if (enabledFeatures.contains(Feature.LEVEL) && enabledFeatures.contains(Feature.ORIGIN)) if (enabledFeatures.contains(Feature.LEVEL) && enabledFeatures.contains(Feature.ORIGIN))
format.append(" ") append(" ")
addOriginAndMetadata(format) addOriginAndMetadata(this)
format.append("] ") append("] ")
} }
// Message // Message
if (message) if (message)
format.append(this.message) append(this@SOSLSv2FormatBuilder.message)
return format.toString()
} }
/** /**

View file

@ -97,8 +97,7 @@ class CrashHandler private constructor() {
* @return compiled output * @return compiled output
* @since v1-alpha10 * @since v1-alpha10
*/ */
private fun compileCategory(map: LinkedHashMap<*, *>, indent: Int = 1): String { private fun compileCategory(map: LinkedHashMap<*, *>, indent: Int = 1): String = buildString {
val builder: StringBuilder = StringBuilder()
var entryString: String? = null var entryString: String? = null
// Iterate over all entries // Iterate over all entries
@ -107,12 +106,12 @@ class CrashHandler private constructor() {
if (entry !is String) if (entry !is String)
continue continue
builder.append("\n${" ".repeat(indent)}-> ${entry}") append("\n${" ".repeat(indent)}-> ${entry}")
if (map[entry] == null) // Value is null if (map[entry] == null) // Value is null
true true
else if (map[entry] is LinkedHashMap<*, *>) // Value is a map else if (map[entry] is LinkedHashMap<*, *>) // Value is a map
builder.append( append(
compileCategory( compileCategory(
map[entry] as LinkedHashMap<*, *>, map[entry] as LinkedHashMap<*, *>,
indent = indent + 3 // increase the 2nd addend to change the indent size during recursion 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 // Put on separate line if contains newline
if (entryString.contains("\n")) if (entryString.contains("\n"))
builder append("\n${entryString}"
.append("\n${entryString}" .replace(
.replace( "\n",
"\n", "\n ${" ".repeat(indent)}"
"\n ${" ".repeat(indent)}"
)
) )
)
else else
builder.append(": ${entryString}") append(": ${entryString}")
} }
} }
return builder.toString()
} }
} }
} }

View file

@ -448,20 +448,18 @@ class FileAccess {
try { try {
return PosixFilePermissions.toString(Files.getPosixFilePermissions(path)) return PosixFilePermissions.toString(Files.getPosixFilePermissions(path))
} catch (exception: UnsupportedOperationException) { } catch (exception: UnsupportedOperationException) {
if (fakeOnUnsupported) { return if (fakeOnUnsupported) {
val builder: StringBuilder = StringBuilder() buildString {
// Add permissions
if (isReadable()) append("r")
if (isWritable()) append("w")
if (isExecutable()) append("x")
// Add permissions // Repeat two times to match the format
if (isReadable()) builder.append("r") repeat(2)
if (isWritable()) builder.append("w") }
if (isExecutable()) builder.append("x")
// Repeat two times to match the format
builder.repeat(2)
return builder.toString()
} else } else
return null null
} catch (exception: Exception) { } catch (exception: Exception) {
throw IOAccessException(exception.message, exception.cause) throw IOAccessException(exception.message, exception.cause)
} }

View file

@ -20,7 +20,9 @@
package de.staropensource.engine.testapp package de.staropensource.engine.testapp
import de.staropensource.engine.base.Engine import de.staropensource.engine.base.Engine
import de.staropensource.engine.base.EngineConfiguration
import de.staropensource.engine.base.logging.Logger import de.staropensource.engine.base.logging.Logger
import de.staropensource.engine.base.type.logging.Level
/** /**
* Testing program for the StarOpenSource Engine. * Testing program for the StarOpenSource Engine.