mirror of
https://github.com/tursom/TursomServer.git
synced 2025-04-02 15:41:23 +08:00
update
This commit is contained in:
parent
a2ff3f38d4
commit
1080a32e73
80
build.gradle
80
build.gradle
@ -1,80 +0,0 @@
|
|||||||
buildscript {
|
|
||||||
ext.kotlinVersion = '1.4.31'
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
mavenLocal()
|
|
||||||
jcenter()
|
|
||||||
}
|
|
||||||
dependencies {
|
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
allprojects {
|
|
||||||
apply plugin: "maven-publish"
|
|
||||||
apply plugin: 'java'
|
|
||||||
apply plugin: 'kotlin'
|
|
||||||
apply plugin: 'maven'
|
|
||||||
|
|
||||||
group 'cn.tursom'
|
|
||||||
version '0.1'
|
|
||||||
|
|
||||||
sourceCompatibility = 1.8
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
mavenLocal()
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
|
|
||||||
api "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
|
|
||||||
api "com.google.code.gson:gson:2.8.2"
|
|
||||||
testImplementation group: 'junit', name: 'junit', version: '4.12'
|
|
||||||
}
|
|
||||||
|
|
||||||
compileKotlin {
|
|
||||||
kotlinOptions.jvmTarget = "1.8"
|
|
||||||
kotlinOptions.useIR = true
|
|
||||||
}
|
|
||||||
compileTestKotlin {
|
|
||||||
kotlinOptions.jvmTarget = "1.8"
|
|
||||||
kotlinOptions.useIR = true
|
|
||||||
}
|
|
||||||
|
|
||||||
//打包源代码
|
|
||||||
task sourcesJar(type: Jar, dependsOn: classes) {
|
|
||||||
//classifier = 'sources'
|
|
||||||
archiveClassifier.set('sources')
|
|
||||||
from sourceSets.main.allSource
|
|
||||||
}
|
|
||||||
|
|
||||||
artifacts {
|
|
||||||
archives sourcesJar
|
|
||||||
}
|
|
||||||
publishing {
|
|
||||||
repositories {
|
|
||||||
maven {
|
|
||||||
name = "GitHubPackages"
|
|
||||||
url = uri("https://maven.pkg.github.com/tursom/TursomServer")
|
|
||||||
credentials {
|
|
||||||
//username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
|
|
||||||
username = "tursom"
|
|
||||||
password = project.findProperty("gpr.key") ?: System.getenv("PASSWORD")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
publications {
|
|
||||||
gpr(MavenPublication) {
|
|
||||||
from(components.java)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sourceSets {
|
|
||||||
all {
|
|
||||||
languageSettings {
|
|
||||||
useExperimentalAnnotation('-Xopt-in=kotlin.RequiresOptIn')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
58
build.gradle.kts
Normal file
58
build.gradle.kts
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
|
|
||||||
|
ext["netty.version"] = "4.1.59.Final"
|
||||||
|
ext["excludeTest"] = { project: Project, tasks: TaskContainer ->
|
||||||
|
if (project.gradle.startParameter.taskNames.firstOrNull { taskName ->
|
||||||
|
taskName.endsWith(":test")
|
||||||
|
} == null) {
|
||||||
|
tasks {
|
||||||
|
test { enabled = false }
|
||||||
|
testClasses { enabled = false }
|
||||||
|
compileTestJava { enabled = false }
|
||||||
|
compileTestKotlin { enabled = false }
|
||||||
|
processTestResources { enabled = false }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
kotlin("jvm") version "1.4.31"
|
||||||
|
}
|
||||||
|
|
||||||
|
allprojects {
|
||||||
|
group = "cn.tursom"
|
||||||
|
version = "0.2"
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.withType<JavaCompile> {
|
||||||
|
tasks.withType<KotlinCompile>().configureEach {
|
||||||
|
kotlinOptions.jvmTarget = "1.8"
|
||||||
|
kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
|
||||||
|
}
|
||||||
|
|
||||||
|
if (project.gradle.startParameter.taskNames.firstOrNull { taskName ->
|
||||||
|
taskName.endsWith(":test")
|
||||||
|
} == null) {
|
||||||
|
tasks.withType<Test> {
|
||||||
|
enabled = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@kotlin.Suppress("UNCHECKED_CAST")
|
||||||
|
(rootProject.ext["excludeTest"] as (Project, TaskContainer) -> Unit)(project, tasks)
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
api(kotlin("stdlib-jdk8"))
|
||||||
|
api(kotlin("reflect"))
|
||||||
|
testImplementation(group = "junit", name = "junit", version = "4.12")
|
||||||
|
|
||||||
|
val commonVersion = "1.0.RELEASE"
|
||||||
|
api("com.ddbes", "common-kotlin", commonVersion)
|
||||||
|
}
|
@ -1,22 +0,0 @@
|
|||||||
rootProject.name = 'TursomServer'
|
|
||||||
include 'web', 'aop', 'database', 'utils', 'utils:xml', 'utils:async-http', 'web:netty-web'
|
|
||||||
include 'socket', 'socket:socket-async'
|
|
||||||
include 'AsyncSocket'
|
|
||||||
include 'log'
|
|
||||||
include 'json'
|
|
||||||
include 'utils:yaml'
|
|
||||||
include 'web:web-coroutine'
|
|
||||||
include 'microservices'
|
|
||||||
include 'database:database-mysql'
|
|
||||||
include 'database:mongodb'
|
|
||||||
include 'database:mongodb:mongodb-async'
|
|
||||||
include 'database:redis'
|
|
||||||
include 'utils:ws-client'
|
|
||||||
include 'utils:mail'
|
|
||||||
include 'utils:csv'
|
|
||||||
include 'utils:delegation'
|
|
||||||
include 'utils:observer'
|
|
||||||
include 'utils:TrafficForward'
|
|
||||||
include 'utils:performance-test'
|
|
||||||
include 'utils:math'
|
|
||||||
include 'utils:json'
|
|
25
settings.gradle.kts
Normal file
25
settings.gradle.kts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
rootProject.name = "TursomServer"
|
||||||
|
include("ts-core", "ts-core:ts-buffer", "ts-core:ts-encrypt", "ts-core:ts-datastruct")
|
||||||
|
include("ts-core:ts-pool")
|
||||||
|
include("ts-core:ts-hash")
|
||||||
|
//include("web", "aop", "database", "utils", "utils:xml", "utils:async-http", "web:netty-web")
|
||||||
|
//include("socket", "socket:socket-async")
|
||||||
|
//include("AsyncSocket")
|
||||||
|
//include("log")
|
||||||
|
//include("json")
|
||||||
|
//include("utils:yaml")
|
||||||
|
//include("web:web-coroutine")
|
||||||
|
//include("microservices")
|
||||||
|
//include("database:database-mysql")
|
||||||
|
//include("database:mongodb")
|
||||||
|
//include("database:mongodb:mongodb-async")
|
||||||
|
//include("database:redis")
|
||||||
|
//include("utils:ws-client")
|
||||||
|
//include("utils:mail")
|
||||||
|
//include("utils:csv")
|
||||||
|
//include("utils:delegation")
|
||||||
|
//include("utils:observer")
|
||||||
|
//include("utils:TrafficForward")
|
||||||
|
//include("utils:performance-test")
|
||||||
|
//include("utils:math")
|
||||||
|
//include("utils:json")
|
@ -1,26 +0,0 @@
|
|||||||
package cn.tursom.core
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer
|
|
||||||
|
|
||||||
/**
|
|
||||||
* HOOK java.nio.HeapByteBuffer
|
|
||||||
*/
|
|
||||||
object HeapByteBufferUtil {
|
|
||||||
private val field = ByteBuffer::class.java.getDeclaredField("offset")
|
|
||||||
|
|
||||||
init {
|
|
||||||
field.isAccessible = true
|
|
||||||
}
|
|
||||||
|
|
||||||
fun wrap(array: ByteArray, offset: Int = 0, size: Int = array.size - offset): ByteBuffer {
|
|
||||||
val buffer = ByteBuffer.wrap(array, 0, offset + size)
|
|
||||||
//return if (offset == 0) buffer else {
|
|
||||||
// buffer.position(offset)
|
|
||||||
// buffer.slice()
|
|
||||||
//}
|
|
||||||
if (offset > 0) field.set(buffer, offset)
|
|
||||||
return buffer
|
|
||||||
}
|
|
||||||
|
|
||||||
fun wrap(string: String) = wrap(string.toByteArray())
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
package cn.tursom.core
|
|
||||||
|
|
||||||
import java.io.File
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
class RandomCode {
|
|
||||||
private val randomCode = "${randomInt(10000000, 99999999)}"
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
return randomCode
|
|
||||||
}
|
|
||||||
|
|
||||||
fun showCode(codeName: String = "passcode", filepath: String? = null) {
|
|
||||||
println("$codeName: $randomCode")
|
|
||||||
filepath ?: return
|
|
||||||
val file = File(filepath)
|
|
||||||
file.createNewFile()
|
|
||||||
file.writeText("$codeName = $randomCode")
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private fun randomInt(min: Int, max: Int) = Random().nextInt(max) % (max - min + 1) + min
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
package cn.tursom.core.datastruct
|
|
||||||
|
|
||||||
class ContainMap<K>(override val keys: Set<K>) : Map<K, Boolean> {
|
|
||||||
override val entries: Set<Map.Entry<K, Boolean>> = EntrySet(keys)
|
|
||||||
override val size: Int get() = keys.size
|
|
||||||
override val values: Collection<Boolean> = listOf(true)
|
|
||||||
override fun containsKey(key: K): Boolean = keys.contains(key)
|
|
||||||
override fun containsValue(value: Boolean): Boolean = true
|
|
||||||
override fun get(key: K): Boolean = keys.contains(key)
|
|
||||||
override fun isEmpty(): Boolean = keys.isEmpty()
|
|
||||||
|
|
||||||
private class EntrySet<K>(val keys: Set<K>) : Set<Map.Entry<K, Boolean>> {
|
|
||||||
override val size: Int get() = keys.size
|
|
||||||
override fun isEmpty(): Boolean = keys.isEmpty()
|
|
||||||
override fun iterator(): Iterator<Map.Entry<K, Boolean>> = EntrySerIterator(keys)
|
|
||||||
override fun contains(element: Map.Entry<K, Boolean>): Boolean = keys.contains(element.key) == element.value
|
|
||||||
override fun containsAll(elements: Collection<Map.Entry<K, Boolean>>): Boolean {
|
|
||||||
elements.forEach { if (contains(it).not()) return false }
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class EntrySerIterator<K>(keys: Set<K>) : Iterator<Map.Entry<K, Boolean>> {
|
|
||||||
private val iterator = keys.iterator()
|
|
||||||
override fun hasNext(): Boolean = iterator.hasNext()
|
|
||||||
override fun next(): Map.Entry<K, Boolean> = Entry(iterator.next(), true)
|
|
||||||
}
|
|
||||||
|
|
||||||
private class Entry<K>(override val key: K, override val value: Boolean) : Map.Entry<K, Boolean>
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package cn.tursom.core.datastruct
|
|
||||||
|
|
||||||
import kotlin.reflect.KProperty1
|
|
||||||
|
|
||||||
class KPropertyEntriesIterator(val target: Any, propertyMap: Map<String, KProperty1<Any, *>>) : Iterator<Map.Entry<String, Any?>> {
|
|
||||||
private val iterator = propertyMap.iterator()
|
|
||||||
override fun hasNext(): Boolean = iterator.hasNext()
|
|
||||||
override fun next(): Map.Entry<String, Any?> {
|
|
||||||
val entry = iterator.next()
|
|
||||||
return object : Map.Entry<String, Any?> {
|
|
||||||
override val key: String = entry.key
|
|
||||||
override val value: Any? get() = entry.value.get(target)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
package cn.tursom.core.datastruct
|
|
||||||
|
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
|
||||||
import kotlin.concurrent.read
|
|
||||||
import kotlin.concurrent.write
|
|
||||||
|
|
||||||
class ReadWriteMap<K, V>(val map: MutableMap<K, V>) : MutableMap<K, V> {
|
|
||||||
private val lock = ReentrantReadWriteLock()
|
|
||||||
|
|
||||||
override val size: Int get() = lock.read { map.size }
|
|
||||||
override val entries get() = lock.read { map.entries }
|
|
||||||
override val keys get() = lock.read { map.keys }
|
|
||||||
override val values get() = lock.read { map.values }
|
|
||||||
override fun containsKey(key: K): Boolean = lock.read { map.containsKey(key) }
|
|
||||||
override fun containsValue(value: V): Boolean = lock.read { map.containsValue(value) }
|
|
||||||
override fun get(key: K): V? = lock.read { map.get(key) }
|
|
||||||
override fun isEmpty(): Boolean = lock.read { map.isEmpty() }
|
|
||||||
override fun clear() = lock.write { map.clear() }
|
|
||||||
override fun put(key: K, value: V): V? = lock.write { map.put(key, value) }
|
|
||||||
override fun putAll(from: Map<out K, V>) = lock.write { map.putAll(from) }
|
|
||||||
override fun remove(key: K): V? = lock.write { map.remove(key) }
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
package cn.tursom.core.datastruct
|
|
||||||
|
|
||||||
class ReversedList<E>(
|
|
||||||
val list: List<E>
|
|
||||||
) : List<E> by list {
|
|
||||||
override fun get(index: Int): E = list[size - index - 1]
|
|
||||||
|
|
||||||
override fun indexOf(element: E): Int {
|
|
||||||
val lastIndexOf = list.lastIndexOf(element)
|
|
||||||
return if (lastIndexOf >= 0) size - lastIndexOf else -1
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun lastIndexOf(element: E): Int {
|
|
||||||
val indexOf = list.indexOf(element)
|
|
||||||
return if (indexOf >= 0) size - indexOf else -1
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun iterator(): Iterator<E> = listIterator()
|
|
||||||
override fun listIterator(): ListIterator<E> = listIterator(0)
|
|
||||||
|
|
||||||
override fun listIterator(index: Int): ListIterator<E> = ReverseListIterator(list.listIterator(size - index))
|
|
||||||
|
|
||||||
override fun subList(fromIndex: Int, toIndex: Int): List<E> {
|
|
||||||
return ReversedList(list.subList(size - toIndex, size - fromIndex))
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
val iterator = iterator()
|
|
||||||
return buildString {
|
|
||||||
append('[')
|
|
||||||
iterator.forEach {
|
|
||||||
append(it)
|
|
||||||
if (iterator.hasNext()) append(", ")
|
|
||||||
}
|
|
||||||
append(']')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class ReverseListIterator<E>(val listIterator: ListIterator<E>) : ListIterator<E> {
|
|
||||||
override fun hasNext(): Boolean = listIterator.hasPrevious()
|
|
||||||
override fun next(): E = listIterator.previous()
|
|
||||||
override fun nextIndex(): Int = listIterator.previousIndex()
|
|
||||||
override fun hasPrevious(): Boolean = listIterator.hasNext()
|
|
||||||
override fun previous(): E = listIterator.next()
|
|
||||||
override fun previousIndex(): Int = listIterator.nextIndex()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,66 +0,0 @@
|
|||||||
package cn.tursom.core.datastruct
|
|
||||||
|
|
||||||
class SetMap<K>(private val set: Set<K>) : Map<K, Unit> {
|
|
||||||
|
|
||||||
override val keys: Set<K>
|
|
||||||
get() = set
|
|
||||||
override val size: Int
|
|
||||||
get() = set.size
|
|
||||||
override val values: Collection<Unit> = listOf()
|
|
||||||
|
|
||||||
override fun containsKey(key: K): Boolean {
|
|
||||||
return set.contains(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun containsValue(value: Unit): Boolean {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun get(key: K): Unit? {
|
|
||||||
return if (set.contains(key)) Unit else null
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isEmpty(): Boolean {
|
|
||||||
return size == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
override val entries: Set<Map.Entry<K, Unit>> = object : Set<Map.Entry<K, Unit>> {
|
|
||||||
override val size: Int
|
|
||||||
get() = set.size
|
|
||||||
|
|
||||||
override fun contains(element: Map.Entry<K, Unit>): Boolean {
|
|
||||||
return set.contains(element.key)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun containsAll(elements: Collection<Map.Entry<K, Unit>>): Boolean {
|
|
||||||
elements.forEach {
|
|
||||||
if (!set.contains(it.key)) return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isEmpty(): Boolean {
|
|
||||||
return size == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun iterator(): Iterator<Map.Entry<K, Unit>> {
|
|
||||||
return SetMapIterator(set)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SetMapIterator<K>(set: Set<K>) : Iterator<Map.Entry<K, Unit>> {
|
|
||||||
private val iterator = set.iterator()
|
|
||||||
override fun hasNext(): Boolean {
|
|
||||||
return iterator.hasNext()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun next(): Map.Entry<K, Unit> {
|
|
||||||
return Entry(iterator.next())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data class Entry<K>(override val key: K) : Map.Entry<K, Unit> {
|
|
||||||
override val value: Unit
|
|
||||||
get() = Unit
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package cn.tursom.core.datastruct
|
|
||||||
|
|
||||||
interface SimpMap<K, V> : MutableMap<K, V> {
|
|
||||||
/**
|
|
||||||
* @return prev value
|
|
||||||
*/
|
|
||||||
operator fun set(key: K, value: V)
|
|
||||||
|
|
||||||
infix fun delete(key: K): V?
|
|
||||||
|
|
||||||
fun setAndGet(key: K, value: V): V? {
|
|
||||||
val prev = get(key)
|
|
||||||
set(key, value)
|
|
||||||
return prev
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun put(key: K, value: V): V? = setAndGet(key, value)
|
|
||||||
|
|
||||||
override fun remove(key: K): V? = delete(key)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清空整个表
|
|
||||||
*/
|
|
||||||
override fun clear()
|
|
||||||
|
|
||||||
fun first(): V?
|
|
||||||
|
|
||||||
override infix fun putAll(from: Map<out K, V>) {
|
|
||||||
from.forEach { (k, u) ->
|
|
||||||
set(k, u)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
|||||||
package cn.tursom.core.datastruct
|
|
||||||
|
|
||||||
import java.lang.ref.SoftReference
|
|
||||||
|
|
||||||
class SoftArrayMap<K, V>(val map: MutableMap<K, SoftReference<V>>) : MutableMap<K, V> {
|
|
||||||
constructor(initialCapacity: Int = 16) : this(HashMap(initialCapacity))
|
|
||||||
|
|
||||||
override val size: Int get() = map.size
|
|
||||||
override val entries: MutableSet<MutableMap.MutableEntry<K, V>> get() = throw NotImplementedError()
|
|
||||||
override val keys: MutableSet<K> get() = map.keys
|
|
||||||
override val values: MutableCollection<V> get() = throw NotImplementedError()
|
|
||||||
|
|
||||||
override fun put(key: K, value: V): V? = map.put(key, SoftReference(value))?.get()
|
|
||||||
override fun remove(key: K): V? = map.remove(key)?.get()
|
|
||||||
override fun clear() = map.clear()
|
|
||||||
override fun containsKey(key: K): Boolean = map.containsKey(key)
|
|
||||||
override fun containsValue(value: V): Boolean = map.containsValue(SoftReference(value))
|
|
||||||
override fun get(key: K): V? = map[key]?.get()
|
|
||||||
override fun isEmpty(): Boolean = map.isEmpty()
|
|
||||||
override fun putAll(from: Map<out K, V>) = from.forEach { (k, u) -> map.put(k, SoftReference(u)) }
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package cn.tursom.core.datastruct.async.interfaces
|
|
||||||
|
|
||||||
interface AsyncCollection<out E> {
|
|
||||||
val size: Int
|
|
||||||
|
|
||||||
suspend fun isEmpty(): Boolean
|
|
||||||
suspend infix fun contains(element: @UnsafeVariance E): Boolean
|
|
||||||
suspend infix fun containsAll(elements: AsyncCollection<@UnsafeVariance E>): Boolean
|
|
||||||
suspend fun forEach(action: suspend (E) -> Boolean): Boolean
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
package cn.tursom.core.datastruct.async.interfaces
|
|
||||||
|
|
||||||
interface AsyncMap<K, V> : AsyncCollection<Map.Entry<K, V>> {
|
|
||||||
override val size: Int
|
|
||||||
val entries: AsyncSet<Map.Entry<K, V>>
|
|
||||||
val keys: AsyncSet<K>
|
|
||||||
val values: AsyncCollection<V>
|
|
||||||
|
|
||||||
suspend infix fun containsKey(key: K): Boolean
|
|
||||||
suspend infix fun containsValue(value: V): Boolean
|
|
||||||
suspend infix fun get(key: K): V?
|
|
||||||
|
|
||||||
override suspend fun isEmpty(): Boolean
|
|
||||||
suspend fun isNotEmpty(): Boolean = !isEmpty()
|
|
||||||
|
|
||||||
override suspend fun contains(element: Map.Entry<K, V>): Boolean {
|
|
||||||
return get(element.key) == element.value
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun containsAll(elements: AsyncCollection<Map.Entry<K, V>>): Boolean {
|
|
||||||
return elements.forEach { contains(it) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
|||||||
package cn.tursom.core.datastruct.async.interfaces
|
|
||||||
|
|
||||||
interface AsyncPotableMap<K, V> : AsyncMap<K, V> {
|
|
||||||
suspend fun clear()
|
|
||||||
suspend fun set(key: K, value: V): V?
|
|
||||||
suspend fun putIfAbsent(key: K, value: V): Boolean
|
|
||||||
suspend infix fun putAll(from: Map<out K, V>)
|
|
||||||
suspend infix fun remove(key: K): V?
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
package cn.tursom.core.datastruct.async.interfaces
|
|
||||||
|
|
||||||
interface AsyncPotableSet<K> : AsyncSet<K> {
|
|
||||||
suspend fun clear(): AsyncPotableSet<K>
|
|
||||||
suspend fun put(key: K): AsyncPotableSet<K>
|
|
||||||
suspend fun putIfAbsent(key: K): Boolean
|
|
||||||
suspend infix fun putAll(from: Set<K>): AsyncPotableSet<K>
|
|
||||||
suspend infix fun remove(key: K): AsyncPotableSet<K>
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package cn.tursom.core.regex
|
|
||||||
|
|
||||||
class ControlCharRegexUnit(private val char: Char) : RegexUnit {
|
|
||||||
constructor(char: ControlCharRegexUnit) : this(char.char)
|
|
||||||
|
|
||||||
override fun toString() = "\\c$char"
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
package cn.tursom.core.regex
|
|
||||||
|
|
||||||
class GetMatchingUnit(val subUnit: RegexUnit) : RegexUnit {
|
|
||||||
override val unit: String?
|
|
||||||
get() = toString()
|
|
||||||
|
|
||||||
override fun toString(): String = "($subUnit)"
|
|
||||||
}
|
|
@ -1,278 +0,0 @@
|
|||||||
package cn.tursom.core.regex
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 使用 regex 函数创建一个正则对象
|
|
||||||
*
|
|
||||||
* 字符串前加 + 表示一个字符串单元,任何时候都会作为一个独立单元存在
|
|
||||||
* 字符串前加 - 表示一个字符串,不会作为一个独立单元处理
|
|
||||||
* 注意,我们不支持原始的字符串对象,请使用 + 或 - 将其打包
|
|
||||||
*
|
|
||||||
* 在 RegexMaker 对象头部的这些对象都是字符转义,请根据需要使用
|
|
||||||
* uppercase,lowercase 与 numbers 都是字符列表,用于表示在其范围内的单个字符
|
|
||||||
* 使用 Char.control 获得控制字符转义
|
|
||||||
*
|
|
||||||
* 接下来是连接两个正则单元用的方法,我们可以用 (单元1) link (单元2),(单元1) also (单元2),甚至是 (单元1)(单元2) 的形式连接两个单元
|
|
||||||
* 当然,现在我们也可以用 + 甚至是 - 来连接两个单元了
|
|
||||||
*
|
|
||||||
* 接着是创建单元组的方法,toSet 不建议使用,建议使用 ((单元1) or (单元2) or ...) 的形式创建一个单元组
|
|
||||||
*
|
|
||||||
* 后面跟着的就都是表示重复次数的方法,(单元)-次数n 表示最多重复n次,相应的 * 与 .. 表示精确的 n 次,%表示至少 n 次
|
|
||||||
* 我们还可以使用 (单元)-(min..max)的形式指定重复区间,对于这个接口,-、*、% 与 .. 的效果相同,范围区间还可以使用(min to max)的形式
|
|
||||||
* 有时候,- 会导致运算符优先级的问题,这时我们可以用 / 来代替
|
|
||||||
*
|
|
||||||
* 如果我们想匹配属于某一组的单个字符,可以使用 (开始字符 % 结束字符) 的形式,使用 and、also、link 或者 + 将多个字符组单元相连
|
|
||||||
* 我们还可以在一个 CharRange 或 Pair<Char, Char> 前面加 + 生成字符组
|
|
||||||
* 或者我们也可以手动指定使用哪些字符,使用 list 方法,字符串会被转义成一个字符组单元
|
|
||||||
* 如果你对自己足够有信心,也可以在字符串前面加 !,这会直接生成一个字符组对象,不经检查
|
|
||||||
*
|
|
||||||
* 比如下面这句就是一个合法的表达式
|
|
||||||
* '1' % '2' / 5 + list("ABC-\\") * lowercase * numbers % (2 to 3) + uppercase + any % 3 + caret % 1 + +"还行" * 2
|
|
||||||
* 运行后会生成
|
|
||||||
* [1-2]{0,5}[ABC\-\\a-z0-9]{2,3}[A-Z].{3,}\^+(还行){2}
|
|
||||||
*/
|
|
||||||
@Suppress("unused", "MemberVisibilityCanBePrivate", "DuplicatedCode")
|
|
||||||
object RegexMaker {
|
|
||||||
operator fun String.unaryPlus() = StringRegexUnit(this)
|
|
||||||
operator fun String.unaryMinus() = UnitRegexUnit(this)
|
|
||||||
operator fun RegexUnit.unaryPlus() = GetMatchingUnit(this)
|
|
||||||
operator fun RegexUnit.unaryMinus() = NonGetMatchingUnit(this)
|
|
||||||
|
|
||||||
val Any.str
|
|
||||||
get() = +toString()
|
|
||||||
val Any.unit
|
|
||||||
get() = -toString()
|
|
||||||
|
|
||||||
val slush = -"\\\\"
|
|
||||||
val point = -"\\."
|
|
||||||
val caret = -"\\^"
|
|
||||||
val dollar = -"\\$"
|
|
||||||
val plus = -"\\+"
|
|
||||||
val minus = -"\\-"
|
|
||||||
val star = -"\\*"
|
|
||||||
val roundBrackets = -"\\("
|
|
||||||
val squareBrackets = -"\\["
|
|
||||||
val curlyBrackets = -"\\{"
|
|
||||||
val backslash = -"\\\\"
|
|
||||||
val verticalBar = -"\\|"
|
|
||||||
val questionMark = -"\\?"
|
|
||||||
val nextPage = -"\\f"
|
|
||||||
val nextLine = -"\\n"
|
|
||||||
val enter = -"\\r"
|
|
||||||
val blankCharacter = -"\\s"
|
|
||||||
val nonBlankCharacter = -"\\S"
|
|
||||||
val tab = -"\\t"
|
|
||||||
val verticalTab = -"\\v"
|
|
||||||
val wordBoundary = -"\\b"
|
|
||||||
val nonWordBoundary = -"\\B"
|
|
||||||
val number = -"\\d"
|
|
||||||
val nonNumber = -"\\D"
|
|
||||||
val pageBreak = -"\\f"
|
|
||||||
val lineBreak = -"n"
|
|
||||||
val carriageReturn = -"\\r"
|
|
||||||
val lettersNumbersUnderscores = -"\\w"
|
|
||||||
val nonLettersNumbersUnderscores = -"\\W"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @warning except \n
|
|
||||||
*/
|
|
||||||
val any = -"."
|
|
||||||
val beg = -"^"
|
|
||||||
val begin = -"^"
|
|
||||||
val end = -"$"
|
|
||||||
val empty = -"()"
|
|
||||||
|
|
||||||
val uppercase = 'A' % 'Z'
|
|
||||||
val lowercase = 'a' % 'z'
|
|
||||||
val numbers = '0' % '9'
|
|
||||||
|
|
||||||
val Char.control
|
|
||||||
get() = ControlCharRegexUnit(this)
|
|
||||||
|
|
||||||
infix fun RegexUnit.link(target: RegexUnit) = +"$this$target"
|
|
||||||
infix fun RegexUnit.link(target: (() -> RegexUnit)) = +"$this${target()}"
|
|
||||||
infix fun (() -> RegexUnit).link(target: RegexUnit) = +"${this()}$target"
|
|
||||||
infix fun (() -> RegexUnit).link(target: (() -> RegexUnit)) = +"${this()}${target()}"
|
|
||||||
infix fun RegexUnit.also(target: RegexUnit) = this link target
|
|
||||||
infix fun RegexUnit.also(target: (() -> RegexUnit)) = this link target
|
|
||||||
infix fun (() -> RegexUnit).also(target: RegexUnit) = this link target
|
|
||||||
infix fun (() -> RegexUnit).also(target: (() -> RegexUnit)) = this link target
|
|
||||||
infix operator fun RegexUnit.invoke(unit: RegexUnit) = this link unit
|
|
||||||
infix operator fun RegexUnit.invoke(unit: () -> RegexUnit) = this link unit
|
|
||||||
infix operator fun (() -> RegexUnit).invoke(unit: () -> RegexUnit) = this link unit
|
|
||||||
infix operator fun (() -> RegexUnit).invoke(unit: RegexUnit) = this link unit
|
|
||||||
infix operator fun RegexUnit.plus(unit: RegexUnit) = this link unit
|
|
||||||
infix operator fun RegexUnit.plus(unit: () -> RegexUnit) = this link unit
|
|
||||||
infix operator fun (() -> RegexUnit).plus(unit: () -> RegexUnit) = this link unit
|
|
||||||
infix operator fun (() -> RegexUnit).plus(unit: RegexUnit) = this link unit
|
|
||||||
infix operator fun RegexUnit.minus(unit: RegexUnit) = this link unit
|
|
||||||
infix operator fun RegexUnit.minus(unit: () -> RegexUnit) = this link unit
|
|
||||||
infix operator fun (() -> RegexUnit).minus(unit: RegexUnit) = this link unit
|
|
||||||
infix operator fun (() -> RegexUnit).minus(unit: () -> RegexUnit) = this link unit
|
|
||||||
infix operator fun RegexUnit.rangeTo(unit: RegexUnit) = this link unit
|
|
||||||
infix operator fun RegexUnit.rangeTo(unit: () -> RegexUnit) = this link unit
|
|
||||||
infix operator fun (() -> RegexUnit).rangeTo(unit: () -> RegexUnit) = this link unit
|
|
||||||
infix operator fun (() -> RegexUnit).rangeTo(unit: RegexUnit) = this link unit
|
|
||||||
|
|
||||||
val Iterable<RegexUnit>.toSet: StringRegexUnit?
|
|
||||||
get() {
|
|
||||||
val iterator = iterator()
|
|
||||||
if (!iterator.hasNext()) return null
|
|
||||||
val stringBuilder = StringBuilder()
|
|
||||||
stringBuilder.append(iterator.next().unit)
|
|
||||||
forEach {
|
|
||||||
stringBuilder.append("|${it.unit}")
|
|
||||||
}
|
|
||||||
return StringRegexUnit(stringBuilder.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
val Array<out RegexUnit>.toSet: StringRegexUnit?
|
|
||||||
get() {
|
|
||||||
val iterator = iterator()
|
|
||||||
if (!iterator.hasNext()) return null
|
|
||||||
val stringBuilder = StringBuilder()
|
|
||||||
stringBuilder.append(iterator.next().unit)
|
|
||||||
forEach {
|
|
||||||
stringBuilder.append("|${it.unit}")
|
|
||||||
}
|
|
||||||
return StringRegexUnit(stringBuilder.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
infix fun RegexUnit.or(target: RegexUnit): StringRegexUnit {
|
|
||||||
val unit = this.unit
|
|
||||||
val targetUnit = target.unit
|
|
||||||
return +when {
|
|
||||||
unit == null -> targetUnit ?: ""
|
|
||||||
targetUnit == null -> unit
|
|
||||||
else -> "$unit|$targetUnit"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
infix fun (() -> RegexUnit).or(target: RegexUnit) = this() or target
|
|
||||||
|
|
||||||
val RegexUnit.onceMore
|
|
||||||
get() = RepeatRegexUnit(this, 1, -1)
|
|
||||||
val (() -> RegexUnit).onceMore
|
|
||||||
get() = RepeatRegexUnit(this(), 1, -1)
|
|
||||||
|
|
||||||
val RegexUnit.anyTime
|
|
||||||
get() = RepeatRegexUnit(this, -1)
|
|
||||||
val (() -> RegexUnit).anyTime
|
|
||||||
get() = RepeatRegexUnit(this(), -1)
|
|
||||||
|
|
||||||
val RegexUnit.onceBelow
|
|
||||||
get() = RepeatRegexUnit(this, 0, 1)
|
|
||||||
val (() -> RegexUnit).onceBelow
|
|
||||||
get() = RepeatRegexUnit(this(), 0, 1)
|
|
||||||
|
|
||||||
infix fun RegexUnit.repeat(times: Int) = RepeatRegexUnit(this, times)
|
|
||||||
infix fun RegexUnit.repeat(times: IntRange) = RepeatRegexUnit(this, times)
|
|
||||||
infix fun RegexUnit.repeat(times: Pair<Int, Int>) = RepeatRegexUnit(this, times)
|
|
||||||
fun RegexUnit.timeRange(from: Int, to: Int) = RepeatRegexUnit(this, from, to)
|
|
||||||
|
|
||||||
infix fun (() -> RegexUnit).repeat(times: Int) = RepeatRegexUnit(this(), times)
|
|
||||||
infix fun (() -> RegexUnit).repeat(times: IntRange) = RepeatRegexUnit(this(), times)
|
|
||||||
infix fun (() -> RegexUnit).repeat(times: Pair<Int, Int>) = RepeatRegexUnit(this(), times)
|
|
||||||
fun (() -> RegexUnit).timeRange(from: Int, to: Int) = RepeatRegexUnit(this(), from, to)
|
|
||||||
|
|
||||||
infix operator fun RegexUnit.invoke(times: Int) = this repeat times
|
|
||||||
infix operator fun RegexUnit.invoke(times: IntRange) = this repeat times
|
|
||||||
infix operator fun RegexUnit.invoke(times: Pair<Int, Int>) = this repeat times
|
|
||||||
operator fun RegexUnit.invoke(from: Int, to: Int) = this.timeRange(from, to)
|
|
||||||
|
|
||||||
infix operator fun (() -> RegexUnit).invoke(unit: Int) = this()(unit)
|
|
||||||
infix operator fun (() -> RegexUnit).invoke(unit: IntRange) = this()(unit)
|
|
||||||
infix operator fun (() -> RegexUnit).invoke(unit: Pair<Int, Int>) = this()(unit)
|
|
||||||
operator fun (() -> RegexUnit).invoke(from: Int, to: Int) = this().timeRange(from, to)
|
|
||||||
|
|
||||||
infix fun RegexUnit.upTo(times: Int) = RepeatRegexUnit(this, 0, times)
|
|
||||||
infix fun RegexUnit.repeatTime(times: Int) = RepeatRegexUnit(this, times)
|
|
||||||
infix fun RegexUnit.repeatLast(times: Int) = RepeatRegexUnit(this, times, -1)
|
|
||||||
infix fun RegexUnit.last(times: Int) = RepeatRegexUnit(this, times, -1)
|
|
||||||
|
|
||||||
infix fun (() -> RegexUnit).upTo(times: Int) = this() upTo times
|
|
||||||
infix fun (() -> RegexUnit).repeatTime(times: Int) = this() repeatTime times
|
|
||||||
infix fun (() -> RegexUnit).repeatLast(times: Int) = this() repeatLast times
|
|
||||||
infix fun (() -> RegexUnit).last(times: Int) = this() last times
|
|
||||||
|
|
||||||
infix operator fun RegexUnit.rem(times: Int) = RepeatRegexUnit(this, times, -1)
|
|
||||||
infix operator fun RegexUnit.times(times: Int) = RepeatRegexUnit(this, times)
|
|
||||||
infix operator fun RegexUnit.minus(times: Int) = RepeatRegexUnit(this, 0, times)
|
|
||||||
infix operator fun RegexUnit.div(times: Int) = RepeatRegexUnit(this, 0, times)
|
|
||||||
infix operator fun RegexUnit.rangeTo(range: Int) = RepeatRegexUnit(this, range)
|
|
||||||
|
|
||||||
infix operator fun (() -> RegexUnit).rem(times: Int) = this() rem times
|
|
||||||
infix operator fun (() -> RegexUnit).times(times: Int) = this() times times
|
|
||||||
infix operator fun (() -> RegexUnit).minus(times: Int) = this() minus times
|
|
||||||
infix operator fun (() -> RegexUnit).div(times: Int) = this() div times
|
|
||||||
infix operator fun (() -> RegexUnit).rangeTo(range: Int) = this() rangeTo range
|
|
||||||
|
|
||||||
infix operator fun RegexUnit.rem(range: Pair<Int, Int>) = RepeatRegexUnit(this, range)
|
|
||||||
infix operator fun RegexUnit.rem(range: IntRange) = RepeatRegexUnit(this, range)
|
|
||||||
infix operator fun RegexUnit.times(range: IntRange) = RepeatRegexUnit(this, range)
|
|
||||||
infix operator fun RegexUnit.times(range: Pair<Int, Int>) = RepeatRegexUnit(this, range)
|
|
||||||
infix operator fun RegexUnit.minus(range: IntRange) = RepeatRegexUnit(this, range)
|
|
||||||
infix operator fun RegexUnit.minus(range: Pair<Int, Int>) = RepeatRegexUnit(this, range)
|
|
||||||
infix operator fun RegexUnit.rangeTo(range: IntRange) = RepeatRegexUnit(this, range)
|
|
||||||
infix operator fun RegexUnit.rangeTo(range: Pair<Int, Int>) = RepeatRegexUnit(this, range)
|
|
||||||
|
|
||||||
infix operator fun (() -> RegexUnit).rem(range: Pair<Int, Int>) = this() rem range
|
|
||||||
infix operator fun (() -> RegexUnit).rem(range: IntRange) = this() rem range
|
|
||||||
infix operator fun (() -> RegexUnit).times(range: IntRange) = this() times range
|
|
||||||
infix operator fun (() -> RegexUnit).times(range: Pair<Int, Int>) = this() times range
|
|
||||||
infix operator fun (() -> RegexUnit).minus(range: IntRange) = this() minus range
|
|
||||||
infix operator fun (() -> RegexUnit).minus(range: Pair<Int, Int>) = this() minus range
|
|
||||||
infix operator fun (() -> RegexUnit).rangeTo(range: IntRange) = this() rangeTo range
|
|
||||||
infix operator fun (() -> RegexUnit).rangeTo(range: Pair<Int, Int>) = this() rangeTo range
|
|
||||||
|
|
||||||
infix fun Char.list(target: Char) = UnitListRegexUnit(this, target)
|
|
||||||
infix operator fun Char.rem(char: Char) = UnitListRegexUnit(this, char)
|
|
||||||
operator fun CharRange.unaryPlus() = UnitListRegexUnit(this)
|
|
||||||
operator fun CharRange.unaryMinus() = UnitListRegexUnit(this)
|
|
||||||
operator fun CharRange.not() = UnitListRegexUnit(this)
|
|
||||||
operator fun Pair<Char, Char>.unaryPlus() = UnitListRegexUnit(this)
|
|
||||||
operator fun Pair<Char, Char>.unaryMinus() = UnitListRegexUnit(this)
|
|
||||||
operator fun Pair<Char, Char>.not() = UnitListRegexUnit(this)
|
|
||||||
|
|
||||||
infix operator fun UnitListRegexUnit.invoke(unitList: UnitListRegexUnit) = this and unitList
|
|
||||||
|
|
||||||
object UnitList {
|
|
||||||
const val hyphen = "\\-"
|
|
||||||
const val slush = "\\\\"
|
|
||||||
operator fun invoke(action: UnitList.() -> Any) = !this.action()
|
|
||||||
}
|
|
||||||
|
|
||||||
class UnitListCheckException : Exception()
|
|
||||||
|
|
||||||
operator fun String.not() = UnitListRegexUnit(this)
|
|
||||||
operator fun Any.not() = !toString()
|
|
||||||
private val listChar = Regex("[-\\\\]")
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取str的字面符号表示的字符表
|
|
||||||
*/
|
|
||||||
fun list(str: String): UnitListRegexUnit {
|
|
||||||
if (!listChar.containsMatchIn(str)) {
|
|
||||||
return !str
|
|
||||||
}
|
|
||||||
val sb = StringBuilder()
|
|
||||||
str.forEach { c ->
|
|
||||||
when (c) {
|
|
||||||
'\\', '-', ':' -> sb.append("\\")
|
|
||||||
}
|
|
||||||
sb.append(c)
|
|
||||||
}
|
|
||||||
return !sb
|
|
||||||
}
|
|
||||||
|
|
||||||
val RegexUnit.nonGetMatch get() = NonGetMatchingUnit(this)
|
|
||||||
val RegexUnit.lookAheadPositiveAssert get() = LookAheadPositiveAssertUnit(this)
|
|
||||||
val RegexUnit.lookAheadNegativeAssert get() = LookAheadNegativeAssertUnit(this)
|
|
||||||
val RegexUnit.lookBehindPositiveAssert get() = LookBehindPositiveAssertUnit(this)
|
|
||||||
val RegexUnit.lookBehindNegativeAssert get() = LookBehindNegativeAssertUnit(this)
|
|
||||||
|
|
||||||
fun make(func: RegexMaker.() -> RegexUnit) = this.func()
|
|
||||||
|
|
||||||
operator fun invoke(func: RegexMaker.() -> RegexUnit) = this.func()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun regex(func: RegexMaker.() -> RegexUnit) = Regex(RegexMaker.func().toString())
|
|
@ -1,7 +0,0 @@
|
|||||||
package cn.tursom.core.regex
|
|
||||||
|
|
||||||
interface RegexUnit {
|
|
||||||
val unit: String? get() = toString()
|
|
||||||
val regex get() = toString().toRegex()
|
|
||||||
override fun toString(): String
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
package cn.tursom.core.regex
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 ${repeatUnit} 匹配 ${from} 到 ${to} 次
|
|
||||||
* 匹配任意次数 如果 ${from} < 0
|
|
||||||
* 精确匹配 ${from} 次 如果 ${to} == 0
|
|
||||||
* 最少匹配 ${from} 次 如果 ${to} < 0
|
|
||||||
*/
|
|
||||||
class RepeatRegexUnit(repeatUnit: RegexUnit?, from: Int, to: Int = 0) : RegexUnit {
|
|
||||||
constructor(repeatUnit: RegexUnit?, range: IntRange) : this(repeatUnit, range.start, range.last)
|
|
||||||
constructor(repeatUnit: RegexUnit?, range: Pair<Int, Int>) : this(repeatUnit, range.first, range.second)
|
|
||||||
|
|
||||||
private val str = when {
|
|
||||||
from < 0 -> "*"
|
|
||||||
to == 0 -> when (from) {
|
|
||||||
0 -> null
|
|
||||||
1 -> ""
|
|
||||||
else -> "{$from}"
|
|
||||||
}
|
|
||||||
to < 0 -> when (from) {
|
|
||||||
0 -> "*"
|
|
||||||
1 -> "+"
|
|
||||||
else -> "{$from,}"
|
|
||||||
}
|
|
||||||
to == 1 && from == 0 -> "?"
|
|
||||||
to == from -> when (from) {
|
|
||||||
0 -> null
|
|
||||||
1 -> ""
|
|
||||||
else -> "{$from}"
|
|
||||||
}
|
|
||||||
else -> "{$from,$to}"
|
|
||||||
}?.let { range ->
|
|
||||||
repeatUnit?.unit?.let {
|
|
||||||
if (it.isNotEmpty()) "$it$range"
|
|
||||||
else ""
|
|
||||||
}
|
|
||||||
} ?: ""
|
|
||||||
|
|
||||||
override val unit = if (str.isEmpty()) {
|
|
||||||
""
|
|
||||||
} else {
|
|
||||||
"($str)"
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString() = str
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package cn.tursom.core.regex
|
|
||||||
|
|
||||||
class StringRegexUnit(private val str: String) : RegexUnit {
|
|
||||||
constructor(str: StringRegexUnit) : this(str.str)
|
|
||||||
|
|
||||||
override val unit = when (str.length) {
|
|
||||||
0 -> null
|
|
||||||
1 -> str
|
|
||||||
else -> "($str)"
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString() = str
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
package cn.tursom.core.regex
|
|
||||||
|
|
||||||
class UnitListRegexUnit(private val valList: String) : RegexUnit {
|
|
||||||
constructor(from: Char, to: Char) : this("$from-$to")
|
|
||||||
constructor(range: Pair<Char, Char>) : this("${range.first}-${range.second}")
|
|
||||||
constructor(range: CharRange) : this("${range.first}-${range.last}")
|
|
||||||
constructor(range: UnitListRegexUnit) : this(range.valList)
|
|
||||||
|
|
||||||
val reverse
|
|
||||||
get() = UnitListRegexUnit(if (valList.first() == '^') valList.drop(1) else "^$valList")
|
|
||||||
|
|
||||||
operator fun not() = reverse
|
|
||||||
infix operator fun plus(unitList: UnitListRegexUnit) = UnitListRegexUnit("$valList${unitList.valList}")
|
|
||||||
infix operator fun times(unitList: UnitListRegexUnit) = UnitListRegexUnit("$valList${unitList.valList}")
|
|
||||||
infix fun also(unitList: UnitListRegexUnit) = UnitListRegexUnit("$valList${unitList.valList}")
|
|
||||||
infix fun and(unitList: UnitListRegexUnit) = UnitListRegexUnit("$valList${unitList.valList}")
|
|
||||||
infix fun link(unitList: UnitListRegexUnit) = UnitListRegexUnit("$valList${unitList.valList}")
|
|
||||||
|
|
||||||
override fun toString() = "[$valList]"
|
|
||||||
}
|
|
12
ts-core/build.gradle.kts
Normal file
12
ts-core/build.gradle.kts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
plugins {
|
||||||
|
kotlin("jvm")
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
api(project(":"))
|
||||||
|
compileOnly(group = "com.google.code.gson", name = "gson", version = "2.8.6")
|
||||||
|
}
|
||||||
|
|
||||||
|
@kotlin.Suppress("UNCHECKED_CAST")
|
||||||
|
(rootProject.ext["excludeTest"] as (Project, TaskContainer) -> Unit)(project, tasks)
|
||||||
|
|
@ -3,7 +3,6 @@ package cn.tursom.core
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.net.URLClassLoader
|
import java.net.URLClassLoader
|
||||||
import java.util.ArrayList
|
|
||||||
import java.util.jar.JarFile
|
import java.util.jar.JarFile
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
@ -60,11 +59,13 @@ object ClassLoaderUtil {
|
|||||||
for (childFile in childFiles) {
|
for (childFile in childFiles) {
|
||||||
if (childFile.isDirectory) {
|
if (childFile.isDirectory) {
|
||||||
if (childPackage) {
|
if (childPackage) {
|
||||||
myClassName.addAll(getClassNameByFile(
|
myClassName.addAll(
|
||||||
childFile.path,
|
getClassNameByFile(
|
||||||
"$basePackage.${childFile.path.substringAfterLast(File.separator)}",
|
childFile.path,
|
||||||
childPackage
|
"$basePackage.${childFile.path.substringAfterLast(File.separator)}",
|
||||||
))
|
childPackage
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val childFilePath: String = childFile.path
|
val childFilePath: String = childFile.path
|
@ -1,7 +1,5 @@
|
|||||||
package cn.tursom.core
|
package cn.tursom.core
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor
|
|
||||||
import kotlin.concurrent.thread
|
import kotlin.concurrent.thread
|
||||||
|
|
||||||
|
|
@ -23,8 +23,8 @@ open class NonLockLinkedList<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class TaskListNode<T>(
|
private class TaskListNode<T>(
|
||||||
val data: T,
|
val data: T,
|
||||||
@Volatile var next: TaskListNode<T>?
|
@Volatile var next: TaskListNode<T>?
|
||||||
)
|
)
|
||||||
|
|
||||||
class NotSupportedException : Exception()
|
class NotSupportedException : Exception()
|
@ -7,9 +7,6 @@ import java.lang.reflect.Modifier
|
|||||||
import java.lang.reflect.ParameterizedType
|
import java.lang.reflect.ParameterizedType
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.collections.ArrayList
|
|
||||||
import kotlin.collections.HashMap
|
|
||||||
import kotlin.collections.HashSet
|
|
||||||
|
|
||||||
object Parser {
|
object Parser {
|
||||||
private val dateFormat = ThreadLocalSimpleDateFormat()
|
private val dateFormat = ThreadLocalSimpleDateFormat()
|
24
ts-core/src/main/kotlin/cn/tursom/core/RandomCode.kt
Normal file
24
ts-core/src/main/kotlin/cn/tursom/core/RandomCode.kt
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package cn.tursom.core
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class RandomCode {
|
||||||
|
private val randomCode = "${randomInt(10000000, 99999999)}"
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return randomCode
|
||||||
|
}
|
||||||
|
|
||||||
|
fun showCode(codeName: String = "passcode", filepath: String? = null) {
|
||||||
|
println("$codeName: $randomCode")
|
||||||
|
filepath ?: return
|
||||||
|
val file = File(filepath)
|
||||||
|
file.createNewFile()
|
||||||
|
file.writeText("$codeName = $randomCode")
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private fun randomInt(min: Int, max: Int) = Random().nextInt(max) % (max - min + 1) + min
|
||||||
|
}
|
||||||
|
}
|
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
package cn.tursom.core
|
package cn.tursom.core
|
||||||
|
|
||||||
import cn.tursom.core.datastruct.ReversedList
|
|
||||||
import cn.tursom.core.datastruct.StepList
|
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.google.gson.GsonBuilder
|
import com.google.gson.GsonBuilder
|
||||||
import sun.reflect.Reflection
|
import sun.reflect.Reflection
|
||||||
@ -19,8 +17,6 @@ import java.security.MessageDigest
|
|||||||
import java.security.NoSuchAlgorithmException
|
import java.security.NoSuchAlgorithmException
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.Executor
|
import java.util.concurrent.Executor
|
||||||
import kotlin.collections.ArrayList
|
|
||||||
import kotlin.collections.HashSet
|
|
||||||
import kotlin.contracts.ExperimentalContracts
|
import kotlin.contracts.ExperimentalContracts
|
||||||
import kotlin.contracts.contract
|
import kotlin.contracts.contract
|
||||||
import kotlin.coroutines.resume
|
import kotlin.coroutines.resume
|
||||||
@ -386,35 +382,6 @@ fun Any.serialize(): ByteArray {
|
|||||||
return outputStream.toByteArray()
|
return outputStream.toByteArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
operator fun <E> List<E>.get(startIndex: Int = 0, endIndex: Int = size, step: Int = 1): List<E> {
|
|
||||||
if (step <= 0) throw IllegalArgumentException("step($step) is negative or zero")
|
|
||||||
val fromIndex = when {
|
|
||||||
startIndex < 0 -> size + startIndex
|
|
||||||
startIndex >= size -> size
|
|
||||||
else -> startIndex
|
|
||||||
}
|
|
||||||
val toIndex = when {
|
|
||||||
endIndex < 0 -> size + endIndex + 1
|
|
||||||
endIndex >= size -> size
|
|
||||||
else -> endIndex
|
|
||||||
}
|
|
||||||
var targetList = if (fromIndex > toIndex) ReversedList(subList(toIndex, fromIndex)) else subList(fromIndex, toIndex)
|
|
||||||
if (step != 1) targetList = targetList step step
|
|
||||||
return targetList
|
|
||||||
}
|
|
||||||
|
|
||||||
operator fun <E> List<E>.get(intProgression: IntProgression): List<E> {
|
|
||||||
val first = intProgression.first
|
|
||||||
val last = intProgression.last
|
|
||||||
val step = intProgression.step
|
|
||||||
return when {
|
|
||||||
step == 0 -> get(first, last + if (last < 0) 0 else 1, 1)
|
|
||||||
step < 0 -> get(first + if (last > 0 && first >= 0) 1 else 0, last, -step)
|
|
||||||
else -> get(first, last + if (last < 0) 0 else 1, step)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
infix fun <E> List<E>.step(step: Int): List<E> = StepList(this, step)
|
|
||||||
|
|
||||||
inline infix fun String.ifEmpty(ifEmpty: () -> String) = if (isNotEmpty()) this else ifEmpty()
|
inline infix fun String.ifEmpty(ifEmpty: () -> String) = if (isNotEmpty()) this else ifEmpty()
|
||||||
inline infix fun String.ifBlank(ifBlank: () -> String) = if (isNotBlank()) this else ifBlank()
|
inline infix fun String.ifBlank(ifBlank: () -> String) = if (isNotBlank()) this else ifBlank()
|
@ -0,0 +1,7 @@
|
|||||||
|
package cn.tursom.core.regex
|
||||||
|
|
||||||
|
class ControlCharRegexUnit(private val char: Char) : RegexUnit {
|
||||||
|
constructor(char: ControlCharRegexUnit) : this(char.char)
|
||||||
|
|
||||||
|
override fun toString() = "\\c$char"
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package cn.tursom.core.regex
|
||||||
|
|
||||||
|
class GetMatchingUnit(val subUnit: RegexUnit) : RegexUnit {
|
||||||
|
override val unit: String?
|
||||||
|
get() = toString()
|
||||||
|
|
||||||
|
override fun toString(): String = "($subUnit)"
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
package cn.tursom.core.regex
|
package cn.tursom.core.regex
|
||||||
|
|
||||||
class LookAheadNegativeAssertUnit(val subUnit: RegexUnit) : RegexUnit {
|
class LookAheadNegativeAssertUnit(val subUnit: RegexUnit) : RegexUnit {
|
||||||
override fun toString(): String = "(?=$subUnit)"
|
override fun toString(): String = "(?=$subUnit)"
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
package cn.tursom.core.regex
|
package cn.tursom.core.regex
|
||||||
|
|
||||||
class LookAheadPositiveAssertUnit(val subUnit: RegexUnit) : RegexUnit {
|
class LookAheadPositiveAssertUnit(val subUnit: RegexUnit) : RegexUnit {
|
||||||
override fun toString(): String = "(?=$subUnit)"
|
override fun toString(): String = "(?=$subUnit)"
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
package cn.tursom.core.regex
|
package cn.tursom.core.regex
|
||||||
|
|
||||||
class LookBehindNegativeAssertUnit(val subUnit: RegexUnit) : RegexUnit {
|
class LookBehindNegativeAssertUnit(val subUnit: RegexUnit) : RegexUnit {
|
||||||
override fun toString(): String = "(?<!$subUnit)"
|
override fun toString(): String = "(?<!$subUnit)"
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
package cn.tursom.core.regex
|
package cn.tursom.core.regex
|
||||||
|
|
||||||
class LookBehindPositiveAssertUnit(val subUnit: RegexUnit) : RegexUnit {
|
class LookBehindPositiveAssertUnit(val subUnit: RegexUnit) : RegexUnit {
|
||||||
override fun toString(): String = "(?<=$subUnit)"
|
override fun toString(): String = "(?<=$subUnit)"
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
package cn.tursom.core.regex
|
package cn.tursom.core.regex
|
||||||
|
|
||||||
class NonGetMatchingUnit(val subUnit: RegexUnit) : RegexUnit {
|
class NonGetMatchingUnit(val subUnit: RegexUnit) : RegexUnit {
|
||||||
override fun toString(): String = "(?:$subUnit)"
|
override fun toString(): String = "(?:$subUnit)"
|
||||||
}
|
}
|
278
ts-core/src/main/kotlin/cn/tursom/core/regex/RegexMaker.kt
Normal file
278
ts-core/src/main/kotlin/cn/tursom/core/regex/RegexMaker.kt
Normal file
@ -0,0 +1,278 @@
|
|||||||
|
package cn.tursom.core.regex
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用 regex 函数创建一个正则对象
|
||||||
|
*
|
||||||
|
* 字符串前加 + 表示一个字符串单元,任何时候都会作为一个独立单元存在
|
||||||
|
* 字符串前加 - 表示一个字符串,不会作为一个独立单元处理
|
||||||
|
* 注意,我们不支持原始的字符串对象,请使用 + 或 - 将其打包
|
||||||
|
*
|
||||||
|
* 在 RegexMaker 对象头部的这些对象都是字符转义,请根据需要使用
|
||||||
|
* uppercase,lowercase 与 numbers 都是字符列表,用于表示在其范围内的单个字符
|
||||||
|
* 使用 Char.control 获得控制字符转义
|
||||||
|
*
|
||||||
|
* 接下来是连接两个正则单元用的方法,我们可以用 (单元1) link (单元2),(单元1) also (单元2),甚至是 (单元1)(单元2) 的形式连接两个单元
|
||||||
|
* 当然,现在我们也可以用 + 甚至是 - 来连接两个单元了
|
||||||
|
*
|
||||||
|
* 接着是创建单元组的方法,toSet 不建议使用,建议使用 ((单元1) or (单元2) or ...) 的形式创建一个单元组
|
||||||
|
*
|
||||||
|
* 后面跟着的就都是表示重复次数的方法,(单元)-次数n 表示最多重复n次,相应的 * 与 .. 表示精确的 n 次,%表示至少 n 次
|
||||||
|
* 我们还可以使用 (单元)-(min..max)的形式指定重复区间,对于这个接口,-、*、% 与 .. 的效果相同,范围区间还可以使用(min to max)的形式
|
||||||
|
* 有时候,- 会导致运算符优先级的问题,这时我们可以用 / 来代替
|
||||||
|
*
|
||||||
|
* 如果我们想匹配属于某一组的单个字符,可以使用 (开始字符 % 结束字符) 的形式,使用 and、also、link 或者 + 将多个字符组单元相连
|
||||||
|
* 我们还可以在一个 CharRange 或 Pair<Char, Char> 前面加 + 生成字符组
|
||||||
|
* 或者我们也可以手动指定使用哪些字符,使用 list 方法,字符串会被转义成一个字符组单元
|
||||||
|
* 如果你对自己足够有信心,也可以在字符串前面加 !,这会直接生成一个字符组对象,不经检查
|
||||||
|
*
|
||||||
|
* 比如下面这句就是一个合法的表达式
|
||||||
|
* '1' % '2' / 5 + list("ABC-\\") * lowercase * numbers % (2 to 3) + uppercase + any % 3 + caret % 1 + +"还行" * 2
|
||||||
|
* 运行后会生成
|
||||||
|
* [1-2]{0,5}[ABC\-\\a-z0-9]{2,3}[A-Z].{3,}\^+(还行){2}
|
||||||
|
*/
|
||||||
|
@Suppress("unused", "MemberVisibilityCanBePrivate", "DuplicatedCode")
|
||||||
|
object RegexMaker {
|
||||||
|
operator fun String.unaryPlus() = StringRegexUnit(this)
|
||||||
|
operator fun String.unaryMinus() = UnitRegexUnit(this)
|
||||||
|
operator fun RegexUnit.unaryPlus() = GetMatchingUnit(this)
|
||||||
|
operator fun RegexUnit.unaryMinus() = NonGetMatchingUnit(this)
|
||||||
|
|
||||||
|
val Any.str
|
||||||
|
get() = +toString()
|
||||||
|
val Any.unit
|
||||||
|
get() = -toString()
|
||||||
|
|
||||||
|
val slush = -"\\\\"
|
||||||
|
val point = -"\\."
|
||||||
|
val caret = -"\\^"
|
||||||
|
val dollar = -"\\$"
|
||||||
|
val plus = -"\\+"
|
||||||
|
val minus = -"\\-"
|
||||||
|
val star = -"\\*"
|
||||||
|
val roundBrackets = -"\\("
|
||||||
|
val squareBrackets = -"\\["
|
||||||
|
val curlyBrackets = -"\\{"
|
||||||
|
val backslash = -"\\\\"
|
||||||
|
val verticalBar = -"\\|"
|
||||||
|
val questionMark = -"\\?"
|
||||||
|
val nextPage = -"\\f"
|
||||||
|
val nextLine = -"\\n"
|
||||||
|
val enter = -"\\r"
|
||||||
|
val blankCharacter = -"\\s"
|
||||||
|
val nonBlankCharacter = -"\\S"
|
||||||
|
val tab = -"\\t"
|
||||||
|
val verticalTab = -"\\v"
|
||||||
|
val wordBoundary = -"\\b"
|
||||||
|
val nonWordBoundary = -"\\B"
|
||||||
|
val number = -"\\d"
|
||||||
|
val nonNumber = -"\\D"
|
||||||
|
val pageBreak = -"\\f"
|
||||||
|
val lineBreak = -"n"
|
||||||
|
val carriageReturn = -"\\r"
|
||||||
|
val lettersNumbersUnderscores = -"\\w"
|
||||||
|
val nonLettersNumbersUnderscores = -"\\W"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @warning except \n
|
||||||
|
*/
|
||||||
|
val any = -"."
|
||||||
|
val beg = -"^"
|
||||||
|
val begin = -"^"
|
||||||
|
val end = -"$"
|
||||||
|
val empty = -"()"
|
||||||
|
|
||||||
|
val uppercase = 'A' % 'Z'
|
||||||
|
val lowercase = 'a' % 'z'
|
||||||
|
val numbers = '0' % '9'
|
||||||
|
|
||||||
|
val Char.control
|
||||||
|
get() = ControlCharRegexUnit(this)
|
||||||
|
|
||||||
|
infix fun RegexUnit.link(target: RegexUnit) = +"$this$target"
|
||||||
|
infix fun RegexUnit.link(target: (() -> RegexUnit)) = +"$this${target()}"
|
||||||
|
infix fun (() -> RegexUnit).link(target: RegexUnit) = +"${this()}$target"
|
||||||
|
infix fun (() -> RegexUnit).link(target: (() -> RegexUnit)) = +"${this()}${target()}"
|
||||||
|
infix fun RegexUnit.also(target: RegexUnit) = this link target
|
||||||
|
infix fun RegexUnit.also(target: (() -> RegexUnit)) = this link target
|
||||||
|
infix fun (() -> RegexUnit).also(target: RegexUnit) = this link target
|
||||||
|
infix fun (() -> RegexUnit).also(target: (() -> RegexUnit)) = this link target
|
||||||
|
infix operator fun RegexUnit.invoke(unit: RegexUnit) = this link unit
|
||||||
|
infix operator fun RegexUnit.invoke(unit: () -> RegexUnit) = this link unit
|
||||||
|
infix operator fun (() -> RegexUnit).invoke(unit: () -> RegexUnit) = this link unit
|
||||||
|
infix operator fun (() -> RegexUnit).invoke(unit: RegexUnit) = this link unit
|
||||||
|
infix operator fun RegexUnit.plus(unit: RegexUnit) = this link unit
|
||||||
|
infix operator fun RegexUnit.plus(unit: () -> RegexUnit) = this link unit
|
||||||
|
infix operator fun (() -> RegexUnit).plus(unit: () -> RegexUnit) = this link unit
|
||||||
|
infix operator fun (() -> RegexUnit).plus(unit: RegexUnit) = this link unit
|
||||||
|
infix operator fun RegexUnit.minus(unit: RegexUnit) = this link unit
|
||||||
|
infix operator fun RegexUnit.minus(unit: () -> RegexUnit) = this link unit
|
||||||
|
infix operator fun (() -> RegexUnit).minus(unit: RegexUnit) = this link unit
|
||||||
|
infix operator fun (() -> RegexUnit).minus(unit: () -> RegexUnit) = this link unit
|
||||||
|
infix operator fun RegexUnit.rangeTo(unit: RegexUnit) = this link unit
|
||||||
|
infix operator fun RegexUnit.rangeTo(unit: () -> RegexUnit) = this link unit
|
||||||
|
infix operator fun (() -> RegexUnit).rangeTo(unit: () -> RegexUnit) = this link unit
|
||||||
|
infix operator fun (() -> RegexUnit).rangeTo(unit: RegexUnit) = this link unit
|
||||||
|
|
||||||
|
val Iterable<RegexUnit>.toSet: StringRegexUnit?
|
||||||
|
get() {
|
||||||
|
val iterator = iterator()
|
||||||
|
if (!iterator.hasNext()) return null
|
||||||
|
val stringBuilder = StringBuilder()
|
||||||
|
stringBuilder.append(iterator.next().unit)
|
||||||
|
forEach {
|
||||||
|
stringBuilder.append("|${it.unit}")
|
||||||
|
}
|
||||||
|
return StringRegexUnit(stringBuilder.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
val Array<out RegexUnit>.toSet: StringRegexUnit?
|
||||||
|
get() {
|
||||||
|
val iterator = iterator()
|
||||||
|
if (!iterator.hasNext()) return null
|
||||||
|
val stringBuilder = StringBuilder()
|
||||||
|
stringBuilder.append(iterator.next().unit)
|
||||||
|
forEach {
|
||||||
|
stringBuilder.append("|${it.unit}")
|
||||||
|
}
|
||||||
|
return StringRegexUnit(stringBuilder.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
infix fun RegexUnit.or(target: RegexUnit): StringRegexUnit {
|
||||||
|
val unit = this.unit
|
||||||
|
val targetUnit = target.unit
|
||||||
|
return +when {
|
||||||
|
unit == null -> targetUnit ?: ""
|
||||||
|
targetUnit == null -> unit
|
||||||
|
else -> "$unit|$targetUnit"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
infix fun (() -> RegexUnit).or(target: RegexUnit) = this() or target
|
||||||
|
|
||||||
|
val RegexUnit.onceMore
|
||||||
|
get() = RepeatRegexUnit(this, 1, -1)
|
||||||
|
val (() -> RegexUnit).onceMore
|
||||||
|
get() = RepeatRegexUnit(this(), 1, -1)
|
||||||
|
|
||||||
|
val RegexUnit.anyTime
|
||||||
|
get() = RepeatRegexUnit(this, -1)
|
||||||
|
val (() -> RegexUnit).anyTime
|
||||||
|
get() = RepeatRegexUnit(this(), -1)
|
||||||
|
|
||||||
|
val RegexUnit.onceBelow
|
||||||
|
get() = RepeatRegexUnit(this, 0, 1)
|
||||||
|
val (() -> RegexUnit).onceBelow
|
||||||
|
get() = RepeatRegexUnit(this(), 0, 1)
|
||||||
|
|
||||||
|
infix fun RegexUnit.repeat(times: Int) = RepeatRegexUnit(this, times)
|
||||||
|
infix fun RegexUnit.repeat(times: IntRange) = RepeatRegexUnit(this, times)
|
||||||
|
infix fun RegexUnit.repeat(times: Pair<Int, Int>) = RepeatRegexUnit(this, times)
|
||||||
|
fun RegexUnit.timeRange(from: Int, to: Int) = RepeatRegexUnit(this, from, to)
|
||||||
|
|
||||||
|
infix fun (() -> RegexUnit).repeat(times: Int) = RepeatRegexUnit(this(), times)
|
||||||
|
infix fun (() -> RegexUnit).repeat(times: IntRange) = RepeatRegexUnit(this(), times)
|
||||||
|
infix fun (() -> RegexUnit).repeat(times: Pair<Int, Int>) = RepeatRegexUnit(this(), times)
|
||||||
|
fun (() -> RegexUnit).timeRange(from: Int, to: Int) = RepeatRegexUnit(this(), from, to)
|
||||||
|
|
||||||
|
infix operator fun RegexUnit.invoke(times: Int) = this repeat times
|
||||||
|
infix operator fun RegexUnit.invoke(times: IntRange) = this repeat times
|
||||||
|
infix operator fun RegexUnit.invoke(times: Pair<Int, Int>) = this repeat times
|
||||||
|
operator fun RegexUnit.invoke(from: Int, to: Int) = this.timeRange(from, to)
|
||||||
|
|
||||||
|
infix operator fun (() -> RegexUnit).invoke(unit: Int) = this()(unit)
|
||||||
|
infix operator fun (() -> RegexUnit).invoke(unit: IntRange) = this()(unit)
|
||||||
|
infix operator fun (() -> RegexUnit).invoke(unit: Pair<Int, Int>) = this()(unit)
|
||||||
|
operator fun (() -> RegexUnit).invoke(from: Int, to: Int) = this().timeRange(from, to)
|
||||||
|
|
||||||
|
infix fun RegexUnit.upTo(times: Int) = RepeatRegexUnit(this, 0, times)
|
||||||
|
infix fun RegexUnit.repeatTime(times: Int) = RepeatRegexUnit(this, times)
|
||||||
|
infix fun RegexUnit.repeatLast(times: Int) = RepeatRegexUnit(this, times, -1)
|
||||||
|
infix fun RegexUnit.last(times: Int) = RepeatRegexUnit(this, times, -1)
|
||||||
|
|
||||||
|
infix fun (() -> RegexUnit).upTo(times: Int) = this() upTo times
|
||||||
|
infix fun (() -> RegexUnit).repeatTime(times: Int) = this() repeatTime times
|
||||||
|
infix fun (() -> RegexUnit).repeatLast(times: Int) = this() repeatLast times
|
||||||
|
infix fun (() -> RegexUnit).last(times: Int) = this() last times
|
||||||
|
|
||||||
|
infix operator fun RegexUnit.rem(times: Int) = RepeatRegexUnit(this, times, -1)
|
||||||
|
infix operator fun RegexUnit.times(times: Int) = RepeatRegexUnit(this, times)
|
||||||
|
infix operator fun RegexUnit.minus(times: Int) = RepeatRegexUnit(this, 0, times)
|
||||||
|
infix operator fun RegexUnit.div(times: Int) = RepeatRegexUnit(this, 0, times)
|
||||||
|
infix operator fun RegexUnit.rangeTo(range: Int) = RepeatRegexUnit(this, range)
|
||||||
|
|
||||||
|
infix operator fun (() -> RegexUnit).rem(times: Int) = this() rem times
|
||||||
|
infix operator fun (() -> RegexUnit).times(times: Int) = this() times times
|
||||||
|
infix operator fun (() -> RegexUnit).minus(times: Int) = this() minus times
|
||||||
|
infix operator fun (() -> RegexUnit).div(times: Int) = this() div times
|
||||||
|
infix operator fun (() -> RegexUnit).rangeTo(range: Int) = this() rangeTo range
|
||||||
|
|
||||||
|
infix operator fun RegexUnit.rem(range: Pair<Int, Int>) = RepeatRegexUnit(this, range)
|
||||||
|
infix operator fun RegexUnit.rem(range: IntRange) = RepeatRegexUnit(this, range)
|
||||||
|
infix operator fun RegexUnit.times(range: IntRange) = RepeatRegexUnit(this, range)
|
||||||
|
infix operator fun RegexUnit.times(range: Pair<Int, Int>) = RepeatRegexUnit(this, range)
|
||||||
|
infix operator fun RegexUnit.minus(range: IntRange) = RepeatRegexUnit(this, range)
|
||||||
|
infix operator fun RegexUnit.minus(range: Pair<Int, Int>) = RepeatRegexUnit(this, range)
|
||||||
|
infix operator fun RegexUnit.rangeTo(range: IntRange) = RepeatRegexUnit(this, range)
|
||||||
|
infix operator fun RegexUnit.rangeTo(range: Pair<Int, Int>) = RepeatRegexUnit(this, range)
|
||||||
|
|
||||||
|
infix operator fun (() -> RegexUnit).rem(range: Pair<Int, Int>) = this() rem range
|
||||||
|
infix operator fun (() -> RegexUnit).rem(range: IntRange) = this() rem range
|
||||||
|
infix operator fun (() -> RegexUnit).times(range: IntRange) = this() times range
|
||||||
|
infix operator fun (() -> RegexUnit).times(range: Pair<Int, Int>) = this() times range
|
||||||
|
infix operator fun (() -> RegexUnit).minus(range: IntRange) = this() minus range
|
||||||
|
infix operator fun (() -> RegexUnit).minus(range: Pair<Int, Int>) = this() minus range
|
||||||
|
infix operator fun (() -> RegexUnit).rangeTo(range: IntRange) = this() rangeTo range
|
||||||
|
infix operator fun (() -> RegexUnit).rangeTo(range: Pair<Int, Int>) = this() rangeTo range
|
||||||
|
|
||||||
|
infix fun Char.list(target: Char) = UnitListRegexUnit(this, target)
|
||||||
|
infix operator fun Char.rem(char: Char) = UnitListRegexUnit(this, char)
|
||||||
|
operator fun CharRange.unaryPlus() = UnitListRegexUnit(this)
|
||||||
|
operator fun CharRange.unaryMinus() = UnitListRegexUnit(this)
|
||||||
|
operator fun CharRange.not() = UnitListRegexUnit(this)
|
||||||
|
operator fun Pair<Char, Char>.unaryPlus() = UnitListRegexUnit(this)
|
||||||
|
operator fun Pair<Char, Char>.unaryMinus() = UnitListRegexUnit(this)
|
||||||
|
operator fun Pair<Char, Char>.not() = UnitListRegexUnit(this)
|
||||||
|
|
||||||
|
infix operator fun UnitListRegexUnit.invoke(unitList: UnitListRegexUnit) = this and unitList
|
||||||
|
|
||||||
|
object UnitList {
|
||||||
|
const val hyphen = "\\-"
|
||||||
|
const val slush = "\\\\"
|
||||||
|
operator fun invoke(action: UnitList.() -> Any) = !this.action()
|
||||||
|
}
|
||||||
|
|
||||||
|
class UnitListCheckException : Exception()
|
||||||
|
|
||||||
|
operator fun String.not() = UnitListRegexUnit(this)
|
||||||
|
operator fun Any.not() = !toString()
|
||||||
|
private val listChar = Regex("[-\\\\]")
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取str的字面符号表示的字符表
|
||||||
|
*/
|
||||||
|
fun list(str: String): UnitListRegexUnit {
|
||||||
|
if (!listChar.containsMatchIn(str)) {
|
||||||
|
return !str
|
||||||
|
}
|
||||||
|
val sb = StringBuilder()
|
||||||
|
str.forEach { c ->
|
||||||
|
when (c) {
|
||||||
|
'\\', '-', ':' -> sb.append("\\")
|
||||||
|
}
|
||||||
|
sb.append(c)
|
||||||
|
}
|
||||||
|
return !sb
|
||||||
|
}
|
||||||
|
|
||||||
|
val RegexUnit.nonGetMatch get() = NonGetMatchingUnit(this)
|
||||||
|
val RegexUnit.lookAheadPositiveAssert get() = LookAheadPositiveAssertUnit(this)
|
||||||
|
val RegexUnit.lookAheadNegativeAssert get() = LookAheadNegativeAssertUnit(this)
|
||||||
|
val RegexUnit.lookBehindPositiveAssert get() = LookBehindPositiveAssertUnit(this)
|
||||||
|
val RegexUnit.lookBehindNegativeAssert get() = LookBehindNegativeAssertUnit(this)
|
||||||
|
|
||||||
|
fun make(func: RegexMaker.() -> RegexUnit) = this.func()
|
||||||
|
|
||||||
|
operator fun invoke(func: RegexMaker.() -> RegexUnit) = this.func()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun regex(func: RegexMaker.() -> RegexUnit) = Regex(RegexMaker.func().toString())
|
@ -0,0 +1,7 @@
|
|||||||
|
package cn.tursom.core.regex
|
||||||
|
|
||||||
|
interface RegexUnit {
|
||||||
|
val unit: String? get() = toString()
|
||||||
|
val regex get() = toString().toRegex()
|
||||||
|
override fun toString(): String
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package cn.tursom.core.regex
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 ${repeatUnit} 匹配 ${from} 到 ${to} 次
|
||||||
|
* 匹配任意次数 如果 ${from} < 0
|
||||||
|
* 精确匹配 ${from} 次 如果 ${to} == 0
|
||||||
|
* 最少匹配 ${from} 次 如果 ${to} < 0
|
||||||
|
*/
|
||||||
|
class RepeatRegexUnit(repeatUnit: RegexUnit?, from: Int, to: Int = 0) : RegexUnit {
|
||||||
|
constructor(repeatUnit: RegexUnit?, range: IntRange) : this(repeatUnit, range.start, range.last)
|
||||||
|
constructor(repeatUnit: RegexUnit?, range: Pair<Int, Int>) : this(repeatUnit, range.first, range.second)
|
||||||
|
|
||||||
|
private val str = when {
|
||||||
|
from < 0 -> "*"
|
||||||
|
to == 0 -> when (from) {
|
||||||
|
0 -> null
|
||||||
|
1 -> ""
|
||||||
|
else -> "{$from}"
|
||||||
|
}
|
||||||
|
to < 0 -> when (from) {
|
||||||
|
0 -> "*"
|
||||||
|
1 -> "+"
|
||||||
|
else -> "{$from,}"
|
||||||
|
}
|
||||||
|
to == 1 && from == 0 -> "?"
|
||||||
|
to == from -> when (from) {
|
||||||
|
0 -> null
|
||||||
|
1 -> ""
|
||||||
|
else -> "{$from}"
|
||||||
|
}
|
||||||
|
else -> "{$from,$to}"
|
||||||
|
}?.let { range ->
|
||||||
|
repeatUnit?.unit?.let {
|
||||||
|
if (it.isNotEmpty()) "$it$range"
|
||||||
|
else ""
|
||||||
|
}
|
||||||
|
} ?: ""
|
||||||
|
|
||||||
|
override val unit = if (str.isEmpty()) {
|
||||||
|
""
|
||||||
|
} else {
|
||||||
|
"($str)"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString() = str
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package cn.tursom.core.regex
|
||||||
|
|
||||||
|
class StringRegexUnit(private val str: String) : RegexUnit {
|
||||||
|
constructor(str: StringRegexUnit) : this(str.str)
|
||||||
|
|
||||||
|
override val unit = when (str.length) {
|
||||||
|
0 -> null
|
||||||
|
1 -> str
|
||||||
|
else -> "($str)"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString() = str
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package cn.tursom.core.regex
|
||||||
|
|
||||||
|
class UnitListRegexUnit(private val valList: String) : RegexUnit {
|
||||||
|
constructor(from: Char, to: Char) : this("$from-$to")
|
||||||
|
constructor(range: Pair<Char, Char>) : this("${range.first}-${range.second}")
|
||||||
|
constructor(range: CharRange) : this("${range.first}-${range.last}")
|
||||||
|
constructor(range: UnitListRegexUnit) : this(range.valList)
|
||||||
|
|
||||||
|
val reverse
|
||||||
|
get() = UnitListRegexUnit(if (valList.first() == '^') valList.drop(1) else "^$valList")
|
||||||
|
|
||||||
|
operator fun not() = reverse
|
||||||
|
infix operator fun plus(unitList: UnitListRegexUnit) = UnitListRegexUnit("$valList${unitList.valList}")
|
||||||
|
infix operator fun times(unitList: UnitListRegexUnit) = UnitListRegexUnit("$valList${unitList.valList}")
|
||||||
|
infix fun also(unitList: UnitListRegexUnit) = UnitListRegexUnit("$valList${unitList.valList}")
|
||||||
|
infix fun and(unitList: UnitListRegexUnit) = UnitListRegexUnit("$valList${unitList.valList}")
|
||||||
|
infix fun link(unitList: UnitListRegexUnit) = UnitListRegexUnit("$valList${unitList.valList}")
|
||||||
|
|
||||||
|
override fun toString() = "[$valList]"
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package cn.tursom.core.regex
|
package cn.tursom.core.regex
|
||||||
|
|
||||||
class UnitRegexUnit(override val unit: String) : RegexUnit {
|
class UnitRegexUnit(override val unit: String) : RegexUnit {
|
||||||
constructor(unit: UnitRegexUnit) : this(unit.unit)
|
constructor(unit: UnitRegexUnit) : this(unit.unit)
|
||||||
|
|
||||||
override fun toString() = unit
|
override fun toString() = unit
|
||||||
}
|
}
|
@ -24,6 +24,7 @@ class BufferedStorageHandler<T>(
|
|||||||
private val writeHandler: (list: Collection<T>) -> Unit
|
private val writeHandler: (list: Collection<T>) -> Unit
|
||||||
) : StorageHandler<T> {
|
) : StorageHandler<T> {
|
||||||
private val onWrite = AtomicBoolean(false)
|
private val onWrite = AtomicBoolean(false)
|
||||||
|
|
||||||
@Volatile
|
@Volatile
|
||||||
private var msgList = ConcurrentLinkedQueue<T>()
|
private var msgList = ConcurrentLinkedQueue<T>()
|
||||||
|
|
@ -35,10 +35,10 @@ class NonLockTaskQueue : TaskQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class TaskListNode(
|
private class TaskListNode(
|
||||||
override val timeout: Long,
|
override val timeout: Long,
|
||||||
override val task: () -> Unit,
|
override val task: () -> Unit,
|
||||||
override val createTime: Long,
|
override val createTime: Long,
|
||||||
@Volatile var next: TaskListNode?
|
@Volatile var next: TaskListNode?
|
||||||
) : TimerTask {
|
) : TimerTask {
|
||||||
@Volatile
|
@Volatile
|
||||||
override var canceled: Boolean = false
|
override var canceled: Boolean = false
|
@ -40,11 +40,11 @@ class SynchronizedTaskQueue : TaskQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inner class TaskNode(
|
inner class TaskNode(
|
||||||
override val timeout: Long,
|
override val timeout: Long,
|
||||||
override val task: () -> Unit,
|
override val task: () -> Unit,
|
||||||
@Volatile var prev: TaskNode?,
|
@Volatile var prev: TaskNode?,
|
||||||
@Volatile var next: TaskNode?,
|
@Volatile var next: TaskNode?,
|
||||||
override val createTime: Long = CurrentTimeMillisClock.now
|
override val createTime: Long = CurrentTimeMillisClock.now
|
||||||
) : TimerTask {
|
) : TimerTask {
|
||||||
@Volatile
|
@Volatile
|
||||||
override var canceled: Boolean = false
|
override var canceled: Boolean = false
|
11
ts-core/ts-buffer/build.gradle.kts
Normal file
11
ts-core/ts-buffer/build.gradle.kts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
plugins {
|
||||||
|
kotlin("jvm")
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
api(project(":"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@kotlin.Suppress("UNCHECKED_CAST")
|
||||||
|
(rootProject.ext["excludeTest"] as (Project, TaskContainer) -> Unit)(project, tasks)
|
||||||
|
|
@ -0,0 +1,26 @@
|
|||||||
|
package cn.tursom.core
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HOOK java.nio.HeapByteBuffer
|
||||||
|
*/
|
||||||
|
object HeapByteBufferUtil {
|
||||||
|
private val field = ByteBuffer::class.java.getDeclaredField("offset")
|
||||||
|
|
||||||
|
init {
|
||||||
|
field.isAccessible = true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun wrap(array: ByteArray, offset: Int = 0, size: Int = array.size - offset): ByteBuffer {
|
||||||
|
val buffer = ByteBuffer.wrap(array, 0, offset + size)
|
||||||
|
//return if (offset == 0) buffer else {
|
||||||
|
// buffer.position(offset)
|
||||||
|
// buffer.slice()
|
||||||
|
//}
|
||||||
|
if (offset > 0) field.set(buffer, offset)
|
||||||
|
return buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
fun wrap(string: String) = wrap(string.toByteArray())
|
||||||
|
}
|
@ -64,8 +64,10 @@ class Snowflake(
|
|||||||
}, updateRateMs, updateRateMs, TimeUnit.MICROSECONDS)
|
}, updateRateMs, updateRateMs, TimeUnit.MICROSECONDS)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toString() = "Snowflake(workerId=${nodeId
|
override fun toString() = "Snowflake(workerId=${
|
||||||
}, timestamp=0x${timestamp.toHexString(false)
|
nodeId
|
||||||
|
}, timestamp=0x${
|
||||||
|
timestamp.toHexString(false)
|
||||||
}, seed=0x${seed.get().toHexString(false)})"
|
}, seed=0x${seed.get().toHexString(false)})"
|
||||||
|
|
||||||
enum class WorkMode {
|
enum class WorkMode {
|
@ -5,5 +5,10 @@ class ClosedBufferException : Exception {
|
|||||||
constructor(message: String?) : super(message)
|
constructor(message: String?) : super(message)
|
||||||
constructor(message: String?, cause: Throwable?) : super(message, cause)
|
constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||||
constructor(cause: Throwable?) : super(cause)
|
constructor(cause: Throwable?) : super(cause)
|
||||||
constructor(message: String?, cause: Throwable?, enableSuppression: Boolean, writableStackTrace: Boolean) : super(message, cause, enableSuppression, writableStackTrace)
|
constructor(message: String?, cause: Throwable?, enableSuppression: Boolean, writableStackTrace: Boolean) : super(
|
||||||
|
message,
|
||||||
|
cause,
|
||||||
|
enableSuppression,
|
||||||
|
writableStackTrace
|
||||||
|
)
|
||||||
}
|
}
|
@ -5,10 +5,8 @@ import cn.tursom.core.buffer.impl.ListByteBuffer
|
|||||||
import cn.tursom.core.forEachIndex
|
import cn.tursom.core.forEachIndex
|
||||||
import cn.tursom.core.toBytes
|
import cn.tursom.core.toBytes
|
||||||
import java.io.Closeable
|
import java.io.Closeable
|
||||||
import java.io.IOException
|
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
import kotlin.math.min
|
|
||||||
|
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
interface MultipleByteBuffer : List<ByteBuffer>, Closeable, ByteBuffer {
|
interface MultipleByteBuffer : List<ByteBuffer>, Closeable, ByteBuffer {
|
@ -1,7 +1,6 @@
|
|||||||
package cn.tursom.core.buffer.impl
|
package cn.tursom.core.buffer.impl
|
||||||
|
|
||||||
import cn.tursom.core.buffer.ByteBuffer
|
import cn.tursom.core.buffer.ByteBuffer
|
||||||
import cn.tursom.buffer.MultipleByteBuffer
|
|
||||||
|
|
||||||
class ArrayByteBuffer(
|
class ArrayByteBuffer(
|
||||||
override vararg val buffers: ByteBuffer
|
override vararg val buffers: ByteBuffer
|
@ -1,7 +1,7 @@
|
|||||||
package cn.tursom.core.buffer.impl
|
package cn.tursom.core.buffer.impl
|
||||||
|
|
||||||
import cn.tursom.core.buffer.ByteBuffer
|
|
||||||
import cn.tursom.core.HeapByteBufferUtil
|
import cn.tursom.core.HeapByteBufferUtil
|
||||||
|
import cn.tursom.core.buffer.ByteBuffer
|
||||||
|
|
||||||
class HeapByteBuffer(
|
class HeapByteBuffer(
|
||||||
private var buffer: java.nio.ByteBuffer,
|
private var buffer: java.nio.ByteBuffer,
|
@ -45,6 +45,7 @@ open class ListByteBuffer(val bufferList: List<ByteBuffer>) : MultipleByteBuffer
|
|||||||
updateRead()
|
updateRead()
|
||||||
return readOperator!!.get()
|
return readOperator!!.get()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun put(byte: Byte) {
|
override fun put(byte: Byte) {
|
||||||
TODO("Not yet implemented")
|
TODO("Not yet implemented")
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package cn.tursom.core.buffer.impl
|
package cn.tursom.core.buffer.impl
|
||||||
|
|
||||||
import cn.tursom.core.buffer.ByteBuffer
|
|
||||||
import cn.tursom.buffer.MarkableByteBuffer
|
import cn.tursom.buffer.MarkableByteBuffer
|
||||||
|
import cn.tursom.core.buffer.ByteBuffer
|
||||||
import cn.tursom.core.buffer.ProxyByteBuffer
|
import cn.tursom.core.buffer.ProxyByteBuffer
|
||||||
|
|
||||||
class MarkedByteBuffer(override val agent: ByteBuffer) : ProxyByteBuffer, MarkableByteBuffer, ByteBuffer by agent {
|
class MarkedByteBuffer(override val agent: ByteBuffer) : ProxyByteBuffer, MarkableByteBuffer, ByteBuffer by agent {
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user