Skip to content

Commit

Permalink
minor tweaks (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
itboy87 authored Sep 28, 2024
1 parent 98b7d8a commit 0382dc3
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 63 deletions.
32 changes: 32 additions & 0 deletions ksoup-benchmark/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,37 @@ benchmark {
// exclude("com.fleeksoft.ksoup.benchmark.KsoupBenchmark")
}
}
}

val rootPath = "generated/kotlin"
kotlin {
sourceSets {
commonMain {
this.kotlin.srcDir(layout.buildDirectory.file(rootPath))
}
}
}

val generateBuildConfigFile: Task by tasks.creating {
group = "build setup"
val file = layout.buildDirectory.file("$rootPath/BuildConfig.kt")
outputs.file(file)

doLast {
val content =
"""
package com.fleeksoft.ksoup
object BuildConfig {
const val PROJECT_ROOT: String = "${rootProject.rootDir.absolutePath.replace("\\", "\\\\")}"
}
""".trimIndent()
file.get().asFile.writeText(content)
}
}

tasks.configureEach {
if (name != generateBuildConfigFile.name && !name.contains("publish", ignoreCase = true)) {
dependsOn(generateBuildConfigFile.name)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.fleeksoft.ksoup.benchmark

import com.fleeksoft.ksoup.BuildConfig
import com.fleeksoft.ksoup.Ksoup
import com.fleeksoft.ksoup.nodes.Document
import com.fleeksoft.ksoup.nodes.Element
import com.fleeksoft.ksoup.select.Elements
import com.fleeksoft.ksoup.select.Evaluator
import kotlinx.benchmark.*
import kotlinx.io.buffered
import kotlinx.io.files.Path
Expand All @@ -22,7 +20,7 @@ class KsoupBenchmark {
@Setup
fun setUp() {
fileData =
SystemFileSystem.source(Path("/Users/sabeeh/IdeaProjects/ksoup-benchmark/ksoup-test/testResources/test.txt")).buffered().readString()
SystemFileSystem.source(Path("${BuildConfig.PROJECT_ROOT}/ksoup-test/testResources/test.txt")).buffered().readString()
doc1 = parseHtml()
}

Expand Down
3 changes: 2 additions & 1 deletion ksoup-benchmark/src@jvm/org/jsoup/parser/JsoupBenchmark.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jsoup.parser

import com.fleeksoft.ksoup.BuildConfig
import kotlinx.benchmark.*
import kotlinx.io.buffered
import kotlinx.io.files.Path
Expand All @@ -18,7 +19,7 @@ class JsoupBenchmark {
@Setup
fun setUp() {
fileData =
SystemFileSystem.source(Path("/Users/sabeeh/IdeaProjects/ksoup-benchmark/ksoup-test/testResources/test.txt")).buffered().readString()
SystemFileSystem.source(Path("${BuildConfig.PROJECT_ROOT}/ksoup-test/testResources/test.txt")).buffered().readString()
doc1 = parseHtml()
}

Expand Down
32 changes: 16 additions & 16 deletions ksoup-engine-common/src/com/fleeksoft/ksoup/io/KByteBuffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ class KByteBuffer(capacity: Int) {
}

fun compact() {
if (position == buffer.size || readAvailable == 0) {
position = 0
offset = 0
} else if (position > 0) {
val length = size - position
(0 until length).forEach { i ->
buffer[i] = buffer[i + position]
if (readAvailable > 0) {
if (position > 0) {
// Use copyInto for efficient copying
buffer.copyInto(buffer, destinationOffset = 0, startIndex = position, endIndex = position + readAvailable)
}
offset = length
offset = readAvailable
position = 0
} else {
position = 0
offset = 0
}
}

Expand All @@ -42,12 +42,12 @@ class KByteBuffer(capacity: Int) {
}

fun clone(): KByteBuffer {
return KByteBuffer(buffer.size).apply {
position = this@KByteBuffer.position
readAvailable = this@KByteBuffer.readAvailable
offset = this@KByteBuffer.offset
this@KByteBuffer.buffer.copyInto(buffer)
}
val kByteBuffer = KByteBuffer(buffer.size)
kByteBuffer.position = this.position
kByteBuffer.readAvailable = this.readAvailable
kByteBuffer.offset = this.offset
this.buffer.copyInto(kByteBuffer.buffer)
return kByteBuffer
}

fun readText(charset: Charset, maxBytes: Int): String {
Expand All @@ -74,14 +74,14 @@ class KByteBuffer(capacity: Int) {
}

fun readBytes(count: Int): ByteArray {
val byteArray = buffer.sliceArray(position until min(position + count, position + readAvailable))
val byteArray = buffer.copyOfRange(position, min(position + count, position + readAvailable))
position += byteArray.size
readAvailable -= byteArray.size
return byteArray
}

fun readAll(): ByteArray {
return buffer.sliceArray(position until position + readAvailable).also {
return buffer.copyOfRange(position, position + readAvailable).also {
position += readAvailable
readAvailable = 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class SourceReaderByteArray(bytes: ByteArray) : SourceReader {
return if (i == 0) {
byteArrayOf()
} else if (i != count) {
byteArray.sliceArray(0 until i)
byteArray.copyOfRange(0, i)
} else {
byteArray
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class SourceReaderImpl : SourceReader {
return if (i == 0) {
byteArrayOf()
} else if (i != count) {
byteArray.sliceArray(0 until i)
byteArray.copyOfRange(0, i)
} else {
byteArray
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class SourceReaderImpl : SourceReader {
return if (i == 0) {
byteArrayOf()
} else if (i != count) {
byteArray.sliceArray(0 until i)
byteArray.copyOfRange(0, i)
} else {
byteArray
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal class SourceReaderImpl : SourceReader {
return if (i == 0) {
byteArrayOf()
} else if (i != count) {
byteArray.sliceArray(0 until i)
byteArray.copyOfRange(0, i)
} else {
byteArray
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class KByteBufferTest {
val data = "äöü".toByteArray(Charsets.forName("UTF-8")) // Multi-byte characters

// Write partial data to simulate incomplete multi-byte characters
buffer.writeBytes(data.sliceArray(0 until 4), 4) // First character (ä) is 2 bytes
buffer.writeBytes(data.copyOfRange(0, 4), 4) // First character (ä) is 2 bytes

// Attempt to decode part of the data (e.g., max = 3, not enough to decode full multi-byte character)
val result = buffer.readText(Charsets.forName("UTF-8"), 3)
Expand All @@ -164,7 +164,7 @@ class KByteBufferTest {
val data = "äöü".toByteArray(Charsets.forName("UTF-8")) // Multi-byte characters

// Write partial data (2 bytes, just enough to complete the first character ä)
buffer.writeBytes(data.sliceArray(0 until 2), 2)
buffer.writeBytes(data.copyOfRange(0, 2), 2)

// Attempt to decode the data
val result = buffer.readText(Charsets.forName("UTF-8"), 2)
Expand All @@ -175,7 +175,7 @@ class KByteBufferTest {
assertEquals(2, buffer.position())

// Write more data to complete the next character
buffer.writeBytes(data.sliceArray(2 until data.size), data.size - 2)
buffer.writeBytes(data.copyOfRange(2, data.size), data.size - 2)
assertEquals(data.size - 2, buffer.available())
assertEquals(2, buffer.position())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class ReaderTest {
}

private fun testMixCharReader(inputData: String) = readerStringTestStarter(inputData) { inputData, reader ->
inputData.toCharArray().forEach { char ->
inputData.forEach { char ->
val charArray = CharArray(1)
assertEquals(1, reader.read(charArray, 0, 1))
assertEquals(char, charArray[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class InputStreamReader {
}

private fun testMixCharReader(inputData: String) = readerStringTestStarter(inputData) { inputData, reader ->
inputData.toCharArray().forEach { char ->
inputData.forEach { char ->
val charArray = CharArray(1)
assertEquals(1, reader.read(charArray, 0, 1))
assertEquals(char, charArray[0])
Expand Down
8 changes: 2 additions & 6 deletions ksoup/src/com/fleeksoft/ksoup/nodes/Document.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ import com.fleeksoft.ksoup.select.Selector
public class Document(private val namespace: String, private val location: String?) :
Element(Tag.valueOf("#root", namespace, ParseSettings.htmlDefault), location) {
private var outputSettings = OutputSettings()
private var parser: Parser?
private var parser: Parser = Parser.htmlParser() // default, but overridable
private var quirksMode = QuirksMode.noQuirks
private var updateMetaCharset = false

init {
parser = Parser.htmlParser() // default, but overridable
}

/**
* Create a new, empty Document, in the HTML namespace.
* @param baseUri base URI of document
Expand Down Expand Up @@ -591,7 +587,7 @@ public class Document(private val namespace: String, private val location: Strin
* @param parser the configured parser to use when further parsing is required for this document.
* @return this document, for chaining.
*/
public fun parser(parser: Parser?): Document {
public fun parser(parser: Parser): Document {
this.parser = parser
return this
}
Expand Down
25 changes: 2 additions & 23 deletions ksoup/src/com/fleeksoft/ksoup/parser/TokenQueue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -341,29 +341,8 @@ public class TokenQueue(data: String) {
public companion object {
private const val ESC = '\\' // escape char for chomp balanced.

/**
* Unescape a \ escaped string.
* @param in backslash escaped string
* @return unescaped string
*/
/*fun unescape(`in`: String): String {
val out: StringBuilder = StringUtil.borrowBuilder()
var last = 0.toChar()
for (c in `in`.toCharArray()) {
if (c == ESC) {
if (last == ESC) {
out.append(c)
c = 0.toChar()
}
} else {
out.append(c)
}
last = c
}
return StringUtil.releaseBuilder(out)
}*/
public fun unescape(input: String): String {
val output = StringBuilder()
val output = StringUtil.borrowBuilder();
var lastChar: Char = 0.toChar()
for (c in input) {
var c1 = c
Expand All @@ -377,7 +356,7 @@ public class TokenQueue(data: String) {
}
lastChar = c1
}
return output.toString()
return StringUtil.releaseBuilder(output)
}

/*
Expand Down
6 changes: 3 additions & 3 deletions ksoup/src/com/fleeksoft/ksoup/ported/KsoupExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fun String.toByteArray(charset: Charset? = null): ByteArray = charset?.toByteArr
fun String.toSourceFile(): FileSource = KsoupEngineInstance.ksoupEngine.pathToFileSource(this)


public fun <T : Comparable<T>> Array<out T?>.binarySearch(element: T): Int {
inline fun <T : Comparable<T>> Array<out T?>.binarySearch(element: T): Int {
var low = 0
var high = this.size - 1

Expand All @@ -41,7 +41,7 @@ public fun <T : Comparable<T>> Array<out T?>.binarySearch(element: T): Int {
return -(low + 1) // key not found
}

fun IntArray.binarySearch(key: Int): Int {
inline fun IntArray.binarySearch(key: Int): Int {
var low = 0
var high = this.size - 1

Expand All @@ -57,7 +57,7 @@ fun IntArray.binarySearch(key: Int): Int {
}


public fun <T> Array<T>.binarySearchBy(comparison: (T) -> Int): Int {
inline fun <T> Array<T>.binarySearchBy(comparison: (T) -> Int): Int {

var low = 0
var high = size - 1
Expand Down
2 changes: 1 addition & 1 deletion ksoup/src/com/fleeksoft/ksoup/ported/io/BufferedReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class BufferedReader(reader: Reader, sz: Int = SharedConstants.DEFAULT_CHAR_BUFF
bufferLoop@ while (true) {
if (nextChar >= nChars) fill()
if (nextChar >= nChars) { /* EOF */
return if (s != null && s.length > 0) s.toString()
return if (s != null && s.isNotEmpty()) s.toString()
else null
}
var eol = false
Expand Down

0 comments on commit 0382dc3

Please sign in to comment.