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

Add samples #154

Merged
merged 7 commits into from
Jun 30, 2023
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/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,7 @@ tasks.withType<DokkaTaskPartial>().configureEach {
suppress.set(true)
matchingRegex.set(".*unsafe.*")
}

samples.from("common/test/samples/samples.kt")
}
}
39 changes: 39 additions & 0 deletions bytestring/common/src/ByteString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import kotlin.math.min
* Wraps given [bytes] into a byte string.
*
* @param bytes a sequence of bytes to be wrapped.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.constructionFromBytesSample
*/
public fun ByteString(vararg bytes: Byte): ByteString = if (bytes.isEmpty()) {
ByteString.EMPTY
Expand All @@ -51,6 +53,8 @@ public class ByteString private constructor(
*
* @throws IndexOutOfBoundsException when [startIndex] or [endIndex] is out of range of [data] array indices.
* @throws IllegalArgumentException when `startIndex > endIndex`.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.constructionSample
*/
public constructor(data: ByteArray, startIndex: Int = 0, endIndex: Int = data.size) :
this(data.copyOfRange(startIndex, endIndex), null)
Expand Down Expand Up @@ -125,6 +129,9 @@ public class ByteString private constructor(
*
* @throws IndexOutOfBoundsException when [startIndex] or [endIndex] is out of range of byte string indices.
* @throws IllegalArgumentException when `startIndex > endIndex`.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.toByteArraySample
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.toByteArrayWithIndicesSample
*/
public fun toByteArray(startIndex: Int = 0, endIndex: Int = size): ByteArray {
require(startIndex <= endIndex) { "startIndex ($startIndex) > endIndex ($endIndex)" }
Expand All @@ -144,6 +151,8 @@ public class ByteString private constructor(
* @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at
* the specified [destinationOffset], or when that index is out of the [destination] array indices range.
* @throws IllegalArgumentException when `startIndex > endIndex`.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.copyToSample
*/
public fun copyInto(
destination: ByteArray, destinationOffset: Int = 0,
Expand All @@ -162,6 +171,8 @@ public class ByteString private constructor(
*
* @throws IndexOutOfBoundsException when [startIndex] or [endIndex] is out of range of byte string indices.
* @throws IllegalArgumentException when `startIndex > endIndex`.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.substringSample
*/
public fun substring(startIndex: Int, endIndex: Int = size): ByteString = if (startIndex == endIndex) {
EMPTY
Expand All @@ -177,6 +188,8 @@ public class ByteString private constructor(
* The behavior is similar to [String.compareTo].
*
* @param other the byte string to compare this string to.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.compareTo
*/
override fun compareTo(other: ByteString): Int {
if (other === this) return 0
Expand All @@ -200,6 +213,8 @@ public class ByteString private constructor(
* Note that a string representation includes the whole byte string content encoded.
* Due to limitations exposed for the maximum string length, an attempt to return a string representation
* of too long byte string may fail.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.toStringSample
*/
override fun toString(): String {
if (isEmpty()) {
Expand Down Expand Up @@ -247,6 +262,8 @@ public val ByteString.indices: IntRange
*
* @param byte the value to search for.
* @param startIndex the index (inclusive) starting from which the [byte] should be searched.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.indexOfByteSample
*/
public fun ByteString.indexOf(byte: Byte, startIndex: Int = 0): Int {
val localData = getBackingArrayReference()
Expand All @@ -267,6 +284,8 @@ public fun ByteString.indexOf(byte: Byte, startIndex: Int = 0): Int {
*
* @param byteString the value to search for.
* @param startIndex the index (inclusive) starting from which the [byteString] should be searched.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.indexOfByteStringSample
*/
public fun ByteString.indexOf(byteString: ByteString, startIndex: Int = 0): Int {
if (byteString.isEmpty()) return max(min(startIndex, size), 0)
Expand All @@ -289,6 +308,8 @@ public fun ByteString.indexOf(byteString: ByteString, startIndex: Int = 0): Int
*
* @param byteArray the value to search for.
* @param startIndex the index (inclusive) starting from which the [byteArray] should be searched.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.indexOfByteArraySample
*/
public fun ByteString.indexOf(byteArray: ByteArray, startIndex: Int = 0): Int {
if (byteArray.isEmpty()) return max(min(startIndex, size), 0)
Expand All @@ -311,6 +332,8 @@ public fun ByteString.indexOf(byteArray: ByteArray, startIndex: Int = 0): Int {
*
* @param byte the value to search for.
* @param startIndex the index (inclusive) starting from which the [byte] should be searched.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.lastIndexOfByteSample
*/
public fun ByteString.lastIndexOf(byte: Byte, startIndex: Int = 0): Int {
val localData = getBackingArrayReference()
Expand All @@ -331,6 +354,8 @@ public fun ByteString.lastIndexOf(byte: Byte, startIndex: Int = 0): Int {
*
* @param byteString the value to search for.
* @param startIndex the index (inclusive) starting from which the [byteString] should be searched.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.lastIndexOfByteStringSample
*/
public fun ByteString.lastIndexOf(byteString: ByteString, startIndex: Int = 0): Int {
if (byteString.isEmpty()) return size
Expand All @@ -351,6 +376,8 @@ public fun ByteString.lastIndexOf(byteString: ByteString, startIndex: Int = 0):
*
* @param byteArray the value to search for.
* @param startIndex the index (inclusive) starting from which the [byteArray] should be searched.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.lastIndexOfByteArraySample
*/
public fun ByteString.lastIndexOf(byteArray: ByteArray, startIndex: Int = 0): Int {
if (byteArray.isEmpty()) return size
Expand All @@ -368,6 +395,8 @@ public fun ByteString.lastIndexOf(byteArray: ByteArray, startIndex: Int = 0): In
* Behavior of this method is compatible with [CharSequence.startsWith].
*
* @param byteArray the prefix to check for.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.startsWithByteArraySample
*/
public fun ByteString.startsWith(byteArray: ByteArray): Boolean = when {
byteArray.size > size -> false
Expand All @@ -380,6 +409,8 @@ public fun ByteString.startsWith(byteArray: ByteArray): Boolean = when {
* Behavior of this method is compatible with [CharSequence.startsWith].
*
* @param byteString the prefix to check for.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.startsWithByteStringSample
*/
public fun ByteString.startsWith(byteString: ByteString): Boolean = when {
byteString.size > size -> false
Expand All @@ -393,6 +424,8 @@ public fun ByteString.startsWith(byteString: ByteString): Boolean = when {
* Behavior of this method is compatible with [CharSequence.endsWith].
*
* @param byteArray the suffix to check for.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.endsWithByteArraySample
*/
public fun ByteString.endsWith(byteArray: ByteArray): Boolean = when {
byteArray.size > size -> false
Expand All @@ -405,6 +438,8 @@ public fun ByteString.endsWith(byteArray: ByteArray): Boolean = when {
* Behavior of this method is compatible with [CharSequence.endsWith].
*
* @param byteString the suffix to check for.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.endsWithByteStringSample
*/
public fun ByteString.endsWith(byteString: ByteString): Boolean = when {
byteString.size > size -> false
Expand Down Expand Up @@ -451,13 +486,17 @@ public fun ByteString.isNotEmpty(): Boolean = !isEmpty()

/**
* Decodes content of a byte string into a string using UTF-8 encoding.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.encodeAndDecodeUtf8String
*/
public fun ByteString.decodeToString(): String {
return getBackingArrayReference().decodeToString()
}

/**
* Encodes a string into a byte sequence using UTF8-encoding and wraps it into a byte string.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.encodeAndDecodeUtf8String
*/
public fun String.encodeToByteString(): ByteString {
return ByteString.wrap(encodeToByteArray())
Expand Down
3 changes: 3 additions & 0 deletions bytestring/common/src/ByteStringBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import kotlin.math.max
* will be allocated and previously written data will be copied into it.
*
* @param initialCapacity the initial size of an underlying byte sequence.
*
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.builderSample
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.builderSampleWithoutAdditionalAllocs
*/
public class ByteStringBuilder(initialCapacity: Int = 0) {
private var buffer = ByteArray(initialCapacity)
Expand Down
Loading