├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle.kts ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── secret.gpg.enc ├── settings.gradle.kts └── src ├── commonMain └── kotlin │ └── extra │ └── kotlin │ ├── Utils.kt │ ├── bytes │ ├── Bits.kt │ └── Utils.kt │ ├── collection │ ├── ArrayListQueue.kt │ ├── Extensions.kt │ ├── Queue.kt │ ├── SortedQueue.kt │ ├── SortedSet.kt │ ├── WeakHashMap.kt │ ├── contentEquals.kt │ ├── treeWalkBreadth.kt │ └── treeWalkDepth.kt │ ├── concurrent │ ├── AtomicInt.kt │ ├── AtomicLong.kt │ ├── AtomicReference.kt │ └── Lock.kt │ ├── exception │ ├── ElementAlreadyExistsException.kt │ ├── Exception.ext.kt │ └── ForbiddenException.kt │ ├── native │ └── native.kt │ ├── random │ └── RandomStrings.kt │ ├── string │ ├── Char.ext.kt │ └── isEmail.kt │ ├── system │ └── System.kt │ └── util │ ├── Base64.kt │ └── UUID.kt ├── commonTest └── kotlin │ └── test │ └── extra │ └── kotlin │ ├── UtilsTest.kt │ ├── bytes │ └── BytesTest.kt │ └── collection │ ├── Collections.kt │ ├── TreeWalkTest.kt │ └── WeakHashMap.kt ├── jsMain └── kotlin │ └── extra │ └── kotlin │ ├── JSFix.kt │ ├── collection │ └── WeakHashMap.kt │ ├── concurrent │ ├── AtomicInt.kt │ ├── AtomicLong.kt │ ├── AtomicReference.kt │ └── Lock.kt │ ├── exception │ └── stackTraceString.kt │ ├── native │ └── native.kt │ └── system │ └── System.kt ├── jvmMain └── kotlin │ └── extra │ └── kotlin │ ├── collection │ └── WeakHashMap.kt │ ├── concurrent │ ├── AtomicInt.kt │ ├── AtomicLong.kt │ ├── AtomicReference.kt │ └── Lock.kt │ ├── exception │ └── Exception.ext.kt │ ├── native │ └── native.kt │ └── system │ └── System.kt ├── nativeCommon └── kotlin │ └── extra │ └── kotlin │ ├── collection │ └── WeakHashMap.kt │ ├── concurrent │ ├── AtomicInt.kt │ ├── AtomicLong.kt │ ├── AtomicReference.kt │ └── Lock.kt │ ├── exception │ └── Exception.ext.kt │ ├── native │ ├── native.kt │ └── reroute │ │ └── Rerouted.kt │ └── system │ └── System.kt └── nativeInterop └── cinterop └── nativeMutex.def /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Example user template template 3 | ### Example user template 4 | 5 | # IntelliJ project files 6 | .idea 7 | *.iml 8 | out/ 9 | gen/ 10 | build/ 11 | local.properties 12 | .gradle/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: openjdk11 3 | 4 | os: 5 | - windows 6 | - osx 7 | - linux 8 | 9 | matrix: 10 | allow_failures: 11 | - os: windows 12 | 13 | before_install: 14 | - openssl aes-256-cbc -K $encrypted_3fdf79acf4ff_key -iv $encrypted_3fdf79acf4ff_iv 15 | -in secret.gpg.enc -out secret.gpg 16 | -d 17 | - chmod +x gradlew 18 | 19 | script: ./gradlew check 20 | 21 | before_cache: 22 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 23 | - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ 24 | 25 | cache: 26 | directories: 27 | - "$HOME/.gradle/caches/" 28 | - "$HOME/.gradle/wrapper/" 29 | - "$HOME/.konan/cache/" 30 | 31 | deploy: 32 | provider: script 33 | script: ./gradlew publish 34 | skip_cleanup: true 35 | on: 36 | tags: true 37 | branch: master 38 | 39 | 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kotlin extra library 2 | This library is aimed to become a community powered extension of the Kotlin common standard library. 3 | 4 | ## Installing 5 | 6 | Import the latest version in the `build.gradle` of the modules you need: 7 | 8 | ``` 9 | dependencies { 10 | implementation 'org.kotlinextra:kotlin-extlib-{backend}:{latest_version}' 11 | } 12 | ``` 13 | 14 | If using Gradle Kotlin DSL: 15 | ``` 16 | dependencies { 17 | implementation("org.kotlinextra", "kotlin-extlib-{backend}", "{latest_version}") 18 | } 19 | ``` 20 | 21 | Available backends are: 22 | - [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-common) `common` 23 | - [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-js/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-js) `js` 24 | - [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-jvm/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-jvm) `jvm` 25 | - [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-linuxarm32hfp/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-linuxarm32hfp) `linuxarm32hfp` 26 | - [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-linuxmips32/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-linuxmips32) `linuxmips32` 27 | - [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-linuxmipsel32/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-linuxmipsel32) `linuxmipsel32` 28 | - [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-linuxx64/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-linuxx64) `linuxx64` 29 | - [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-androidnativearm64/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-androidnativearm64) `androidnativearm64` 30 | - [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-mingwx64/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-mingwx64) `mingwx64` 31 | - [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-iosarm64/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-iosarm64) `iosarm64` 32 | - [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-iosx64/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-iosx64) `iosx64` 33 | - [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-macosx64/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.kotlinextra/kotlin-extlib-macosx64) `macosx64` 34 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | import Build_gradle.OS.LINUX 2 | import Build_gradle.OS.WINDOWS 3 | import Build_gradle.OS.MAC 4 | import groovy.util.Node 5 | import org.gradle.internal.os.OperatingSystem 6 | import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension 7 | import org.jetbrains.kotlin.gradle.plugin.KotlinTarget 8 | import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeCompilation 9 | import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget 10 | import org.jetbrains.kotlin.konan.target.KonanTarget 11 | import java.util.Properties 12 | 13 | plugins { 14 | kotlin("multiplatform") version "1.3.21" 15 | id("maven-publish") 16 | id("signing") 17 | } 18 | 19 | repositories { 20 | mavenCentral() 21 | jcenter() 22 | maven(url = "https://dl.bintray.com/kotlin/kotlin-eap") 23 | } 24 | 25 | group = "org.kotlinextra" 26 | version = System.getenv()["TRAVIS_TAG"] ?: "0.1.6" 27 | 28 | val localProp = properties("local.properties") 29 | 30 | kotlin { 31 | 32 | sourceSets.create("nativeCommon") 33 | 34 | jvm { 35 | configure(compilations) { 36 | kotlinOptions.jvmTarget = "1.8" 37 | } 38 | } 39 | js() 40 | // wasm32() <-- keeps giving error 41 | iosArm64() 42 | // iosArm32() 43 | iosX64() 44 | mingwX64() 45 | // mingwX86() 46 | macosX64() 47 | linuxX64() 48 | linuxArm32Hfp() 49 | linuxMips32() 50 | linuxMipsel32() 51 | androidNativeArm64() 52 | metadata() 53 | 54 | configure(nativeTargets) { 55 | compilations("main") { 56 | defaultSourceSet.dependsOn(sourceSets["nativeCommon"]) 57 | outputKinds(DYNAMIC) 58 | cinterops.create("nativeMutex") { 59 | includeDirs(buildDir) 60 | } 61 | } 62 | } 63 | 64 | publish(platformIndependentTargets + androidTargets) onlyOn LINUX 65 | publish(windowsTargets) onlyOn WINDOWS 66 | publish(appleTargets) onlyOn MAC 67 | 68 | sourceSets { 69 | val commonMain by getting { 70 | dependencies { 71 | implementation(kotlin("stdlib-common")) 72 | } 73 | } 74 | val commonTest by getting { 75 | dependencies { 76 | implementation(kotlin("test-common")) 77 | implementation(kotlin("test-annotations-common")) 78 | } 79 | } 80 | val jvmMain by getting { 81 | dependencies { 82 | implementation(kotlin("stdlib-jdk8")) 83 | } 84 | } 85 | val jvmTest by getting { 86 | dependencies { 87 | implementation(kotlin("test")) 88 | implementation(kotlin("test-junit")) 89 | } 90 | } 91 | val jsMain by getting { 92 | dependencies { 93 | implementation(kotlin("stdlib-js")) 94 | } 95 | } 96 | val jsTest by getting { 97 | dependencies { 98 | implementation(kotlin("test-js")) 99 | } 100 | } 101 | } 102 | } 103 | 104 | val javadocJar by tasks.creating(Jar::class) { 105 | archiveClassifier.value("javadoc") 106 | // TODO: instead of a single empty Javadoc JAR, generate real documentation for each module 107 | } 108 | 109 | val sourcesJar by tasks.creating(Jar::class) { 110 | archiveClassifier.value("sources") 111 | } 112 | 113 | var keyId = recoverProperty("signing.keyId") 114 | var gpgPassword = recoverProperty("signing.password") 115 | var gpgFile = recoverProperty("signing.secretKeyRingFile") 116 | ?: file("./secret.gpg").run { if (exists()) absolutePath else null } 117 | var sonatypeUsername = recoverProperty("sonatypeUsername") 118 | var sonatypePassword = recoverProperty("sonatypePassword") 119 | 120 | setupPublishingForMavenCentral( 121 | (group as String).replace('.','-'), 122 | "Kotlin community common multiplatform library", 123 | "Kotlin Extra Library", 124 | "https://github.com/lamba92/kotlin-extlib", 125 | "org.kotlinextra", 126 | "kotlinextra.org", 127 | "github", 128 | "https://github.com/kotlin-extra-library/kotlin-extlib/issues", 129 | listOf( 130 | License( 131 | "Apache License 2.0", 132 | "https://github.com/kotlin-extra-library/kotlin-extlib/blob/master/LICENSE", 133 | "repo" 134 | ) 135 | ), 136 | "https://github.com/kotlin-extra-library/kotlin-extlib", 137 | "scm:git:https://github.com/kotlin-extra-library/kotlin-extlib.git", 138 | "scm:git@github.com:kotlin-extra-library/kotlin-extlib.git", 139 | listOf( 140 | Developer("Lamba92"), 141 | Developer("SOFe"), 142 | Developer("UnknownJoe796") 143 | ), 144 | javadocJar, sourcesJar 145 | ) 146 | 147 | fun recoverProperty(propertyName: String) = System.getenv()[propertyName.toUpperCase().replace('.', '_')] 148 | ?: localProp[propertyName] as String? 149 | ?: extra.getOrNull(propertyName) as String? 150 | 151 | fun setupPublishingForMavenCentral( 152 | repositoryName: String, 153 | description: String, 154 | name: String, 155 | url: String, 156 | organizationName: String, 157 | organizationUrl: String, 158 | issueManagementSystem: String, 159 | issueManagementUrl: String, 160 | licenses: List, 161 | scmUrl: String, 162 | scmConnection: String, 163 | scmDevConnection: String, 164 | developers: List, 165 | javadocJar: Jar, 166 | sourcesJar: Jar, 167 | keyId_: String? = keyId, 168 | gpgPassword_: String? = gpgPassword, 169 | gpgFile_: String? = gpgFile, 170 | sonatypeUsername_: String? = sonatypeUsername, 171 | sonatypePassword_: String? = sonatypePassword 172 | ) { 173 | if (listOf( 174 | keyId_, 175 | gpgPassword_, 176 | gpgFile_, 177 | sonatypeUsername_, 178 | sonatypePassword_ 179 | ).none { it == null } && file(gpgFile_!!).exists() 180 | ) { 181 | val deployUrl = "https://oss.sonatype.org/" + if ((version as String).contains("snapshot", true)) 182 | "content/repositories/snapshots" 183 | else 184 | "service/local/staging/deploy/maven2" 185 | 186 | println("Publishing setup detected. Setting up publishing for\n$deployUrl") 187 | 188 | extra["signing.keyId"] = keyId_ 189 | extra["signing.password"] = gpgPassword_ 190 | extra["signing.secretKeyRingFile"] = gpgFile_ 191 | 192 | publishing { 193 | publications { 194 | configure(withType()) { 195 | signing.sign(this) 196 | pom.customizeForMavenCentral( 197 | description, name, url, 198 | organizationName, organizationUrl, 199 | issueManagementSystem, issueManagementUrl, 200 | licenses, scmUrl, scmConnection, 201 | scmDevConnection, developers 202 | ) 203 | artifact(javadocJar) 204 | } 205 | withType()["kotlinMultiplatform"].artifact(sourcesJar) 206 | } 207 | repositories { 208 | maven(deployUrl).credentials { 209 | username = sonatypeUsername_ 210 | password = sonatypePassword_ 211 | } 212 | } 213 | } 214 | } else println(buildString { 215 | appendln("Not enough information to publish:") 216 | appendln("keyId: ${if (keyId_ == null) "NOT " else ""}found") 217 | appendln("gpgPassword: ${if (gpgPassword_ == null) "NOT " else ""}found") 218 | appendln("gpgFile: ${gpgFile_ ?: "NOT found"}") 219 | appendln("gpgFile presence: ${gpgFile_?.let { file(it).exists() } ?: "false"}") 220 | appendln("sonatypeUsername: ${if (sonatypeUsername_ == null) "NOT " else ""}found") 221 | appendln("sonatypePassword: ${if (sonatypePassword_ == null) "NOT " else ""}found") 222 | }) 223 | } 224 | 225 | data class License(val name: String, val url: String, val distribution: String) 226 | data class Developer(val name: String, val email: String? = null) 227 | 228 | fun org.gradle.api.publish.maven.MavenPom.customizeForMavenCentral( 229 | description: String, 230 | name: String, 231 | url: String, 232 | organizationName: String, 233 | organizationUrl: String, 234 | issueManagementSystem: String, 235 | issueManagementUrl: String, 236 | licenses: List, 237 | scmUrl: String, 238 | scmConnection: String, 239 | scmDevConnection: String, 240 | developers: List 241 | ) = buildAsNode { 242 | add("description", description) 243 | add("name", name) 244 | add("url", url) 245 | node("organization") { 246 | add("name", organizationName) 247 | add("url", organizationUrl) 248 | } 249 | node("issueManagement") { 250 | add("system", issueManagementSystem) 251 | add("url", issueManagementUrl) 252 | } 253 | node("licenses") { 254 | licenses.forEach { 255 | node("license") { 256 | add("name", it.name) 257 | add("url", it.url) 258 | add("distribution", it.distribution) 259 | } 260 | } 261 | } 262 | node("scm") { 263 | add("url", scmUrl) 264 | add("connection", scmConnection) 265 | add("developerConnection", scmDevConnection) 266 | } 267 | node("developers") { 268 | developers.forEach { 269 | node("developer") { 270 | add("name", it.name) 271 | if (it.email != null) 272 | add("email", it.email) 273 | } 274 | } 275 | } 276 | } 277 | 278 | fun KotlinNativeTarget.compilations(name: String, config: KotlinNativeCompilation.() -> Unit) = 279 | compilations[name].apply(config) 280 | 281 | fun properties(file: File) = Properties().apply { load(file.apply { if (!exists()) createNewFile() }.inputStream()) } 282 | 283 | fun properties(fileSrc: String) = properties(file(fileSrc)) 284 | 285 | val KotlinMultiplatformExtension.nativeTargets 286 | get() = targets.filter { it is KotlinNativeTarget }.map { it as KotlinNativeTarget } 287 | 288 | val KotlinMultiplatformExtension.platformIndependentTargets 289 | get() = targets.filter { it !is KotlinNativeTarget || it.konanTarget == KonanTarget.WASM32 } 290 | 291 | val KotlinMultiplatformExtension.appleTargets 292 | get() = targets.filter { 293 | it is KotlinNativeTarget && listOf( 294 | KonanTarget.IOS_ARM64, 295 | KonanTarget.IOS_X64, 296 | KonanTarget.MACOS_X64 297 | // KonanTarget.IOS_ARM32 298 | ).any { target -> it.konanTarget == target } 299 | } 300 | 301 | val KotlinMultiplatformExtension.windowsTargets 302 | get() = targets.filter { 303 | it is KotlinNativeTarget && listOf( 304 | KonanTarget.MINGW_X64 305 | // KonanTarget.MINGW_X86 306 | ).any { target -> it.konanTarget == target } 307 | } 308 | 309 | val KotlinMultiplatformExtension.linuxTargets 310 | get() = targets.filter { 311 | it is KotlinNativeTarget && listOf( 312 | KonanTarget.LINUX_ARM32_HFP, 313 | KonanTarget.LINUX_MIPS32, 314 | KonanTarget.LINUX_MIPSEL32, 315 | KonanTarget.LINUX_X64 316 | ).any { target -> it.konanTarget == target } 317 | } 318 | 319 | val KotlinMultiplatformExtension.androidTargets 320 | get() = targets.filter { 321 | it is KotlinNativeTarget && listOf( 322 | KonanTarget.ANDROID_ARM32, 323 | KonanTarget.ANDROID_ARM64 324 | ).any { target -> it.konanTarget == target } 325 | } 326 | 327 | fun Node.add(key: String, value: String) = appendNode(key).setValue(value) 328 | 329 | fun Node.node(key: String, content: Node.() -> Unit) = appendNode(key).also(content) 330 | 331 | fun org.gradle.api.publish.maven.MavenPom.buildAsNode(builder: Node.() -> Unit) = withXml { asNode().apply(builder) } 332 | 333 | fun ExtraPropertiesExtension.getOrNull(name: String) = if (has(name)) get(name) else null 334 | 335 | fun KotlinMultiplatformExtension.publish(targets: Iterable) = targets 336 | 337 | infix fun Iterable.onlyOn(os: OS) = configure(this) { 338 | mavenPublication { 339 | tasks.withType().all { 340 | onlyIf { 341 | publication != this@mavenPublication || when (os) { 342 | LINUX -> OperatingSystem.current().isLinux 343 | MAC -> OperatingSystem.current().isMacOsX 344 | WINDOWS -> OperatingSystem.current().isWindows 345 | } 346 | } 347 | } 348 | } 349 | } 350 | 351 | enum class OS { 352 | LINUX, MAC, WINDOWS 353 | } 354 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | org.gradle.parallel=true 3 | kotlin.parallel.tasks.in.project=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kotlin-extra-library/kotlin-extlib/c461714c39a0cf16e7bb46f8d90f8ce86b53687e/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /secret.gpg.enc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kotlin-extra-library/kotlin-extlib/c461714c39a0cf16e7bb46f8d90f8ce86b53687e/secret.gpg.enc -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | mavenCentral() 4 | maven(url="https://plugins.gradle.org/m2/") 5 | maven(url="https://dl.bintray.com/kotlin/kotlin-eap") 6 | google() 7 | } 8 | 9 | resolutionStrategy { 10 | eachPlugin { 11 | when(requested.id.id){ 12 | "kotlin-multiplatform"-> useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}") 13 | "com.android.library" -> useModule("com.android.tools.build:gradle:${requested.version}") 14 | } 15 | } 16 | } 17 | } 18 | rootProject.name = "kotlin-extlib" 19 | enableFeaturePreview("GRADLE_METADATA") 20 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/Utils.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin 2 | 3 | import kotlin.js.JsName 4 | import kotlin.jvm.JvmName 5 | 6 | @Deprecated("Package moved", ReplaceWith("this.getBytesLE()", "extra.kotlin.bytes.getBytesLE")) 7 | fun Long.getBytesLE() = ByteArray(8).apply { 8 | var buff = this@getBytesLE 9 | repeat(size) { 10 | this[it] = buff.toByte() 11 | buff = buff shr 8 12 | } 13 | } 14 | 15 | @Deprecated("Package moved", ReplaceWith("this.getBytesLE()", "extra.kotlin.bytes.getBytesLE")) 16 | fun Double.getBytesLE() 17 | = toRawBits().getBytesLE() 18 | 19 | @Deprecated("Package moved", ReplaceWith("this.upperNibble", "extra.kotlin.bytes.upperNibble")) 20 | val Byte.upperNibble get() = (this.toInt() shr 4 and 0b1111).toByte() 21 | 22 | @Deprecated("Package moved", ReplaceWith("this.lowerNibble", "extra.kotlin.bytes.lowerNibble")) 23 | val Byte.lowerNibble get() = (this.toInt() and 0b1111).toByte() 24 | 25 | @Deprecated("Package moved", ReplaceWith("this.writeBytesLE()", "extra.kotlin.bytes.writeBytesLE")) 26 | fun ByteArray.writeBytesLE(value: Long, offset : Int = 0) : Int { 27 | if (this.size - offset >= 8) 28 | throw IndexOutOfBoundsException("The remaining space is less then 8") 29 | var buff = value 30 | 31 | repeat(8){ 32 | this[it+offset] = buff.toByte() 33 | buff = buff shr 8 34 | } 35 | 36 | return offset + 8 37 | } 38 | 39 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/bytes/Bits.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("NOTHING_TO_INLINE") 2 | 3 | package extra.kotlin.bytes 4 | 5 | inline fun Int.bitLow(index: Int): Boolean = ushr(index).and(0x1) == 1 6 | inline fun Int.bitLowOn(index: Int) = this or 0x1.shl(index) 7 | inline fun Int.bitLowOff(index: Int) = and(1.shl(index).inv()) 8 | inline fun Int.bitLowFlip(index: Int) = xor(1.shl(index)) 9 | inline fun Int.bitLowSet(index: Int, value: Boolean) = if(value) bitLowOn(index) else bitLowOff(index) 10 | inline fun Int.bitsLow(): BooleanIterator = object : BooleanIterator() { 11 | var index = 0 12 | override fun hasNext(): Boolean = index < 32 13 | override fun nextBoolean(): Boolean = bitLow(index++) 14 | } 15 | 16 | inline fun Int.bitHigh(index: Int): Boolean = ushr(31-index).and(0x1) == 1 17 | inline fun Int.bitHighOn(index: Int) = this or 0x1.shl(31-index) 18 | inline fun Int.bitHighOff(index: Int) = and(1.shl(31-index).inv()) 19 | inline fun Int.bitHighFlip(index: Int) = xor(1.shl(31-index)) 20 | inline fun Int.bitHighSet(index: Int, value: Boolean) = if(value) bitHighOn(index) else bitHighOff(index) 21 | inline fun Int.bitsHigh(): BooleanIterator = object : BooleanIterator() { 22 | var index = 0 23 | override fun hasNext(): Boolean = index < 32 24 | override fun nextBoolean(): Boolean = bitHigh(index++) 25 | } 26 | 27 | inline fun Long.bitLow(index: Int): Boolean = ushr(index).and(0x1) == 1L 28 | inline fun Long.bitLowOn(index: Int) = this or 0x1L.shl(index) 29 | inline fun Long.bitLowOff(index: Int) = and(1L.shl(index).inv()) 30 | inline fun Long.bitLowFlip(index: Int) = xor(1L.shl(index)) 31 | inline fun Long.bitLowSet(index: Int, value: Boolean) = if(value) bitLowOn(index) else bitLowOff(index) 32 | inline fun Long.bitsLow(): BooleanIterator = object : BooleanIterator() { 33 | var index = 0 34 | override fun hasNext(): Boolean = index < 64 35 | override fun nextBoolean(): Boolean = bitLow(index++) 36 | } 37 | 38 | inline fun Long.bitHigh(index: Int): Boolean = ushr(63-index).and(0x1) == 1L 39 | inline fun Long.bitHighOn(index: Int) = this or 0x1L.shl(63-index) 40 | inline fun Long.bitHighOff(index: Int) = and(1L.shl(63-index).inv()) 41 | inline fun Long.bitHighFlip(index: Int) = xor(1L.shl(63-index)) 42 | inline fun Long.bitHighSet(index: Int, value: Boolean) = if(value) bitHighOn(index) else bitHighOff(index) 43 | inline fun Long.bitsHigh(): BooleanIterator = object : BooleanIterator() { 44 | var index = 0 45 | override fun hasNext(): Boolean = index < 64 46 | override fun nextBoolean(): Boolean = bitHigh(index++) 47 | } 48 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/bytes/Utils.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.bytes 2 | 3 | import kotlin.js.JsName 4 | import kotlin.jvm.JvmName 5 | 6 | fun Long.getBytesLE() = ByteArray(8).apply { 7 | var buff = this@getBytesLE 8 | repeat(size) { 9 | this[it] = buff.toByte() 10 | buff = buff shr 8 11 | } 12 | } 13 | 14 | fun Double.getBytesLE() 15 | = toRawBits().getBytesLE() 16 | 17 | val Byte.upperNibble get() = (this.toInt() shr 4 and 0b1111).toByte() 18 | val Byte.lowerNibble get() = (this.toInt() and 0b1111).toByte() 19 | @ExperimentalUnsignedTypes 20 | @get:JvmName("upperNibbleUnsigned") 21 | @get:JsName("upperNibbleUnsigned") 22 | val UByte.upperNibble get() = (this.toInt() shr 4 and 0b1111).toUByte() 23 | @ExperimentalUnsignedTypes 24 | @get:JvmName("lowerNibbleUnsigned") 25 | @get:JsName("lowerNibbleUnsigned") 26 | val UByte.lowerNibble get() = (this.toInt() and 0b1111).toUByte() 27 | 28 | fun ByteArray.writeBytesLE(value: Long, offset : Int = 0) : Int { 29 | if (this.size - offset >= 8) 30 | throw IndexOutOfBoundsException("The remaining space is less then 8") 31 | var buff = value 32 | 33 | repeat(8){ 34 | this[it+offset] = buff.toByte() 35 | buff = buff shr 8 36 | } 37 | 38 | return offset + 8 39 | } 40 | 41 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/collection/ArrayListQueue.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.collection 2 | 3 | /** 4 | * A simple FIFO queue backed by an [ArrayList] 5 | */ 6 | open class ArrayListQueue(): Queue, AbstractMutableCollection() { 7 | 8 | protected val data = ArrayList() 9 | 10 | constructor(collection: Collection): this(){ 11 | collection.addTo(data) 12 | } 13 | 14 | // AbstractCollection methods 15 | override val size: Int 16 | get() = data.size 17 | 18 | override fun add(element: E) = data.add(element) 19 | 20 | override fun iterator() = data.iterator() 21 | 22 | // Queue interface method 23 | override fun offer(e: E) { add(e) } 24 | 25 | override fun poll() 26 | = if(isEmpty()) null else element().also { remove(it) } 27 | 28 | override fun element() = data.first() 29 | 30 | override fun peek() 31 | = if(isEmpty()) null else element() 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/collection/Extensions.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.collection 2 | 3 | /** 4 | * Adds [this] collection to the [target] collection. 5 | */ 6 | fun Collection.addTo(target: MutableCollection) = target.addAll(this) 7 | 8 | /** 9 | * Performs the given [action] on each element. 10 | */ 11 | inline fun Array.forEachWhile(check: () -> Boolean, action: (T) -> Unit) { 12 | for (element in this) { 13 | if(check()) 14 | action(element) 15 | else 16 | break 17 | } 18 | } 19 | 20 | inline fun Array.forEachUntil(check: () -> Boolean, action: (T) -> Unit) 21 | = forEachWhile(check.negate(), action) 22 | 23 | @Suppress("NOTHING_TO_INLINE") 24 | inline fun (() -> Boolean).negate() = {!this()} -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/collection/Queue.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.collection 2 | 3 | interface Queue: MutableCollection { 4 | 5 | /** 6 | * Inserts the specified element into this queue if it is possible to do 7 | * so immediately without violating capacity restrictions. 8 | * When using a capacity-restricted queue, this method is generally 9 | * preferable to [add], which can fail to insert an element only 10 | * by throwing an exception. 11 | * 12 | * @param e the element to add 13 | * @return `true` if the element was added to this queue, else 14 | * `false` 15 | * @throws ClassCastException if the class of the specified element 16 | * prevents it from being added to this queue 17 | * @throws NullPointerException if the specified element is null and 18 | * this queue does not permit null elements 19 | * @throws IllegalArgumentException if some property of this element 20 | * prevents it from being added to this queue 21 | */ 22 | fun offer(e: E) 23 | 24 | /** 25 | * Retrieves and removes the head of this queue, 26 | * or returns `null` if this queue is empty. 27 | * 28 | * @return the head of this queue, or `null` if this queue is empty 29 | */ 30 | fun poll(): E? 31 | 32 | /** 33 | * Retrieves, but does not remove, the head of this queue. This method 34 | * differs from [peek] only in that it throws an exception 35 | * if this queue is empty. 36 | * 37 | * @return the head of this queue 38 | * @throws NoSuchElementException if this queue is empty 39 | */ 40 | fun element(): E 41 | 42 | /** 43 | * Retrieves, but does not remove, the head of this queue, 44 | * or returns `null` if this queue is empty. 45 | * 46 | * @return the head of this queue, or `null` if this queue is empty 47 | */ 48 | fun peek(): E? 49 | } -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/collection/SortedQueue.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.collection 2 | 3 | import kotlin.math.absoluteValue 4 | 5 | /** 6 | * A simple FIFO sorted queue backed by an [ArrayList]. Insertions are ordered, 7 | * the index in which make the insertion is searched using [binarySearch] 8 | * 9 | * @param comparator The comparator used to sort this queue. 10 | * @param collection An initial collection from which initialize the queue. 11 | */ 12 | class SortedQueue( 13 | val comparator: Comparator, 14 | collection: Collection = emptyList() 15 | ) : ArrayListQueue(collection) { 16 | 17 | init { 18 | if(data.isNotEmpty()) 19 | data.sortWith(comparator) 20 | } 21 | 22 | override fun add(element: E): Boolean { 23 | val index = data.binarySearch(element, comparator) 24 | when { 25 | index >= 0 -> data.add(index, element) 26 | index < 0 -> data.add(index.absoluteValue - 1, element) 27 | } 28 | return true 29 | } 30 | } -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/collection/SortedSet.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.collection 2 | 3 | import kotlin.math.abs 4 | 5 | /** 6 | * A Sorted [Set] implemented using a sorted [ArrayList]. The insertion is ordered using [binarySearch] 7 | */ 8 | class SortedSet( 9 | val comparator: Comparator, 10 | collection: Collection = emptyList() 11 | ) : AbstractMutableSet() { 12 | 13 | private val data = ArrayList().apply { 14 | addAll(collection.toSet()) 15 | sortWith(comparator) 16 | } 17 | 18 | override val size: Int 19 | get() = data.size 20 | 21 | override fun add(element: E): Boolean { 22 | val index = data.binarySearch(element, comparator) 23 | return if (index >= 0) 24 | false 25 | else { 26 | data.add(abs(index) - 1, element) 27 | true 28 | } 29 | } 30 | 31 | override fun iterator() = data.iterator() 32 | } -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/collection/WeakHashMap.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.collection 2 | 3 | expect class WeakHashMap() : MutableMap 4 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/collection/contentEquals.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.collection 2 | 3 | 4 | infix fun Iterable<*>.contentEquals(other: Iterable<*>): Boolean{ 5 | val otherIter = other.iterator() 6 | for(item in this){ 7 | if(!otherIter.hasNext()) return false 8 | if(item != otherIter.next()) return false 9 | } 10 | return true 11 | } 12 | infix fun Collection<*>.contentEquals(other: Collection<*>): Boolean{ 13 | if(this.size != other.size) return false 14 | return (this as Iterable<*>).contentEquals(other) 15 | } 16 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/collection/treeWalkBreadth.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.collection 2 | 3 | /** 4 | * Creates a recursive sequence, breadth first. 5 | */ 6 | fun Sequence.treeWalkBreadthSequence(getter: (T) -> Sequence) = object : Sequence { 7 | override fun iterator(): Iterator = TreeWalkBreadthIterator(this@treeWalkBreadthSequence.iterator()) { getter(it).iterator() } 8 | } 9 | 10 | /** 11 | * Creates a recursive sequence, breadth first. 12 | */ 13 | fun Iterable.treeWalkBreadthSequence(getter: (T) -> Sequence): Sequence = 14 | this.asSequence().treeWalkBreadthSequence(getter) 15 | 16 | /** 17 | * Creates a recursive iterator, breadth first. 18 | */ 19 | class TreeWalkBreadthIterator(val start: Iterator, val getter: (T) -> Iterator) : Iterator { 20 | 21 | val toCheck = ArrayList() 22 | var current: Iterator = start 23 | 24 | override fun hasNext(): Boolean = current.hasNext() 25 | override fun next(): T { 26 | val item = current.next() 27 | 28 | toCheck.add(item) 29 | 30 | while (true) { 31 | if (current.hasNext()) { 32 | break 33 | } else if (toCheck.isNotEmpty()) { 34 | val newCheck = toCheck.removeAt(0) 35 | current = getter(newCheck) 36 | } else { 37 | break 38 | } 39 | } 40 | 41 | return item 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/collection/treeWalkDepth.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.collection 2 | 3 | /** 4 | * Creates a recursive sequence, depth first. 5 | */ 6 | fun Sequence.treeWalkDepthSequence(getter: (T) -> Sequence) = object : Sequence { 7 | override fun iterator(): Iterator = TreeWalkDepthIterator(this@treeWalkDepthSequence.iterator()) { getter(it).iterator() } 8 | } 9 | 10 | /** 11 | * Creates a recursive sequence, depth first. 12 | */ 13 | fun Iterable.treeWalkDepthSequence(getter: (T) -> Sequence): Sequence = 14 | this.asSequence().treeWalkDepthSequence(getter) 15 | 16 | /** 17 | * Creates a recursive iterator, depth first. 18 | */ 19 | class TreeWalkDepthIterator(val start: Iterator, val getter: (T) -> Iterator) : Iterator { 20 | 21 | val stack = arrayListOf>(start) 22 | val current get() = stack.lastOrNull() 23 | 24 | override fun hasNext(): Boolean = current != null 25 | override fun next(): T { 26 | 27 | if(current == null) throw NoSuchElementException() 28 | while(current?.hasNext() == false){ 29 | stack.removeAt(stack.lastIndex) 30 | } 31 | if(current == null) throw NoSuchElementException() 32 | 33 | val item = current!!.next() 34 | stack.add(getter(item)) 35 | 36 | while(current?.hasNext() == false){ 37 | stack.removeAt(stack.lastIndex) 38 | } 39 | 40 | return item 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/concurrent/AtomicInt.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | expect class AtomicInt(value_: Int) { 4 | var value: Int 5 | fun addAndGet(delta: Int): Int 6 | fun compareAndSet(expected: Int, new: Int): Boolean 7 | fun increment() 8 | fun decrement() 9 | } 10 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/concurrent/AtomicLong.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | expect class AtomicLong(value_: Long) { 4 | var value: Long 5 | fun addAndGet(delta: Long): Long 6 | fun compareAndSet(expected: Long, new: Long): Boolean 7 | fun increment() 8 | fun decrement() 9 | } 10 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/concurrent/AtomicReference.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | expect class AtomicReference(value_: T) { 4 | var value: T 5 | fun compareAndSet(expected: T, new: T): Boolean 6 | } 7 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/concurrent/Lock.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | //TODO: This would better belong in 'extra.kotlin.concurrent' 4 | 5 | expect class Lock() { 6 | fun lock() 7 | fun unlock() 8 | } 9 | 10 | inline fun Lock.use(block: () -> R): R { 11 | try { 12 | lock() 13 | return block() 14 | } finally { 15 | unlock() 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/exception/ElementAlreadyExistsException.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.exception 2 | 3 | /** 4 | * Used to indicate that that element already exists, and replacing it is refused. 5 | */ 6 | class ElementAlreadyExistsException(message: String): Exception(message) 7 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/exception/Exception.ext.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.exception 2 | 3 | /** 4 | * Returns the stack trace of the throwable as a string as best it can 5 | */ 6 | expect fun Throwable.stackTraceString(): String 7 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/exception/ForbiddenException.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.exception 2 | 3 | /** 4 | * Used to indicate that the user does not have permission to do this action. 5 | */ 6 | class ForbiddenException(message: String? = null): Exception(message) 7 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/native/native.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.native 2 | 3 | expect fun T.freeze(): T 4 | expect fun Any.ensureNeverFrozen() 5 | expect val Any.isFrozen: Boolean 6 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/random/RandomStrings.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.random 2 | 3 | import extra.kotlin.util.UUID 4 | import kotlin.random.Random 5 | 6 | /** 7 | * Generates a UUIDv4. 8 | * @param useUppercase Uses uppercase letters instead. 9 | * @return The randomly generated UUID string. 10 | */ 11 | @Deprecated("Use UUIDs directly instead.", replaceWith = ReplaceWith("UUID.uuid4().toString()", "extra.kotlin.util.UUID")) 12 | fun Random.uuid(useUppercase: Boolean = false) = UUID.uuid4().toString().let{ 13 | if(useUppercase) it.toUpperCase() 14 | else it 15 | } 16 | 17 | /** 18 | * A random string containing symbols, letters and numbers. Default length is random between 15 and 20 included. 19 | * @param length The desired lenght of the string. 20 | * @return The randomly generated string. 21 | */ 22 | fun Random.nextString(length: Int = nextInt(15, 21)) = buildString { 23 | for(i in 0 until length){ 24 | append(nextChar()) 25 | } 26 | } 27 | 28 | /** 29 | * A random string containing letters and numbers. Default length is random between 15 and 20 included. 30 | * @param length The desired lenght of the string. 31 | * @return The randomly generated string. 32 | */ 33 | fun Random.nextAlphanumericString(length: Int = nextInt(15, 21)) = buildString { 34 | for(i in 0 until length){ 35 | append(when (nextInt(1, 4)){ 36 | 1 -> nextNumericChar() 37 | 2 -> nextLowercaseLetter() 38 | else -> nextUppercaseLetter() 39 | }) 40 | } 41 | } 42 | 43 | /** 44 | * A random character from `a` to `f` included or a number. 45 | * @param useUppercase Use uppercase letters instead. 46 | * @return The randomly generated character. 47 | */ 48 | fun Random.nextHexChar(useUppercase: Boolean = false) = when (nextInt(1, 3)){ 49 | 1 -> nextNumericChar() 50 | else -> if(useUppercase) nextUppercaseLetter(0, 6) else nextLowercaseLetter(0, 6) 51 | } 52 | 53 | /** 54 | * A random character between symbols, letters and numbers. 55 | * @return The randomly generated character. 56 | */ 57 | fun Random.nextChar() = nextInt(33, 127).toChar() 58 | 59 | /** 60 | * A random character between `0` and '9' included. 61 | * @param from Starting letter index. Default is 0. 62 | * @param to Last letter index. Default is 9. 63 | * @return The randomly generated character. 64 | */ 65 | fun Random.nextNumericChar(from: Int = 0, to: Int = 9): Char { 66 | if(from < 0 || from > 8 || to < 2 || to > 9) 67 | throw IllegalArgumentException("Invalid parameters. Select Ranges from 0 included to 9 included") 68 | return nextInt(48 + from, 58 - (9 - to)).toChar() 69 | } 70 | 71 | /** 72 | * A random character between `a` and 'z' included. 73 | * @param from Starting letter index. Default is 0. 74 | * @param to Last letter index. default is 26. 75 | * @return The randomly generated character. 76 | */ 77 | fun Random.nextLowercaseLetter(from: Int = 0, to: Int = 26): Char{ 78 | if(from < 0 || from > 25 || to < 1 || to > 26) 79 | throw IllegalArgumentException("Invalid parameters. Select Ranges from 0 included to 26 included") 80 | return nextInt(97 + from, 123 - (26 - to)).toChar() 81 | } 82 | 83 | /** 84 | * A random character between `A` and 'Z' included. 85 | * @param from Starting letter index. Default is 0. 86 | * @param to Last letter index. default is 26. 87 | * @return The randomly generated character. 88 | */ 89 | fun Random.nextUppercaseLetter(from: Int = 0, to: Int = 26): Char { 90 | if(from < 0 || from > 26 || to < 1 || to > 26) 91 | throw IllegalArgumentException("Invalid parameters. Select Ranges from 0 included to 26 included") 92 | return nextInt(65 + from, 91 - (26 - to)).toChar() 93 | } 94 | 95 | /** 96 | * A random character between `A` to 'Z' included and 'a' to 'z' included. 97 | * @return The randomly generated character. 98 | */ 99 | fun Random.nextLetter() = when(nextInt(2)){ 100 | 0 -> nextLowercaseLetter() 101 | else -> nextUppercaseLetter() 102 | } 103 | 104 | 105 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/string/Char.ext.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.string 2 | 3 | @Suppress("NOTHING_TO_INLINE") inline fun Char.isDigit() = this in '0'..'9' 4 | @Suppress("NOTHING_TO_INLINE") inline fun Char.isLowercase() = this in 'a' .. 'z' 5 | @Suppress("NOTHING_TO_INLINE") inline fun Char.isUppercase() = this in 'A' .. 'Z' 6 | @Suppress("NOTHING_TO_INLINE") inline fun Char.isLetter() = isLowercase() || isUppercase() 7 | @Suppress("NOTHING_TO_INLINE") inline fun Char.isControl() = this < ' ' 8 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/string/isEmail.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.string 2 | 3 | /** 4 | * Checks if the string is an email according to the spectacularly long regex from RFC 5322: 5 | * `(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])` 6 | * Not guaranteed to be perfectly accurate, but works in all normal cases. 7 | */ 8 | fun String.isEmail(): Boolean { 9 | return matches("(?:[a-z0-9!#\$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#\$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])".toRegex(RegexOption.IGNORE_CASE)) 10 | } 11 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/system/System.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.system 2 | 3 | //TODO: Should probably leave this to the time-specific libraries. 4 | 5 | expect fun getCurrentTimeInMillis(): Long 6 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/util/Base64.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.util 2 | 3 | // Author https://kotlinlang.org/docs/tutorials/multiplatform-library.html 4 | 5 | //TODO: Move to encoding package? Also add URL compliant Base64 variant 6 | object Base64 { 7 | 8 | private val BASE64_ALPHABET: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 9 | private val BASE64_MASK: Byte = 0x3f 10 | private val BASE64_PAD: Char = '=' 11 | private val BASE64_INVERSE_ALPHABET = IntArray(256) { 12 | BASE64_ALPHABET.indexOf(it.toChar()) 13 | } 14 | 15 | private fun Int.toBase64(): Char 16 | = BASE64_ALPHABET[this] 17 | 18 | private fun ByteArray.getOrZero(index: Int): Int 19 | = if (index >= size) 0 else get(index).toInt() 20 | 21 | fun encode(src: ByteArray): ByteArray { 22 | 23 | // 4n / 3 is expected Base64 payload 24 | val result = ArrayList(4 * src.size / 3) 25 | var index = 0 26 | while (index < src.size) { 27 | val symbolsLeft = src.size - index 28 | val padSize = if (symbolsLeft >= 3) 0 else (3 - symbolsLeft) * 8 / 6 29 | val chunk = (src.getOrZero(index) shl 16) or (src.getOrZero(index + 1) shl 8) or src.getOrZero(index + 2) 30 | index += 3 31 | 32 | for (i in 3 downTo padSize) { 33 | val char = (chunk shr (6 * i)) and BASE64_MASK.toInt() 34 | result.add(char.toBase64().toByte()) 35 | } 36 | // Fill the pad with '=' 37 | repeat(padSize) { result.add(BASE64_PAD.toByte()) } 38 | } 39 | 40 | return result.toByteArray() 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/extra/kotlin/util/UUID.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.util 2 | 3 | import kotlin.random.Random 4 | 5 | data class UUID( 6 | val mostSignificantBits: Long, 7 | val leastSignificantBits: Long 8 | ) : Comparable { 9 | 10 | override fun compareTo(other: UUID): Int { 11 | return if (this.mostSignificantBits == other.mostSignificantBits) 12 | this.leastSignificantBits.compareTo(other.leastSignificantBits) 13 | else this.mostSignificantBits.compareTo(other.mostSignificantBits) 14 | } 15 | 16 | companion object { 17 | fun fromUUIDString(input: String): UUID { 18 | val hexOnly = input.filter { it in '0'..'9' || it in 'a'..'f' || it in 'A'..'F' } 19 | if(hexOnly.length != 32) throw IllegalArgumentException("String is not a Id, should be 32 hex characters") 20 | return UUID( 21 | mostSignificantBits = hexOnly.substring(0, 16).toULong(16).toLong(), 22 | leastSignificantBits = hexOnly.substring(16, 32).toULong(16).toLong() 23 | ) 24 | } 25 | fun uuid4(): UUID = UUID( 26 | mostSignificantBits = (Random.nextLong() and (-1L - 0x000000000000F000) or (0x4000)), 27 | leastSignificantBits = (Random.nextLong() and 0x3FFFFFFFFFFFFFFFL or (0x8000_0000L shl 32)) 28 | ) 29 | } 30 | 31 | override fun toString(): String { 32 | 33 | val first = mostSignificantBits.toULong().toString(16).padStart(16, '0') 34 | val second = leastSignificantBits.toULong().toString(16).padStart(16, '0') 35 | return buildString(36) { 36 | append(first.substring(0, 8)) 37 | append("-") 38 | append(first.substring(8, 12)) 39 | append("-") 40 | append(first.substring(12, 16)) 41 | append("-") 42 | append(second.substring(0, 4)) 43 | append("-") 44 | append(second.substring(4, 16)) 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/commonTest/kotlin/test/extra/kotlin/UtilsTest.kt: -------------------------------------------------------------------------------- 1 | package test.extra.kotlin 2 | 3 | import extra.kotlin.bytes.lowerNibble 4 | import extra.kotlin.getBytesLE 5 | import extra.kotlin.lowerNibble 6 | import extra.kotlin.upperNibble 7 | import kotlin.test.Test 8 | import kotlin.test.assertEquals 9 | import kotlin.test.assertTrue 10 | 11 | class UtilsTest { 12 | @Test 13 | fun testLongGetBytesLE() { 14 | assertTrue(ByteArray(8) { 0 }.contentEquals((0L).getBytesLE())) 15 | assertTrue(ByteArray(8) { 0xFF.toByte() }.contentEquals((-1L).getBytesLE())) 16 | assertTrue( 17 | byteArrayOf( 18 | 0x01, 0x23, 0x45, 0x67, 19 | 0x89.toByte(), 0xab.toByte(), 0xcd.toByte(), 0xef.toByte() 20 | ).contentEquals((Long.MIN_VALUE + 0x6fcdab8967452301L).getBytesLE()) 21 | ) 22 | } 23 | 24 | // TODO test Double.getBytesLE() 25 | 26 | @Test 27 | fun testByteUpperNibble(){ 28 | assertEquals(0x00, 0x00.toByte().upperNibble) 29 | assertEquals(0x00, 0x0F.toByte().upperNibble) 30 | assertEquals(0x0F, 0xF0.toByte().upperNibble) 31 | assertEquals(0x0F, 0xFF.toByte().upperNibble) 32 | } 33 | 34 | @ExperimentalUnsignedTypes 35 | @Test 36 | fun testByteLowerNibble(){ 37 | assertEquals(0x00.toUByte(), 0x00.toUByte().lowerNibble) 38 | assertEquals(0x0F.toUByte(), 0x0F.toUByte().lowerNibble) 39 | assertEquals(0x00.toUByte(), 0xF0.toUByte().lowerNibble) 40 | assertEquals(0x0F.toUByte(), 0xFF.toUByte().lowerNibble) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/commonTest/kotlin/test/extra/kotlin/bytes/BytesTest.kt: -------------------------------------------------------------------------------- 1 | package test.extra.kotlin.bytes 2 | 3 | import extra.kotlin.bytes.* 4 | import kotlin.test.Test 5 | import kotlin.test.assertEquals 6 | import kotlin.test.assertTrue 7 | 8 | class ByteTest { 9 | 10 | @Test 11 | fun testLowBits() { 12 | //Sometimes people use integers to efficiently store a set of booleans. 13 | //You can use IntBitArray for this! 14 | var flags = 0x0 15 | flags = flags.bitLowSet(0, true) //Index is number of bits from the right-most bit 16 | flags = flags.bitLowSet(1, false) 17 | flags = flags.bitLowSet(2, true) 18 | 19 | //Using binary, we can determine that the integer value is 5. Let's see if it works. 20 | assertEquals(5, flags) 21 | 22 | //Let's look at an existing set of complicated flags. 23 | flags = 0x34721823 //In binary: 0000 0000 ... 0011 0100 0111 0010 0001 1000 0010 0011 24 | 25 | //Let's print out each bit - note this will be reversed of the binary representation. 26 | println(flags.bitsLow().asSequence().joinToString("") { if (it) "1" else "0" }) 27 | 28 | //Looks like #5 should be on. 29 | assertEquals(true, flags.bitLow(5)) 30 | 31 | //Looks like #2 should be off. 32 | assertEquals(false, flags.bitLow(2)) 33 | 34 | //Let's turn #2 on. 35 | flags = flags.bitLowOn(2) 36 | //The value should be ... 1100 0010 0011 37 | assertEquals(0x34721827, flags) 38 | } 39 | 40 | @Test 41 | fun testHighBits() { 42 | var bits = 0 43 | for(index in 0 .. 31) { 44 | bits = bits.bitHighOn(index) 45 | } 46 | assertEquals(-1, bits) 47 | for(index in 0 .. 31) { 48 | bits = bits.bitHighOff(index) 49 | } 50 | assertEquals(0, bits) 51 | for(index in 0 .. 31) { 52 | bits = bits.bitHighSet(index, true) 53 | } 54 | assertEquals(-1, bits) 55 | } 56 | 57 | @Test 58 | fun testHighBitsLong() { 59 | var bits = 0L 60 | for(index in 0 .. 63) { 61 | bits = bits.bitHighOn(index) 62 | } 63 | assertEquals(-1L, bits) 64 | for(index in 0 .. 63) { 65 | bits = bits.bitHighOff(index) 66 | } 67 | assertEquals(0L, bits) 68 | for(index in 0 .. 63) { 69 | bits = bits.bitHighSet(index, true) 70 | } 71 | assertEquals(-1L, bits) 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/commonTest/kotlin/test/extra/kotlin/collection/Collections.kt: -------------------------------------------------------------------------------- 1 | package test.extra.kotlin.collection 2 | 3 | import extra.kotlin.collection.ArrayListQueue 4 | import kotlin.test.Test 5 | import kotlin.test.assertTrue 6 | 7 | class Collections { 8 | 9 | @Test 10 | fun testArrayListQueue(){ 11 | val q = ArrayListQueue(listOf(1, 3, 5, 7)) 12 | val output = ArrayList() 13 | while (q.isNotEmpty()){ 14 | output.add(q.poll()!!) 15 | } 16 | assertTrue(output.containsAll(listOf(1, 3, 5, 7))) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/commonTest/kotlin/test/extra/kotlin/collection/TreeWalkTest.kt: -------------------------------------------------------------------------------- 1 | package test.extra.kotlin.collection 2 | 3 | import extra.kotlin.collection.contentEquals 4 | import extra.kotlin.collection.treeWalkBreadthSequence 5 | import extra.kotlin.collection.treeWalkDepthSequence 6 | import kotlin.test.Test 7 | import kotlin.test.assertTrue 8 | 9 | class TreeWalkTest{ 10 | 11 | 12 | @Test 13 | fun treeBreadthWalk(){ 14 | val subject = listOf( 15 | 1, 16 | 2, 17 | listOf( 18 | 4, 19 | listOf( 20 | 7, 21 | 8, 22 | 9 23 | ), 24 | 5, 25 | 6 26 | ), 27 | 3 28 | ) 29 | val list = subject 30 | .treeWalkBreadthSequence { if(it is Iterable<*>) it.asSequence() else sequenceOf() } 31 | .mapNotNull { it as? Int } 32 | .toList() 33 | println(list.joinToString { it.toString() }) 34 | assertTrue { list contentEquals (1 .. 9) } 35 | } 36 | 37 | @Test 38 | fun treeDepthWalk(){ 39 | val subject = listOf( 40 | 1, 41 | 2, 42 | listOf( 43 | 3, 44 | 4, 45 | listOf( 46 | 5, 47 | 6, 48 | 7 49 | ), 50 | 8 51 | ), 52 | 9 53 | ) 54 | val list = subject 55 | .treeWalkDepthSequence { if(it is Iterable<*>) it.asSequence() else sequenceOf() } 56 | .mapNotNull { it as? Int } 57 | .toList() 58 | println(list.joinToString { it.toString() }) 59 | assertTrue { list contentEquals (1 .. 9) } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/commonTest/kotlin/test/extra/kotlin/collection/WeakHashMap.kt: -------------------------------------------------------------------------------- 1 | package test.extra.kotlin.collection 2 | 3 | import extra.kotlin.collection.WeakHashMap 4 | import kotlin.test.Test 5 | import kotlin.test.assertTrue 6 | 7 | class WeakHashMapTest { 8 | data class AdvancedKey(val a: Int = 0) 9 | 10 | @Test 11 | fun worksAsNormalMap() { 12 | val keyA = AdvancedKey(0) 13 | val keyB = AdvancedKey(1) 14 | val map = WeakHashMap() 15 | map[keyA] = 0 16 | assertTrue("a-0 for $map") { map[keyA] == 0 } 17 | map[keyA] = 1 18 | assertTrue("a-1 for $map") { map[keyA] == 1 } 19 | assertTrue("b-null for $map") { map[keyB] == null } 20 | map[keyB] = 8 21 | assertTrue("b-8 for $map") { map[keyB] == 8 } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/jsMain/kotlin/extra/kotlin/JSFix.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("NOTHING_TO_INLINE") 2 | 3 | package extra.kotlin 4 | 5 | import org.khronos.webgl.Int8Array 6 | 7 | @Suppress("CAST_NEVER_SUCCEEDS") 8 | inline fun Int8Array.asByteArray(): ByteArray = this as ByteArray 9 | 10 | @Suppress("CAST_NEVER_SUCCEEDS") 11 | inline fun ByteArray.asInt8Array(): Int8Array = this as Int8Array 12 | -------------------------------------------------------------------------------- /src/jsMain/kotlin/extra/kotlin/collection/WeakHashMap.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.collection 2 | 3 | var WeakHashMap_Ids:Int = 0 4 | actual class WeakHashMap : MutableMap{ 5 | 6 | fun genId() = "weakmap-${WeakHashMap_Ids++}" 7 | var id = genId() 8 | 9 | override val size: Int 10 | get() = Int.MAX_VALUE 11 | 12 | override fun containsKey(key: K): Boolean { 13 | return (key.asDynamic()[id] as? Any).takeIf { it != undefined } != null 14 | } 15 | 16 | override fun containsValue(value: V): Boolean = false 17 | 18 | override fun get(key: K): V? { 19 | val existing = (key.asDynamic()[id] as? Any).takeIf { it != undefined } as? V 20 | return existing 21 | } 22 | 23 | override fun isEmpty(): Boolean = false 24 | 25 | override val entries: MutableSet> = mutableSetOf() 26 | override val keys: MutableSet = mutableSetOf() 27 | override val values: MutableCollection = mutableListOf() 28 | 29 | override fun clear() { 30 | id = genId() 31 | } 32 | 33 | override fun put(key: K, value: V): V? { 34 | val existing = (key.asDynamic()[id] as? Any).takeIf { it != undefined } as? V 35 | key.asDynamic()[id] = value 36 | return existing 37 | } 38 | 39 | override fun putAll(from: Map) { 40 | for((entry, value) in from){ 41 | put(entry, value) 42 | } 43 | } 44 | 45 | override fun remove(key: K): V? { 46 | val existing = (key.asDynamic()[id] as? Any).takeIf { it != undefined } as? V 47 | key.asDynamic()[id] = null 48 | return existing 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/jsMain/kotlin/extra/kotlin/concurrent/AtomicInt.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | 4 | actual class AtomicInt actual constructor(value_: Int) { 5 | 6 | actual var value: Int = value_ 7 | 8 | actual fun compareAndSet(expected: Int, new: Int): Boolean { 9 | if (expected == value) { 10 | value = new 11 | return true 12 | } 13 | return false 14 | } 15 | 16 | actual fun addAndGet(delta: Int): Int { 17 | value += delta 18 | return value 19 | } 20 | 21 | actual fun increment() { addAndGet(1) } 22 | actual fun decrement() { addAndGet(-1) } 23 | } 24 | -------------------------------------------------------------------------------- /src/jsMain/kotlin/extra/kotlin/concurrent/AtomicLong.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | 4 | actual class AtomicLong actual constructor(value_: Long) { 5 | 6 | actual var value: Long = value_ 7 | 8 | actual fun compareAndSet(expected: Long, new: Long): Boolean { 9 | if (expected == value) { 10 | value = new 11 | return true 12 | } 13 | return false 14 | } 15 | 16 | actual fun addAndGet(delta: Long): Long { 17 | value += delta 18 | return value 19 | } 20 | actual fun increment() { addAndGet(1) } 21 | actual fun decrement() { addAndGet(-1) } 22 | } 23 | -------------------------------------------------------------------------------- /src/jsMain/kotlin/extra/kotlin/concurrent/AtomicReference.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | 4 | actual class AtomicReference actual constructor(value_: T) { 5 | 6 | actual var value: T = value_ 7 | 8 | actual fun compareAndSet(expected: T, new: T): Boolean { 9 | if (expected == value) { 10 | value = new 11 | return true 12 | } 13 | return false 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/jsMain/kotlin/extra/kotlin/concurrent/Lock.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | actual class Lock actual constructor() { 4 | actual fun lock() {} 5 | 6 | actual fun unlock() {} 7 | } 8 | -------------------------------------------------------------------------------- /src/jsMain/kotlin/extra/kotlin/exception/stackTraceString.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.exception 2 | 3 | /** 4 | * Returns the stack trace of the throwable as a string as best it can 5 | */ 6 | actual fun Throwable.stackTraceString(): String { 7 | return generateSequence(this) { it.cause }.joinToString("\n") { 8 | it.toString() + ": " + it.message 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/jsMain/kotlin/extra/kotlin/native/native.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.native 2 | 3 | actual fun T.freeze(): T = this 4 | actual fun Any.ensureNeverFrozen() = Unit 5 | actual val Any.isFrozen: Boolean get() = false 6 | -------------------------------------------------------------------------------- /src/jsMain/kotlin/extra/kotlin/system/System.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.system 2 | 3 | import kotlin.js.Date 4 | 5 | actual fun getCurrentTimeInMillis() = Date().getTime().toLong() -------------------------------------------------------------------------------- /src/jvmMain/kotlin/extra/kotlin/collection/WeakHashMap.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.collection 2 | 3 | actual typealias WeakHashMap = java.util.WeakHashMap 4 | -------------------------------------------------------------------------------- /src/jvmMain/kotlin/extra/kotlin/concurrent/AtomicInt.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | 4 | actual class AtomicInt actual constructor(value_: Int) { 5 | val backing = java.util.concurrent.atomic.AtomicInteger(value_) 6 | 7 | actual var value: Int 8 | get() = backing.get() 9 | set(value) { 10 | backing.set(value) 11 | } 12 | 13 | actual fun compareAndSet(expected: Int, new: Int): Boolean = backing.compareAndSet(expected, new) 14 | actual fun addAndGet(delta: Int): Int = backing.addAndGet(delta) 15 | actual fun increment() { backing.incrementAndGet() } 16 | actual fun decrement() { backing.decrementAndGet() } 17 | } 18 | -------------------------------------------------------------------------------- /src/jvmMain/kotlin/extra/kotlin/concurrent/AtomicLong.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | 4 | actual class AtomicLong actual constructor(value_: Long) { 5 | val backing = java.util.concurrent.atomic.AtomicLong(value_) 6 | 7 | actual var value: Long 8 | get() = backing.get() 9 | set(value) { 10 | backing.set(value) 11 | } 12 | 13 | actual fun compareAndSet(expected: Long, new: Long): Boolean = backing.compareAndSet(expected, new) 14 | actual fun addAndGet(delta: Long): Long = backing.addAndGet(delta) 15 | actual fun increment() { backing.incrementAndGet() } 16 | actual fun decrement() { backing.decrementAndGet() } 17 | } 18 | -------------------------------------------------------------------------------- /src/jvmMain/kotlin/extra/kotlin/concurrent/AtomicReference.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | 4 | actual class AtomicReference actual constructor(value_: T) { 5 | val backing = java.util.concurrent.atomic.AtomicReference(value_) 6 | 7 | actual var value: T 8 | get() = backing.get() 9 | set(value) { 10 | backing.set(value) 11 | } 12 | 13 | actual fun compareAndSet(expected: T, new: T): Boolean = backing.compareAndSet(expected, new) 14 | } 15 | -------------------------------------------------------------------------------- /src/jvmMain/kotlin/extra/kotlin/concurrent/Lock.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | import java.util.concurrent.locks.ReentrantLock 4 | 5 | actual class Lock { 6 | private val mutex = ReentrantLock() 7 | 8 | actual fun lock() { 9 | mutex.lock() 10 | } 11 | actual fun unlock() { 12 | mutex.unlock() 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/jvmMain/kotlin/extra/kotlin/exception/Exception.ext.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.exception 2 | 3 | import java.io.PrintWriter 4 | import java.io.StringWriter 5 | 6 | actual fun Throwable.stackTraceString(): String { 7 | val underlying = StringWriter() 8 | val writer = PrintWriter(underlying) 9 | writer.println("${javaClass.simpleName}: $message") 10 | printStackTrace(writer) 11 | return underlying.toString() 12 | } 13 | -------------------------------------------------------------------------------- /src/jvmMain/kotlin/extra/kotlin/native/native.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.native 2 | 3 | actual fun T.freeze(): T = this 4 | actual fun Any.ensureNeverFrozen() = Unit 5 | actual val Any.isFrozen: Boolean get() = false 6 | -------------------------------------------------------------------------------- /src/jvmMain/kotlin/extra/kotlin/system/System.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.system 2 | 3 | actual fun getCurrentTimeInMillis() = System.currentTimeMillis() -------------------------------------------------------------------------------- /src/nativeCommon/kotlin/extra/kotlin/collection/WeakHashMap.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.collection 2 | 3 | import kotlin.native.ref.WeakReference 4 | 5 | actual class WeakHashMap actual constructor() : MutableMap { 6 | 7 | val underlying = HashMap, V>() 8 | 9 | class UnderlyingKey(val weakReference: WeakReference) { 10 | constructor(key: K) : this(WeakReference(key)) 11 | 12 | override fun hashCode(): Int = weakReference.get().hashCode() 13 | override fun equals(other: Any?): Boolean { 14 | return other is UnderlyingKey<*> && other.weakReference.get() == this.weakReference.get() 15 | } 16 | } 17 | 18 | override val size: Int 19 | get() { 20 | privateClean() 21 | return Int.MAX_VALUE 22 | } 23 | 24 | override fun containsKey(key: K): Boolean = underlying.containsKey(UnderlyingKey(key)) 25 | 26 | override fun containsValue(value: V): Boolean = underlying.containsValue(value) 27 | 28 | override fun get(key: K): V? = underlying[UnderlyingKey(key)] 29 | 30 | override fun isEmpty(): Boolean = underlying.isEmpty() 31 | 32 | override val entries: MutableSet> = mutableSetOf() 33 | override val keys: MutableSet = mutableSetOf() 34 | override val values: MutableCollection get() = underlying.values 35 | 36 | override fun clear() = underlying.clear() 37 | 38 | override fun put(key: K, value: V): V? { 39 | val ukey = UnderlyingKey(key) 40 | val old = underlying[ukey] 41 | underlying[ukey] = value 42 | return old 43 | } 44 | 45 | override fun putAll(from: Map) { 46 | for ((entry, value) in from) { 47 | put(entry, value) 48 | } 49 | } 50 | 51 | override fun remove(key: K): V? { 52 | val ukey = UnderlyingKey(key) 53 | return underlying.remove(ukey) 54 | } 55 | 56 | private fun privateClean() { 57 | val iterator = underlying.iterator() 58 | while (iterator.hasNext()) { 59 | val it = iterator.next() 60 | if (it.key.weakReference.get() == null) 61 | iterator.remove() 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/nativeCommon/kotlin/extra/kotlin/concurrent/AtomicInt.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | actual typealias AtomicInt = kotlin.native.concurrent.AtomicInt 4 | -------------------------------------------------------------------------------- /src/nativeCommon/kotlin/extra/kotlin/concurrent/AtomicLong.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | actual typealias AtomicLong = kotlin.native.concurrent.AtomicLong 4 | -------------------------------------------------------------------------------- /src/nativeCommon/kotlin/extra/kotlin/concurrent/AtomicReference.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | actual typealias AtomicReference = kotlin.native.concurrent.AtomicReference 4 | -------------------------------------------------------------------------------- /src/nativeCommon/kotlin/extra/kotlin/concurrent/Lock.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.concurrent 2 | 3 | import kotlinx.cinterop.cValue 4 | 5 | actual class Lock { 6 | private val mutex = cValue() 7 | 8 | init { 9 | kextra_mutex_create(mutex) 10 | } 11 | 12 | actual fun lock() { 13 | kextra_mutex_lock(mutex) 14 | } 15 | 16 | actual fun unlock() { 17 | kextra_mutex_unlock(mutex) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/nativeCommon/kotlin/extra/kotlin/exception/Exception.ext.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.exception 2 | 3 | actual fun Throwable.stackTraceString(): String { 4 | return this.getStackTrace().joinToString("\n") 5 | } 6 | -------------------------------------------------------------------------------- /src/nativeCommon/kotlin/extra/kotlin/native/native.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.native 2 | 3 | import extra.kotlin.native.reroute.rerouteEnsureNeverFrozen 4 | import extra.kotlin.native.reroute.rerouteFreeze 5 | import extra.kotlin.native.reroute.rerouteFrozen 6 | 7 | actual fun T.freeze(): T = rerouteFreeze() 8 | actual fun Any.ensureNeverFrozen() = rerouteEnsureNeverFrozen() 9 | actual val Any.isFrozen: Boolean get() = rerouteFrozen() 10 | -------------------------------------------------------------------------------- /src/nativeCommon/kotlin/extra/kotlin/native/reroute/Rerouted.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.native.reroute 2 | 3 | import kotlin.native.concurrent.ensureNeverFrozen 4 | import kotlin.native.concurrent.freeze 5 | import kotlin.native.concurrent.isFrozen 6 | 7 | fun Any.rerouteFrozen(): Boolean = this.isFrozen 8 | fun T.rerouteFreeze(): T = this.freeze() 9 | fun Any.rerouteEnsureNeverFrozen(): Unit = this.ensureNeverFrozen() 10 | -------------------------------------------------------------------------------- /src/nativeCommon/kotlin/extra/kotlin/system/System.kt: -------------------------------------------------------------------------------- 1 | package extra.kotlin.system 2 | 3 | import kotlin.system.getTimeMillis 4 | 5 | actual fun getCurrentTimeInMillis(): Long = getTimeMillis() -------------------------------------------------------------------------------- /src/nativeInterop/cinterop/nativeMutex.def: -------------------------------------------------------------------------------- 1 | package = extra.kotlin.concurrent 2 | --- 3 | #include 4 | 5 | typedef struct kextra_mutex { 6 | pthread_mutex_t* mutex; 7 | } kextra_mutex_t; 8 | 9 | 10 | void kextra_mutex_create(kextra_mutex_t* mutex) { 11 | pthread_mutex_init(mutex->mutex, NULL); 12 | } 13 | 14 | void kextra_mutex_lock(kextra_mutex_t* mutex) { 15 | pthread_mutex_lock(mutex->mutex); 16 | } 17 | 18 | void kextra_mutex_unlock(kextra_mutex_t* mutex) { 19 | pthread_mutex_unlock(mutex->mutex); 20 | } 21 | 22 | void kextra_mutex_destroy(kextra_mutex_t* mutex) { 23 | pthread_mutex_destroy(mutex->mutex); 24 | } 25 | --------------------------------------------------------------------------------