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-03 - Inefficient String Escaping
**Learning:** Chaining `replace` calls on a string in Kotlin allocates multiple intermediate strings and causes O(N) allocation overhead for a simple escaping process.
**Action:** Replace multiple `replace` calls with a single-pass `StringBuilder` loop that checks if escaping is needed first, drastically reducing allocation and execution time.
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,12 +23,13 @@ fun main(args: Array<String>) = Html4tree().main(args)

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

val ll = LinkedList()

ll.push(LinkedListEntry(top_dir,0))
ll.push(LinkedListEntry(canonical_top_dir,0))

var lle: LinkedListEntry? = ll.pull()

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 hasSpecial = false
for (i in 0 until length) {
val c = this[i]
if (c == '&' || c == '<' || c == '>' || c == '"' || c == '\'' || c == '`') {
hasSpecial = true
break
}
}
if (!hasSpecial) return this

val sb = StringBuilder(length + 16)
for (i in 0 until length) {
val c = this[i]
when (c) {
'&' -> sb.append("&amp;")
'<' -> sb.append("&lt;")
'>' -> sb.append("&gt;")
'"' -> sb.append("&quot;")
'\'' -> sb.append("&#x27;")
'`' -> sb.append("&#x60;")
else -> sb.append(c)
}
}
return sb.toString()
}

fun String.urlEncodePath(): String {
Expand Down
24 changes: 24 additions & 0 deletions src/test/kotlin/html4tree/EscapeHtmlTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package html4tree

import org.junit.Test
import kotlin.test.assertEquals

class EscapeHtmlTest {
@Test
fun testEscapeHtmlNoSpecial() {
val s = "no special chars"
assertEquals("no special chars", s.escapeHtml())
}

@Test
fun testEscapeHtmlAllSpecial() {
val s = "&<>\"'`"
assertEquals("&amp;&lt;&gt;&quot;&#x27;&#x60;", s.escapeHtml())
}

@Test
fun testEscapeHtmlMixed() {
val s = "a&b<c>d\"e'f`g"
assertEquals("a&amp;b&lt;c&gt;d&quot;e&#x27;f&#x60;g", s.escapeHtml())
}
}