diff --git a/bytestring/api/kotlinx-io-bytestring.api b/bytestring/api/kotlinx-io-bytestring.api index c9eab718b..439a9f774 100644 --- a/bytestring/api/kotlinx-io-bytestring.api +++ b/bytestring/api/kotlinx-io-bytestring.api @@ -46,6 +46,7 @@ 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 contentEquals (Lkotlinx/io/bytestring/ByteString;[B)Z 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 fromUtf8String (Lkotlinx/io/bytestring/ByteString$Companion;Ljava/lang/String;)Lkotlinx/io/bytestring/ByteString; diff --git a/bytestring/src/commonMain/kotlin/ByteString.kt b/bytestring/src/commonMain/kotlin/ByteString.kt index a608eb741..592940d34 100644 --- a/bytestring/src/commonMain/kotlin/ByteString.kt +++ b/bytestring/src/commonMain/kotlin/ByteString.kt @@ -435,3 +435,13 @@ public fun ByteString.toUtf8String(): String { public fun ByteString.Companion.fromUtf8String(string: String): ByteString { return wrap(string.encodeToByteArray()) } + + +/** + * Returns `true` if the content of this byte string equals to the [array]. + * + * @param array the array to test this byte string's content against. + */ +public fun ByteString.contentEquals(array: ByteArray): Boolean { + return getBackingArrayReference().contentEquals(array) +} \ No newline at end of file diff --git a/bytestring/src/commonTest/kotlin/kotlinx/io/bytestring/ByteStringTest.kt b/bytestring/src/commonTest/kotlin/kotlinx/io/bytestring/ByteStringTest.kt index 9d8249e18..60ca6e2a4 100644 --- a/bytestring/src/commonTest/kotlin/kotlinx/io/bytestring/ByteStringTest.kt +++ b/bytestring/src/commonTest/kotlin/kotlinx/io/bytestring/ByteStringTest.kt @@ -391,4 +391,16 @@ class ByteStringTest { assertEquals(byteString, ByteString(*bronzeHorseman.encodeToByteArray())) assertEquals(byteString.toUtf8String(), bronzeHorseman) } + + @Test + fun contentEquals() { + assertTrue(ByteString().contentEquals(byteArrayOf())) + assertFalse(ByteString(1, 2, 3).contentEquals(byteArrayOf())) + assertFalse(ByteString().contentEquals(byteArrayOf(1, 2, 3))) + + assertTrue(ByteString(1, 2, 3, 4, 5).contentEquals(byteArrayOf(1, 2, 3, 4, 5))) + assertFalse(ByteString(1, 2, 3, 4, 5).contentEquals(byteArrayOf(1, 2, 3, 4, 4))) + 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))) + } }