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

Implement ByteString factory accepting UBytes #390

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions bytestring/api/kotlinx-io-bytestring.api
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public final class kotlinx/io/bytestring/ByteStringJvmExtKt {
}

public final class kotlinx/io/bytestring/ByteStringKt {
public static final fun ByteString ()Lkotlinx/io/bytestring/ByteString;
public static final fun ByteString ([B)Lkotlinx/io/bytestring/ByteString;
public static final fun ByteString-GBYM_sE ([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;
Expand Down
2 changes: 2 additions & 0 deletions bytestring/api/kotlinx-io-bytestring.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,7 @@ final fun (kotlinx.io.bytestring/ByteStringBuilder).kotlinx.io.bytestring/append
final fun (kotlinx.io.bytestring/ByteStringBuilder).kotlinx.io.bytestring/append(kotlin/UByte) // kotlinx.io.bytestring/append|append@kotlinx.io.bytestring.ByteStringBuilder(kotlin.UByte){}[0]
final fun (kotlinx.io.bytestring/ByteStringBuilder).kotlinx.io.bytestring/append(kotlinx.io.bytestring/ByteString) // kotlinx.io.bytestring/append|append@kotlinx.io.bytestring.ByteStringBuilder(kotlinx.io.bytestring.ByteString){}[0]
final fun <#A: kotlin.text/Appendable> (kotlin.io.encoding/Base64).kotlinx.io.bytestring/encodeToAppendable(kotlinx.io.bytestring/ByteString, #A, kotlin/Int = ..., kotlin/Int = ...): #A // kotlinx.io.bytestring/encodeToAppendable|encodeToAppendable@kotlin.io.encoding.Base64(kotlinx.io.bytestring.ByteString;0:0;kotlin.Int;kotlin.Int){0§<kotlin.text.Appendable>}[0]
final fun kotlinx.io.bytestring/ByteString(): kotlinx.io.bytestring/ByteString // kotlinx.io.bytestring/ByteString|ByteString(){}[0]
final fun kotlinx.io.bytestring/ByteString(kotlin/ByteArray...): kotlinx.io.bytestring/ByteString // kotlinx.io.bytestring/ByteString|ByteString(kotlin.ByteArray...){}[0]
final fun kotlinx.io.bytestring/ByteString(kotlin/UByteArray...): kotlinx.io.bytestring/ByteString // kotlinx.io.bytestring/ByteString|ByteString(kotlin.UByteArray...){}[0]
final inline fun kotlinx.io.bytestring/buildByteString(kotlin/Int = ..., kotlin/Function1<kotlinx.io.bytestring/ByteStringBuilder, kotlin/Unit>): kotlinx.io.bytestring/ByteString // kotlinx.io.bytestring/buildByteString|buildByteString(kotlin.Int;kotlin.Function1<kotlinx.io.bytestring.ByteStringBuilder,kotlin.Unit>){}[0]
21 changes: 21 additions & 0 deletions bytestring/common/src/ByteString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

package kotlinx.io.bytestring

import kotlin.js.JsName
import kotlin.math.max
import kotlin.math.min

Expand All @@ -37,6 +38,26 @@ public fun ByteString(vararg bytes: Byte): ByteString = if (bytes.isEmpty()) {
ByteString.wrap(bytes)
}

/**
* Wraps given [bytes] into a byte string.
*
fzhinkin marked this conversation as resolved.
Show resolved Hide resolved
* Internally, [bytes] will be stored as [ByteArray], and accesses to individual
* [ByteString]'s bytes will return a signed [Byte] as if someone call `bytes[i].toByte()`.
*
* @param bytes a sequence of bytes to be wrapped.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.constructionFromUBytesSample
*/
@OptIn(ExperimentalUnsignedTypes::class)
public fun ByteString(vararg bytes: UByte): ByteString = if (bytes.isEmpty()) {
ByteString.EMPTY
} else {
ByteString.wrap(bytes.asByteArray())
}

@JsName("EmptyByteString")
public fun ByteString(): ByteString = ByteString.EMPTY

/**
* An immutable wrapper around a byte sequence providing [String] like functionality.
*
Expand Down
10 changes: 10 additions & 0 deletions bytestring/common/test/ByteStringTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,14 @@ class ByteStringTest {
assertFalse(ByteString(1, 2, 3, 4, 5).contentEquals(byteArrayOf(1, 2, 3, 4, 5, 6)))
assertFalse(ByteString(1, 2, 3, 4, 5, 6).contentEquals(byteArrayOf(1, 2, 3, 4, 5)))
}

@OptIn(ExperimentalUnsignedTypes::class)
@Test
fun fromUBytes() {
val str = ByteString(0xDEu, 0xADu, 0xC0u, 0xDEu)
assertContentEquals(
byteArrayOf(0xDE.toByte(), 0xAD.toByte(), 0xC0u.toByte(), 0xDEu.toByte()),
str.toByteArray()
)
}
}
8 changes: 8 additions & 0 deletions bytestring/common/test/samples/samples.kt
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ class ByteStringSamples {
assertEquals(3, byteStringFromBytes.size)
}

@OptIn(ExperimentalUnsignedTypes::class)
@Test
fun constructionFromUBytesSample() {
val byteStringFromBytes = ByteString(0xCAu, 0xFEu)
assertFalse(byteStringFromBytes.isEmpty())
assertEquals(2, byteStringFromBytes.size)
}

@Test
fun encodeAndDecodeUtf8String() {
val helloAsByteString = "hello".encodeToByteString()
Expand Down