Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
## 2024-05-24 - Loop Allocation Hot Paths
**Learning:** Rendering directory entries with repeated string concatenation and list-based exclusion lookups creates avoidable allocation and lookup cost in large directories.
**Action:** Use `StringBuilder` for entry rendering and a `Set` for excluded file names.

## 2024-07-02 - String Escape Optimization
**Learning:** Multiple passes of `.replace()` on strings cause significant performance degradation due to intermediate string allocations per replacement call.
**Action:** Use a single-pass `StringBuilder` loop to avoid redundant string allocations when escaping HTML or doing multiple character replacements, after checking if an escape is even needed.
35 changes: 27 additions & 8 deletions src/main/kotlin/html4tree/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ fun main(args: Array<String>) = Html4tree().main(args)

fun go(topDir: String, maxLevel: Int) {
require(topDir.isNotBlank())
val top_dir = File(topDir).canonicalFile
require(Files.isDirectory(top_dir.toPath(), LinkOption.NOFOLLOW_LINKS)) { "Top directory must be an existing non-symlink directory" }
val original_dir = File(topDir).absoluteFile
require(Files.isDirectory(original_dir.toPath(), LinkOption.NOFOLLOW_LINKS)) { "Top directory must be an existing non-symlink directory" }
val top_dir = original_dir.canonicalFile

val ll = LinkedList()

Expand All @@ -49,12 +50,30 @@ fun go(topDir: String, maxLevel: Int) {
}

fun String.escapeHtml(): String {
return this.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace("\"", "&quot;")
.replace("'", "&#x27;")
.replace("`", "&#x60;")
var sb: java.lang.StringBuilder? = null
for (i in 0 until this.length) {
val c = this[i]
val escaped = when (c) {
'&' -> "&amp;"
'<' -> "&lt;"
'>' -> "&gt;"
'"' -> "&quot;"
'\'' -> "&#x27;"
'`' -> "&#x60;"
else -> null
}

if (escaped != null) {
if (sb == null) {
sb = java.lang.StringBuilder(this.length + 16)
sb.append(this, 0, i)
}
sb.append(escaped)
} else {
sb?.append(c)
}
}
return sb?.toString() ?: this
}

fun String.urlEncodePath(): String {
Expand Down
10 changes: 10 additions & 0 deletions src/test/kotlin/html4tree/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class MainTest {
assertEquals("&#x60;", "`".escapeHtml())
assertEquals("&amp;&lt;&gt;&quot;&#x27;&#x60;", "&<>\"'`".escapeHtml())
assertEquals("normal text", "normal text".escapeHtml())
assertEquals("mixed &amp; and &lt; text &gt; here", "mixed & and < text > here".escapeHtml())
}

@Test
Expand Down Expand Up @@ -85,6 +86,15 @@ class MainTest {
}
}

@Test
fun testGoRejectsNonExistentDir() {
val nonExistentDir = File(tempDir, "non_existent_dir_${System.currentTimeMillis()}")
assertFalse(nonExistentDir.exists(), "Test directory should not exist initially")
assertFailsWith<IllegalArgumentException> {
go(nonExistentDir.absolutePath, -1)
}
}
Comment on lines +90 to 96

@Test
fun testGoEmptyDir() {
go(tempDir.absolutePath, -1)
Expand Down
Loading