Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ByteString implementation #148

Merged
merged 32 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d6c533f
Implement basic ByteString API
fzhinkin Jun 20, 2023
41b1962
Support ByteString in kotlinx-io
fzhinkin Jun 21, 2023
40f59ed
Cleanup
fzhinkin Jun 21, 2023
2d81743
Update API dump
fzhinkin Jun 21, 2023
d99ff1c
Added tests on ByteStringBuilder
fzhinkin Jun 22, 2023
6d3bd05
Improve test coverage
fzhinkin Jun 22, 2023
e09f188
Removed debugging code
fzhinkin Jun 22, 2023
09e8676
Change ByteString::toString to always return full string representation
fzhinkin Jun 23, 2023
9c8d009
Restructure unsafe API, move utf-8 conversion to base module
fzhinkin Jun 23, 2023
4f31909
Hide ByteString.EMPTY
fzhinkin Jun 23, 2023
5a1b06f
Cleanup
fzhinkin Jun 23, 2023
9eff7c8
Added module description
fzhinkin Jun 23, 2023
7d16db1
Move Unsafe Api annotation to unsafe package
fzhinkin Jun 23, 2023
9519b83
Implement contentEquals
fzhinkin Jun 23, 2023
c05ac7a
Cleanup
fzhinkin Jun 23, 2023
18620a3
Ended the sentence
fzhinkin Jun 23, 2023
045cbe1
Bump up dependencies version
fzhinkin Jun 26, 2023
f2d820a
Add isNotEmpty extension
fzhinkin Jun 26, 2023
09af0ac
Add buildByteString functions
fzhinkin Jun 26, 2023
fc853c7
Add append(vararg Byte) extension to the builder
fzhinkin Jun 26, 2023
74fcebe
Update API dump
fzhinkin Jun 26, 2023
e2f6d76
Restructure project layout
fzhinkin Jun 26, 2023
d83827c
Minor API changes
fzhinkin Jun 27, 2023
1ffce7a
Enable JS target in bytestring module
fzhinkin Jun 27, 2023
a21ec1f
Fixed formatting for byte-string related tests
fzhinkin Jun 28, 2023
5e0fb14
Updated tests, docs and made the code BCE-friendly
fzhinkin Jun 28, 2023
7207c08
Rename string conversion routines
fzhinkin Jun 28, 2023
b3612bd
Increase timeout of JS tests
fzhinkin Jun 28, 2023
9006255
Cleaned up the code, updated docs
fzhinkin Jun 28, 2023
2c1a785
Updated API dump
fzhinkin Jun 28, 2023
61332f6
Updated exception messages
fzhinkin Jun 29, 2023
457692a
Renamed readUtf8/writeUtf8 to readString/writeString (#156)
fzhinkin Jun 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions benchmarks/src/commonMain/kotlin/BufferOps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ open class Utf8StringBenchmark : BufferRWBenchmarkBase() {
@Benchmark
fun benchmark(): String {
val s = buffer.size
buffer.writeUtf8(string)
return buffer.readUtf8(buffer.size - s)
buffer.writeString(string)
return buffer.readString(buffer.size - s)
}
}

Expand Down Expand Up @@ -257,16 +257,16 @@ open class Utf8LineBenchmarkBase : BufferRWBenchmarkBase() {
open class Utf8LineBenchmark : Utf8LineBenchmarkBase() {
@Benchmark
fun benchmark(): String? {
buffer.writeUtf8(string)
return buffer.readUtf8Line()
buffer.writeString(string)
return buffer.readLine()
}
}

open class Utf8LineStrictBenchmark : Utf8LineBenchmarkBase() {
@Benchmark
fun benchmark(): String {
buffer.writeUtf8(string)
return buffer.readUtf8LineStrict()
buffer.writeString(string)
return buffer.readLineStrict()
}
}

Expand Down
3 changes: 3 additions & 0 deletions bytestring/Module.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Module kotlinx-io-bytestring

The module provides the [ByteString] - an immutable sequence of bytes, and extensions facilitating work with it.
84 changes: 84 additions & 0 deletions bytestring/api/kotlinx-io-bytestring.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
public final class kotlinx/io/bytestring/ByteString : java/lang/Comparable {
public static final field Companion Lkotlinx/io/bytestring/ByteString$Companion;
public fun <init> ([BII)V
public synthetic fun <init> ([BIIILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> ([BLjava/lang/Object;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun compareTo (Ljava/lang/Object;)I
public fun compareTo (Lkotlinx/io/bytestring/ByteString;)I
public final fun copyInto ([BIII)V
public static synthetic fun copyInto$default (Lkotlinx/io/bytestring/ByteString;[BIIIILjava/lang/Object;)V
public fun equals (Ljava/lang/Object;)Z
public final fun get (I)B
public final fun getBackingArrayReference ()[B
public final fun getSize ()I
public fun hashCode ()I
public final fun substring (II)Lkotlinx/io/bytestring/ByteString;
public static synthetic fun substring$default (Lkotlinx/io/bytestring/ByteString;IIILjava/lang/Object;)Lkotlinx/io/bytestring/ByteString;
public final fun toByteArray (II)[B
public static synthetic fun toByteArray$default (Lkotlinx/io/bytestring/ByteString;IIILjava/lang/Object;)[B
public fun toString ()Ljava/lang/String;
}

public final class kotlinx/io/bytestring/ByteString$Companion {
}

public final class kotlinx/io/bytestring/ByteStringBuilder {
public fun <init> ()V
public fun <init> (I)V
public synthetic fun <init> (IILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun append (B)V
public final fun append ([BII)V
public static synthetic fun append$default (Lkotlinx/io/bytestring/ByteStringBuilder;[BIIILjava/lang/Object;)V
public final fun getCapacity ()I
public final fun getSize ()I
public final fun toByteString ()Lkotlinx/io/bytestring/ByteString;
}

public final class kotlinx/io/bytestring/ByteStringBuilderKt {
public static final fun append (Lkotlinx/io/bytestring/ByteStringBuilder;Lkotlinx/io/bytestring/ByteString;)V
public static final fun append (Lkotlinx/io/bytestring/ByteStringBuilder;[B)V
public static final fun append-EK-6454 (Lkotlinx/io/bytestring/ByteStringBuilder;B)V
public static final fun buildByteString (ILkotlin/jvm/functions/Function1;)Lkotlinx/io/bytestring/ByteString;
public static synthetic fun buildByteString$default (ILkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/io/bytestring/ByteString;
}

public final class kotlinx/io/bytestring/ByteStringJvmExtKt {
public static final fun decodeToString (Lkotlinx/io/bytestring/ByteString;Ljava/nio/charset/Charset;)Ljava/lang/String;
public static final fun encodeToByteString (Ljava/lang/String;Ljava/nio/charset/Charset;)Lkotlinx/io/bytestring/ByteString;
}

public final class kotlinx/io/bytestring/ByteStringKt {
public static final fun ByteString ([B)Lkotlinx/io/bytestring/ByteString;
public static final fun contentEquals (Lkotlinx/io/bytestring/ByteString;[B)Z
public static final fun decodeToString (Lkotlinx/io/bytestring/ByteString;)Ljava/lang/String;
public static final fun encodeToByteString (Ljava/lang/String;)Lkotlinx/io/bytestring/ByteString;
public static final fun endsWith (Lkotlinx/io/bytestring/ByteString;Lkotlinx/io/bytestring/ByteString;)Z
public static final fun endsWith (Lkotlinx/io/bytestring/ByteString;[B)Z
public static final fun getIndices (Lkotlinx/io/bytestring/ByteString;)Lkotlin/ranges/IntRange;
public static final fun indexOf (Lkotlinx/io/bytestring/ByteString;BI)I
public static final fun indexOf (Lkotlinx/io/bytestring/ByteString;Lkotlinx/io/bytestring/ByteString;I)I
public static final fun indexOf (Lkotlinx/io/bytestring/ByteString;[BI)I
public static synthetic fun indexOf$default (Lkotlinx/io/bytestring/ByteString;BIILjava/lang/Object;)I
public static synthetic fun indexOf$default (Lkotlinx/io/bytestring/ByteString;Lkotlinx/io/bytestring/ByteString;IILjava/lang/Object;)I
public static synthetic fun indexOf$default (Lkotlinx/io/bytestring/ByteString;[BIILjava/lang/Object;)I
public static final fun isEmpty (Lkotlinx/io/bytestring/ByteString;)Z
public static final fun isNotEmpty (Lkotlinx/io/bytestring/ByteString;)Z
public static final fun lastIndexOf (Lkotlinx/io/bytestring/ByteString;BI)I
public static final fun lastIndexOf (Lkotlinx/io/bytestring/ByteString;Lkotlinx/io/bytestring/ByteString;I)I
public static final fun lastIndexOf (Lkotlinx/io/bytestring/ByteString;[BI)I
public static synthetic fun lastIndexOf$default (Lkotlinx/io/bytestring/ByteString;BIILjava/lang/Object;)I
public static synthetic fun lastIndexOf$default (Lkotlinx/io/bytestring/ByteString;Lkotlinx/io/bytestring/ByteString;IILjava/lang/Object;)I
public static synthetic fun lastIndexOf$default (Lkotlinx/io/bytestring/ByteString;[BIILjava/lang/Object;)I
public static final fun startsWith (Lkotlinx/io/bytestring/ByteString;Lkotlinx/io/bytestring/ByteString;)Z
public static final fun startsWith (Lkotlinx/io/bytestring/ByteString;[B)Z
}

public abstract interface annotation class kotlinx/io/bytestring/unsafe/UnsafeByteStringApi : java/lang/annotation/Annotation {
}

public final class kotlinx/io/bytestring/unsafe/UnsafeByteStringOperations {
public static final field INSTANCE Lkotlinx/io/bytestring/unsafe/UnsafeByteStringOperations;
public final fun withByteArrayUnsafe (Lkotlinx/io/bytestring/ByteString;Lkotlin/jvm/functions/Function1;)V
public final fun wrapUnsafe ([B)Lkotlinx/io/bytestring/ByteString;
}

80 changes: 80 additions & 0 deletions bytestring/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import org.jetbrains.dokka.gradle.DokkaTaskPartial
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet

plugins {
kotlin("multiplatform")
id("org.jetbrains.kotlinx.kover") version "0.7.1"
id("org.jetbrains.dokka") version "1.8.20"
}

kotlin {
jvm {
withJava()
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}

js(IR) {
nodejs {
testTask {
useMocha {
timeout = "30s"
}
}
}
browser {
testTask {
filter.setExcludePatterns("*SmokeFileTest*")
useMocha {
timeout = "30s"
}
}
}
}

configureNativePlatforms()
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val jvmMain by getting
val jvmTest by getting

createSourceSet("nativeMain", parent = commonMain, children = nativeTargets)
createSourceSet("nativeTest", parent = commonTest, children = nativeTargets)
}

explicitApi()
sourceSets.configureEach {
configureSourceSet()
}
}

fun KotlinSourceSet.configureSourceSet() {
val srcDir = if (name.endsWith("Main")) "src" else "test"
val platform = name.dropLast(4)
kotlin.srcDir("$platform/$srcDir")
if (name == "jvmMain") {
resources.srcDir("$platform/resources")
} else if (name == "jvmTest") {
resources.srcDir("$platform/test-resources")
}
languageSettings {
progressiveMode = true
}
}

tasks.withType<DokkaTaskPartial>().configureEach {
dokkaSourceSets.configureEach {
includes.from("Module.md")

perPackageOption {
suppress.set(true)
matchingRegex.set(".*unsafe.*")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's worth excluding it from the official documentation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, it looks like a Forbidden fruit - we're asking not to use it by any means and then making it visible to everyone.

}
}
}
Loading