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`)
*
* @see TwoCycleFormatter
* @see de.staropensource.engine.logging.implementable.Formatter
* @see Formatter
* @since v1-alpha10
*/
abstract class TwoCycleFormatterImpl: TwoCycleFormatter() {

View file

@ -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

View file

@ -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)
}
/**

View file

@ -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}"
append("\n${entryString}"
.replace(
"\n",
"\n ${" ".repeat(indent)}"
)
)
else
builder.append(": ${entryString}")
}
}
return builder.toString()
append(": ${entryString}")
}
}
}
}
}

View file

@ -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()) builder.append("r")
if (isWritable()) builder.append("w")
if (isExecutable()) builder.append("x")
if (isReadable()) append("r")
if (isWritable()) append("w")
if (isExecutable()) append("x")
// Repeat two times to match the format
builder.repeat(2)
return builder.toString()
repeat(2)
}
} else
return null
null
} catch (exception: Exception) {
throw IOAccessException(exception.message, exception.cause)
}

View file

@ -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.