Skip to content

Commit

Permalink
fix: Stackoverflow caused by overloading function
Browse files Browse the repository at this point in the history
  • Loading branch information
LichtHund committed Jul 6, 2024
1 parent 3b83880 commit 6c98eef
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 4 additions & 1 deletion common/src/main/kotlin/StringExt.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.triumphteam.website

public fun String.trim(word: String? = null, contextLength: Int = 20): String {
public fun String.trimAround(word: String? = null, contextLength: Int = 20): String {
val index = word?.let { indexOf(it, ignoreCase = true) } ?: 0
val wordLength = word?.length ?: 0

Expand All @@ -10,11 +10,14 @@ public fun String.trim(word: String? = null, contextLength: Int = 20): String {
start--
}

println(start)
// Find end index by moving forward contextLength characters and then to the next space
var end = (index + wordLength + contextLength).coerceAtMost(length)
while (end < length && !this[end].isWhitespace()) {
end++
}
println(end)
println(this)

val trimmed = substring(start, end).trim()

Expand Down
6 changes: 4 additions & 2 deletions docs/src/main/kotlin/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import dev.triumphteam.website.project.Navigation
import dev.triumphteam.website.project.Page
import dev.triumphteam.website.project.Project
import dev.triumphteam.website.project.Repository
import dev.triumphteam.website.trim
import dev.triumphteam.website.trimAround
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
Expand Down Expand Up @@ -125,6 +125,8 @@ public suspend fun main(args: Array<String>) {
},
)

println(JsonSerializer.encode<Repository>(projects.toRepository()))
return
val outputDir = File("output").also(File::mkdirs)

val repository = outputDir.resolve("repository.json").also {
Expand Down Expand Up @@ -229,7 +231,7 @@ private fun parseVersions(versionDirs: List<File>, parentDir: File, repoSettings
path = "${repoSettings.editPath.removeSuffix("/")}/${pageFile.relativeTo(parentDir).path}",
description = Page.Description(
title = title,
subTitle = subTitle?.trim(contextLength = 100),
subTitle = subTitle?.trimAround(contextLength = 100),
group = parsedGroupConfig.header,
summary = summaryExtractor.extract(parsedFile),
),
Expand Down

0 comments on commit 6c98eef

Please sign in to comment.