From 0382dc33b67d9c0cfdd0e285adbb07597ec89d70 Mon Sep 17 00:00:00 2001 From: Sabeeh Ul Hussnain Date: Sat, 28 Sep 2024 16:08:03 +0500 Subject: [PATCH] minor tweaks (#88) --- ksoup-benchmark/build.gradle.kts | 32 +++++++++++++++++++ .../ksoup/benchmark/KsoupBenchmark.kt | 6 ++-- .../org/jsoup/parser/JsoupBenchmark.kt | 3 +- .../src/com/fleeksoft/ksoup/io/KByteBuffer.kt | 32 +++++++++---------- .../ksoup/io/SourceReaderByteArray.kt | 2 +- .../fleeksoft/ksoup/io/SourceReaderImpl.kt | 2 +- .../fleeksoft/ksoup/io/SourceReaderImpl.kt | 2 +- .../fleeksoft/ksoup/io/SourceReaderImpl.kt | 2 +- .../ksoup/ported/io/KByteBufferTest.kt | 6 ++-- .../fleeksoft/ksoup/ported/io/ReaderTest.kt | 2 +- .../ksoup/io/InputStreamReader.test.kt | 2 +- .../src/com/fleeksoft/ksoup/nodes/Document.kt | 8 ++--- .../com/fleeksoft/ksoup/parser/TokenQueue.kt | 25 ++------------- .../com/fleeksoft/ksoup/ported/KsoupExt.kt | 6 ++-- .../ksoup/ported/io/BufferedReader.kt | 2 +- 15 files changed, 69 insertions(+), 63 deletions(-) diff --git a/ksoup-benchmark/build.gradle.kts b/ksoup-benchmark/build.gradle.kts index 0fdce0d6..5b9dbfbe 100644 --- a/ksoup-benchmark/build.gradle.kts +++ b/ksoup-benchmark/build.gradle.kts @@ -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) + } } diff --git a/ksoup-benchmark/src@jvm/com/fleeksoft/ksoup/benchmark/KsoupBenchmark.kt b/ksoup-benchmark/src@jvm/com/fleeksoft/ksoup/benchmark/KsoupBenchmark.kt index 289465ff..dd4ebf1a 100644 --- a/ksoup-benchmark/src@jvm/com/fleeksoft/ksoup/benchmark/KsoupBenchmark.kt +++ b/ksoup-benchmark/src@jvm/com/fleeksoft/ksoup/benchmark/KsoupBenchmark.kt @@ -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 @@ -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() } diff --git a/ksoup-benchmark/src@jvm/org/jsoup/parser/JsoupBenchmark.kt b/ksoup-benchmark/src@jvm/org/jsoup/parser/JsoupBenchmark.kt index 47f439eb..bf2095ef 100644 --- a/ksoup-benchmark/src@jvm/org/jsoup/parser/JsoupBenchmark.kt +++ b/ksoup-benchmark/src@jvm/org/jsoup/parser/JsoupBenchmark.kt @@ -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 @@ -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() } diff --git a/ksoup-engine-common/src/com/fleeksoft/ksoup/io/KByteBuffer.kt b/ksoup-engine-common/src/com/fleeksoft/ksoup/io/KByteBuffer.kt index 2b0f4157..d9fad604 100644 --- a/ksoup-engine-common/src/com/fleeksoft/ksoup/io/KByteBuffer.kt +++ b/ksoup-engine-common/src/com/fleeksoft/ksoup/io/KByteBuffer.kt @@ -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 } } @@ -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 { @@ -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 } diff --git a/ksoup-engine-common/src/com/fleeksoft/ksoup/io/SourceReaderByteArray.kt b/ksoup-engine-common/src/com/fleeksoft/ksoup/io/SourceReaderByteArray.kt index ebaa89ca..9aafca57 100644 --- a/ksoup-engine-common/src/com/fleeksoft/ksoup/io/SourceReaderByteArray.kt +++ b/ksoup-engine-common/src/com/fleeksoft/ksoup/io/SourceReaderByteArray.kt @@ -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 } diff --git a/ksoup-engine-kotlinx/src/com/fleeksoft/ksoup/io/SourceReaderImpl.kt b/ksoup-engine-kotlinx/src/com/fleeksoft/ksoup/io/SourceReaderImpl.kt index 706ec963..5735c8d5 100644 --- a/ksoup-engine-kotlinx/src/com/fleeksoft/ksoup/io/SourceReaderImpl.kt +++ b/ksoup-engine-kotlinx/src/com/fleeksoft/ksoup/io/SourceReaderImpl.kt @@ -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 } diff --git a/ksoup-engine-ktor2/src/com/fleeksoft/ksoup/io/SourceReaderImpl.kt b/ksoup-engine-ktor2/src/com/fleeksoft/ksoup/io/SourceReaderImpl.kt index 706ec963..5735c8d5 100644 --- a/ksoup-engine-ktor2/src/com/fleeksoft/ksoup/io/SourceReaderImpl.kt +++ b/ksoup-engine-ktor2/src/com/fleeksoft/ksoup/io/SourceReaderImpl.kt @@ -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 } diff --git a/ksoup-engine-okio/src/com/fleeksoft/ksoup/io/SourceReaderImpl.kt b/ksoup-engine-okio/src/com/fleeksoft/ksoup/io/SourceReaderImpl.kt index c4f52cae..4fe9cc47 100644 --- a/ksoup-engine-okio/src/com/fleeksoft/ksoup/io/SourceReaderImpl.kt +++ b/ksoup-engine-okio/src/com/fleeksoft/ksoup/io/SourceReaderImpl.kt @@ -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 } diff --git a/ksoup-test/test/com/fleeksoft/ksoup/ported/io/KByteBufferTest.kt b/ksoup-test/test/com/fleeksoft/ksoup/ported/io/KByteBufferTest.kt index af1f8ae0..ca39b429 100644 --- a/ksoup-test/test/com/fleeksoft/ksoup/ported/io/KByteBufferTest.kt +++ b/ksoup-test/test/com/fleeksoft/ksoup/ported/io/KByteBufferTest.kt @@ -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) @@ -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) @@ -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()) diff --git a/ksoup-test/test/com/fleeksoft/ksoup/ported/io/ReaderTest.kt b/ksoup-test/test/com/fleeksoft/ksoup/ported/io/ReaderTest.kt index 8e8c4ddb..a6eaa05c 100644 --- a/ksoup-test/test/com/fleeksoft/ksoup/ported/io/ReaderTest.kt +++ b/ksoup-test/test/com/fleeksoft/ksoup/ported/io/ReaderTest.kt @@ -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]) diff --git a/ksoup-test/test@jvmAndAndroid/com/fleeksoft/ksoup/io/InputStreamReader.test.kt b/ksoup-test/test@jvmAndAndroid/com/fleeksoft/ksoup/io/InputStreamReader.test.kt index 7d82a031..abf4e5ea 100644 --- a/ksoup-test/test@jvmAndAndroid/com/fleeksoft/ksoup/io/InputStreamReader.test.kt +++ b/ksoup-test/test@jvmAndAndroid/com/fleeksoft/ksoup/io/InputStreamReader.test.kt @@ -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]) diff --git a/ksoup/src/com/fleeksoft/ksoup/nodes/Document.kt b/ksoup/src/com/fleeksoft/ksoup/nodes/Document.kt index fe485c77..efebabd2 100644 --- a/ksoup/src/com/fleeksoft/ksoup/nodes/Document.kt +++ b/ksoup/src/com/fleeksoft/ksoup/nodes/Document.kt @@ -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 @@ -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 } diff --git a/ksoup/src/com/fleeksoft/ksoup/parser/TokenQueue.kt b/ksoup/src/com/fleeksoft/ksoup/parser/TokenQueue.kt index 6da61e7b..e90bbdd3 100644 --- a/ksoup/src/com/fleeksoft/ksoup/parser/TokenQueue.kt +++ b/ksoup/src/com/fleeksoft/ksoup/parser/TokenQueue.kt @@ -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 @@ -377,7 +356,7 @@ public class TokenQueue(data: String) { } lastChar = c1 } - return output.toString() + return StringUtil.releaseBuilder(output) } /* diff --git a/ksoup/src/com/fleeksoft/ksoup/ported/KsoupExt.kt b/ksoup/src/com/fleeksoft/ksoup/ported/KsoupExt.kt index 7cf3a644..1bcba614 100644 --- a/ksoup/src/com/fleeksoft/ksoup/ported/KsoupExt.kt +++ b/ksoup/src/com/fleeksoft/ksoup/ported/KsoupExt.kt @@ -22,7 +22,7 @@ fun String.toByteArray(charset: Charset? = null): ByteArray = charset?.toByteArr fun String.toSourceFile(): FileSource = KsoupEngineInstance.ksoupEngine.pathToFileSource(this) -public fun > Array.binarySearch(element: T): Int { +inline fun > Array.binarySearch(element: T): Int { var low = 0 var high = this.size - 1 @@ -41,7 +41,7 @@ public fun > Array.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 @@ -57,7 +57,7 @@ fun IntArray.binarySearch(key: Int): Int { } -public fun Array.binarySearchBy(comparison: (T) -> Int): Int { +inline fun Array.binarySearchBy(comparison: (T) -> Int): Int { var low = 0 var high = size - 1 diff --git a/ksoup/src/com/fleeksoft/ksoup/ported/io/BufferedReader.kt b/ksoup/src/com/fleeksoft/ksoup/ported/io/BufferedReader.kt index 898663cf..15668cb7 100644 --- a/ksoup/src/com/fleeksoft/ksoup/ported/io/BufferedReader.kt +++ b/ksoup/src/com/fleeksoft/ksoup/ported/io/BufferedReader.kt @@ -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