Skip to content

Commit 961779e

Browse files
Add ByteString.isNotEmpty Kotlin extension function.
PiperOrigin-RevId: 552833973
1 parent 21edc3b commit 961779e

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

java/kotlin/src/main/kotlin/com/google/protobuf/ByteStrings.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@ import com.google.protobuf.ByteString
3434
import java.nio.ByteBuffer
3535

3636
/** Encodes this String into a sequence of UTF-8 bytes and returns the result as a [ByteString]. */
37-
fun String.toByteStringUtf8(): ByteString = ByteString.copyFromUtf8(this)
38-
// symmetric from ByteString.toStringUtf8()
37+
fun String.toByteStringUtf8(): ByteString =
38+
ByteString.copyFromUtf8(this) // symmetric from ByteString.toStringUtf8()
3939

4040
/** Concatenates the given [ByteString] to this one. */
4141
operator fun ByteString.plus(other: ByteString): ByteString = concat(other)
4242

4343
/** Gets the byte at [index]. */
4444
operator fun ByteString.get(index: Int): Byte = byteAt(index)
4545

46+
/** Checks if this is not empty. */
47+
fun ByteString.isNotEmpty(): Boolean = !isEmpty()
48+
4649
/** Returns a copy of this [ByteArray] as an immutable [ByteString]. */
4750
fun ByteArray.toByteString(): ByteString = ByteString.copyFrom(this)
4851

java/kotlin/src/test/kotlin/com/google/protobuf/ByteStringsTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ class ByteStringsTest {
6262
assertThat(str[2]).isEqualTo('c'.toByte())
6363
}
6464

65+
@Test
66+
fun isNotEmpty_returnsTrue_whenNotEmpty() {
67+
assertThat("abc".toByteStringUtf8().isNotEmpty()).isTrue()
68+
}
69+
70+
@Test
71+
fun isNotEmpty_returnsFalse_whenEmpty() {
72+
assertThat(ByteString.EMPTY.isNotEmpty()).isFalse()
73+
}
74+
6575
@Test
6676
fun byteAtBelowZero() {
6777
val str = "abc".toByteStringUtf8()

0 commit comments

Comments
 (0)