Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Sync from next
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Feb 17, 2021
1 parent 54160f5 commit dce48e7
Show file tree
Hide file tree
Showing 20 changed files with 550 additions and 422 deletions.
5 changes: 3 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ coroutinesVersion=1.4.2

# korlibs
klockVersion=2.0.6
kdsVersion=2.0.6
kmemVersion=2.0.6
kdsVersion=2.0.7
kmemVersion=2.0.7
kryptoVersion=2.0.6
kloggerVersion=2.0.7

# bintray location
project.bintray.org=korlibs
Expand Down
2 changes: 2 additions & 0 deletions korio/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ val klockVersion: String by project
val kdsVersion: String by project
val kmemVersion: String by project
val kryptoVersion: String by project
val kloggerVersion: String by project
val coroutinesVersion: String by project

dependencies {
add("commonMainApi", "com.soywiz.korlibs.klock:klock:$klockVersion")
add("commonMainApi", "com.soywiz.korlibs.kds:kds:$kdsVersion")
add("commonMainApi", "com.soywiz.korlibs.kmem:kmem:$kmemVersion")
add("commonMainApi", "com.soywiz.korlibs.krypto:krypto:$kryptoVersion")
add("commonMainApi", "com.soywiz.korlibs.klogger:klogger:$kloggerVersion")
add("commonMainApi", "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")

afterEvaluate {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,34 @@ import kotlin.reflect.*
class ObservableProperty<T>(initial: T) {
private var observers = ArrayList<(T) -> Unit>()

var value: T = initial; private set
private var _value: T = initial

var value: T
get() = _value
set(v) {
update(v)
}
val observerCount: Int get() = observers.size
fun clear() = observers.clear()

fun observe(handler: (T) -> Unit) {
fun observe(handler: (T) -> Unit): ObservableProperty<T> {
observers.add(handler)
return this
}
fun observeStart(handler: (T) -> Unit): ObservableProperty<T> {
observe(handler)
handler(value)
return this
}
operator fun invoke(handler: (T) -> Unit) = observe(handler)

fun bind(prop: KMutableProperty0<T>) {
observe { prop.set(value) }
prop.set(value)
}

fun update(value: T) {
this.value = value
this._value = value
observers.fastForEach { it(value) }
}
operator fun invoke(value: T) = update(value)
Expand All @@ -36,3 +53,7 @@ class ObservableProperty<T>(initial: T) {
}
}
}

fun <T> ObservableProperty(prop: KMutableProperty0<T>): ObservableProperty<T> {
return ObservableProperty(prop.get()).observeStart { prop.set(it) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ expect fun korAtomic(initial: Boolean): KorAtomicBoolean
expect fun korAtomic(initial: Int): KorAtomicInt
expect fun korAtomic(initial: Long): KorAtomicLong

fun <T> KorAtomicRef(initial: T): KorAtomicRef<T> = korAtomic(initial)
fun KorAtomicBoolean(initial: Boolean): KorAtomicBoolean = korAtomic(initial)
fun KorAtomicInt(initial: Int): KorAtomicInt = korAtomic(initial)
fun KorAtomicLong(initial: Long): KorAtomicLong = korAtomic(initial)

//fun <T> korAtomic(initial: T): KorAtomicRef<T> = KorAtomicRef(initial)
//fun korAtomic(initial: Boolean): KorAtomicBoolean = KorAtomicBoolean(initial)
//fun korAtomic(initial: Int): KorAtomicInt = KorAtomicInt(initial)
Expand All @@ -21,7 +26,7 @@ interface KorAtomicNumber<T : Number> : KorAtomicBase<T> {
fun addAndGet(delta: T): T
}

open class KorAtomicRef<T>(initial: T) : KorAtomicBase<T> {
open class KorAtomicRef<T> internal constructor(initial: T, dummy: Boolean) : KorAtomicBase<T> {
override var value: T = initial

override fun compareAndSet(expect: T, update: T): Boolean {
Expand All @@ -34,7 +39,7 @@ open class KorAtomicRef<T>(initial: T) : KorAtomicBase<T> {
}
}

open class KorAtomicBoolean(initial: Boolean) : KorAtomicBase<Boolean> {
open class KorAtomicBoolean internal constructor(initial: Boolean, dummy: Boolean) : KorAtomicBase<Boolean> {
override var value: Boolean = initial

override fun compareAndSet(expect: Boolean, update: Boolean): Boolean {
Expand All @@ -47,7 +52,7 @@ open class KorAtomicBoolean(initial: Boolean) : KorAtomicBase<Boolean> {
}
}

open class KorAtomicInt(initial: Int) : KorAtomicNumber<Int> {
open class KorAtomicInt internal constructor(initial: Int, dummy: Boolean) : KorAtomicNumber<Int> {
override var value: Int = initial

override fun compareAndSet(expect: Int, update: Int): Boolean {
Expand All @@ -65,7 +70,7 @@ open class KorAtomicInt(initial: Int) : KorAtomicNumber<Int> {
}
}

open class KorAtomicLong(initial: Long) : KorAtomicNumber<Long> {
open class KorAtomicLong internal constructor(initial: Long, dummy: Boolean) : KorAtomicNumber<Long> {
override var value: Long = initial

override fun compareAndSet(expect: Long, update: Long): Boolean {
Expand Down
2 changes: 1 addition & 1 deletion korio/src/commonMain/kotlin/com/soywiz/korio/file/Vfs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ abstract class Vfs : AsyncCloseable {
handler: VfsProcessHandler = VfsProcessHandler()
): Int = unsupported()

open suspend fun open(path: String, mode: VfsOpenMode): AsyncStream = throw UnsupportedOperationException()
open suspend fun open(path: String, mode: VfsOpenMode): AsyncStream = unsupported()

open suspend fun openInputStream(path: String): AsyncInputStream = open(
path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ data class VfsFile(
}

fun jail(): VfsFile = JailVfs(this)
fun jailParent(): VfsFile = JailVfs(parent)[this.baseName]

suspend fun getUnderlyingUnscapedFile(): FinalVfsFile = vfs.getUnderlyingUnscapedFile(this.path)

Expand Down
35 changes: 25 additions & 10 deletions korio/src/commonMain/kotlin/com/soywiz/korio/lang/Charset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import com.soywiz.korio.internal.*
import kotlin.native.concurrent.SharedImmutable

abstract class Charset(val name: String) {
// Just an estimation, might not be accurate, but hopefully will help setting StringBuilder and ByteArrayBuilder to a better initial capacity
open fun estimateNumberOfCharactersForBytes(nbytes: Int): Int = nbytes * 2
open fun estimateNumberOfBytesForCharacters(nchars: Int): Int = nchars * 2

abstract fun encode(out: ByteArrayBuilder, src: CharSequence, start: Int = 0, end: Int = src.length)
abstract fun decode(out: StringBuilder, src: ByteArray, start: Int = 0, end: Int = src.size)

Expand All @@ -14,7 +18,7 @@ abstract class Charset(val name: String) {
return UTF8
}

fun StringBuilder.appendCodePoint(codePoint: Int) {
fun StringBuilder.appendCodePointV(codePoint: Int) {
if (codePoint in 0xD800..0xDFFF || codePoint > 0xFFFF) {
val U0 = codePoint - 0x10000
val hs = U0.extract(10, 10)
Expand Down Expand Up @@ -51,7 +55,10 @@ abstract class Charset(val name: String) {
}

open class UTC8CharsetBase(name: String) : Charset(name) {
private fun createByte(codePoint: Int, shift: Int): Int = codePoint shr shift and 0x3F or 0x80
override fun estimateNumberOfCharactersForBytes(nbytes: Int): Int = nbytes * 2
override fun estimateNumberOfBytesForCharacters(nchars: Int): Int = nchars * 2

private fun createByte(codePoint: Int, shift: Int): Int = codePoint shr shift and 0x3F or 0x80

override fun encode(out: ByteArrayBuilder, src: CharSequence, start: Int, end: Int) {
decodeCodePoints(src, start, end) { codePoint ->
Expand Down Expand Up @@ -85,22 +92,22 @@ open class UTC8CharsetBase(name: String) : Charset(name) {
when (c shr 4) {
in 0b0000..0b0111 -> {
// 0xxxxxxx
out.appendCodePoint(c)
out.appendCodePointV(c)
i += 1
}
in 0b1100..0b1101 -> {
// 110x xxxx 10xx xxxx
out.appendCodePoint((c and 0x1F shl 6 or (src[i + 1].toInt() and 0x3F)))
out.appendCodePointV((c and 0x1F shl 6 or (src[i + 1].toInt() and 0x3F)))
i += 2
}
0b1110 -> {
// 1110 xxxx 10xx xxxx 10xx xxxx
out.appendCodePoint((c and 0x0F shl 12 or (src[i + 1].toInt() and 0x3F shl 6) or (src[i + 2].toInt() and 0x3F)))
out.appendCodePointV((c and 0x0F shl 12 or (src[i + 1].toInt() and 0x3F shl 6) or (src[i + 2].toInt() and 0x3F)))
i += 3
}
0b1111 -> {
// 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx
out.appendCodePoint(0
out.appendCodePointV(0
.insert(src[i + 0].toInt().extract(0, 3), 18, 3)
.insert(src[i + 1].toInt().extract(0, 6), 12, 6)
.insert(src[i + 2].toInt().extract(0, 6), 6, 6)
Expand All @@ -119,7 +126,12 @@ open class UTC8CharsetBase(name: String) : Charset(name) {
}
}

open class SingleByteCharset(name: String, val conv: String) : Charset(name) {
abstract class BaseSingleByteCharset(name: String) : Charset(name) {
override fun estimateNumberOfCharactersForBytes(nbytes: Int): Int = nbytes
override fun estimateNumberOfBytesForCharacters(nchars: Int): Int = nchars
}

open class SingleByteCharset(name: String, val conv: String) : BaseSingleByteCharset(name) {
val v: IntIntMap = IntIntMap().apply {
for (n in 0 until conv.length) this[conv[n].toInt()] = n
}
Expand All @@ -144,6 +156,9 @@ object ISO_8859_1 : SingleByteCharset("ISO-8859-1", buildString { for (n in 0 un
expect val UTF8: Charset

class UTF16Charset(val le: Boolean) : Charset("UTF-16-" + (if (le) "LE" else "BE")) {
override fun estimateNumberOfCharactersForBytes(nbytes: Int): Int = nbytes * 2
override fun estimateNumberOfBytesForCharacters(nchars: Int): Int = nchars * 2

override fun decode(out: StringBuilder, src: ByteArray, start: Int, end: Int) {
for (n in start until end step 2) out.append(src.readS16(n, le).toChar())
}
Expand All @@ -157,7 +172,7 @@ class UTF16Charset(val le: Boolean) : Charset("UTF-16-" + (if (le) "LE" else "BE
}
}

object ASCII : Charset("ASCII") {
object ASCII : BaseSingleByteCharset("ASCII") {
override fun encode(out: ByteArrayBuilder, src: CharSequence, start: Int, end: Int) {
for (n in start until end) out.append(src[n].toByte())
}
Expand All @@ -182,13 +197,13 @@ object Charsets {
}

fun String.toByteArray(charset: Charset = UTF8): ByteArray {
val out = ByteArrayBuilder()
val out = ByteArrayBuilder(charset.estimateNumberOfBytesForCharacters(this.length))
charset.encode(out, this)
return out.toByteArray()
}

fun ByteArray.toString(charset: Charset): String {
val out = StringBuilder()
val out = StringBuilder(charset.estimateNumberOfCharactersForBytes(this.size))
charset.decode(out, this)
return out.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ operator fun String.Companion.invoke(arrays: IntArray, offset: Int = 0, size: In
}

fun String_fromIntArray(arrays: IntArray, offset: Int = 0, size: Int = arrays.size - offset): String = String(arrays, offset, size)
fun String_fromCharArray(arrays: CharArray, offset: Int = 0, size: Int = arrays.size - offset): String = String(arrays, offset, size)
fun String_fromCharArray(arrays: CharArray, offset: Int = 0, size: Int = arrays.size - offset): String = arrays.concatToString(offset, offset + size)

////////////////////////////////////
////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object Json {
'}' -> break@obj; ',' -> continue@obj; else -> s.unread()
}
val key = parse(s, context) as String
s.skipSpaces().expect(':')
s.skipSpaces().skipExpect(':')
val value = parse(s, context)
this[key] = value
}
Expand Down
19 changes: 19 additions & 0 deletions korio/src/commonMain/kotlin/com/soywiz/korio/time/TraceTime.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.soywiz.korio.time

import com.soywiz.klock.*
import com.soywiz.klogger.*
import com.soywiz.korio.lang.*

@PublishedApi
internal val traceTimes by lazy { Environment["TRACE_TIMES"] == "true" }

inline fun <T : Any> traceTime(name: String, block: () -> T): T {
lateinit var result: T
val time = measureTime {
result = block()
}
if (traceTimes) {
Console.info("$name loaded in $time")
}
return result
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.soywiz.korio.util

inline fun <T> buildList(callback: ArrayList<T>.() -> Unit): List<T> = arrayListOf<T>().apply(callback)
import com.soywiz.kds.*

inline fun <T> buildList(callback: FastArrayList<T>.() -> Unit): List<T> = FastArrayList<T>().apply(callback)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class RedirectMutableField<V>(val redirect: KMutableProperty0<V>) {
inline operator fun setValue(thisRef: Any?, property: KProperty<*>, value: V) = redirect.set(value)
}

// @TODO: Shouldn't be required anymore
fun <V> redirect(prop: KMutableProperty0<V>) = RedirectMutableField(prop)
fun <V> redirect(prop: KProperty0<V>) = RedirectField(prop)

Expand Down
Loading

0 comments on commit dce48e7

Please sign in to comment.