Skip to content

Commit

Permalink
Implement contentEquals
Browse files Browse the repository at this point in the history
  • Loading branch information
fzhinkin committed Jun 23, 2023
1 parent c627408 commit eafb2dc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions bytestring/api/kotlinx-io-bytestring.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions bytestring/src/commonMain/kotlin/ByteString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}

0 comments on commit eafb2dc

Please sign in to comment.