Skip to content

Commit 742fc26

Browse files
authored
Add samples (#154)
1 parent 7f11771 commit 742fc26

25 files changed

+1571
-12
lines changed

bytestring/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,7 @@ tasks.withType<DokkaTaskPartial>().configureEach {
7676
suppress.set(true)
7777
matchingRegex.set(".*unsafe.*")
7878
}
79+
80+
samples.from("common/test/samples/samples.kt")
7981
}
8082
}

bytestring/common/src/ByteString.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import kotlin.math.min
2828
* Wraps given [bytes] into a byte string.
2929
*
3030
* @param bytes a sequence of bytes to be wrapped.
31+
*
32+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.constructionFromBytesSample
3133
*/
3234
public fun ByteString(vararg bytes: Byte): ByteString = if (bytes.isEmpty()) {
3335
ByteString.EMPTY
@@ -61,6 +63,8 @@ public class ByteString private constructor(
6163
*
6264
* @throws IndexOutOfBoundsException when [startIndex] or [endIndex] is out of range of [data] array indices.
6365
* @throws IllegalArgumentException when `startIndex > endIndex`.
66+
*
67+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.constructionSample
6468
*/
6569
public constructor(data: ByteArray, startIndex: Int = 0, endIndex: Int = data.size) :
6670
this(data.copyOfRange(startIndex, endIndex), null)
@@ -135,6 +139,9 @@ public class ByteString private constructor(
135139
*
136140
* @throws IndexOutOfBoundsException when [startIndex] or [endIndex] is out of range of byte string indices.
137141
* @throws IllegalArgumentException when `startIndex > endIndex`.
142+
*
143+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.toByteArraySample
144+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.toByteArrayWithIndicesSample
138145
*/
139146
public fun toByteArray(startIndex: Int = 0, endIndex: Int = size): ByteArray {
140147
require(startIndex <= endIndex) { "startIndex ($startIndex) > endIndex ($endIndex)" }
@@ -154,6 +161,8 @@ public class ByteString private constructor(
154161
* @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at
155162
* the specified [destinationOffset], or when that index is out of the [destination] array indices range.
156163
* @throws IllegalArgumentException when `startIndex > endIndex`.
164+
*
165+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.copyToSample
157166
*/
158167
public fun copyInto(
159168
destination: ByteArray, destinationOffset: Int = 0,
@@ -172,6 +181,8 @@ public class ByteString private constructor(
172181
*
173182
* @throws IndexOutOfBoundsException when [startIndex] or [endIndex] is out of range of byte string indices.
174183
* @throws IllegalArgumentException when `startIndex > endIndex`.
184+
*
185+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.substringSample
175186
*/
176187
public fun substring(startIndex: Int, endIndex: Int = size): ByteString = if (startIndex == endIndex) {
177188
EMPTY
@@ -187,6 +198,8 @@ public class ByteString private constructor(
187198
* The behavior is similar to [String.compareTo].
188199
*
189200
* @param other the byte string to compare this string to.
201+
*
202+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.compareTo
190203
*/
191204
override fun compareTo(other: ByteString): Int {
192205
if (other === this) return 0
@@ -210,6 +223,8 @@ public class ByteString private constructor(
210223
* Note that a string representation includes the whole byte string content encoded.
211224
* Due to limitations exposed for the maximum string length, an attempt to return a string representation
212225
* of too long byte string may fail.
226+
*
227+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.toStringSample
213228
*/
214229
override fun toString(): String {
215230
if (isEmpty()) {
@@ -257,6 +272,8 @@ public val ByteString.indices: IntRange
257272
*
258273
* @param byte the value to search for.
259274
* @param startIndex the index (inclusive) starting from which the [byte] should be searched.
275+
*
276+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.indexOfByteSample
260277
*/
261278
public fun ByteString.indexOf(byte: Byte, startIndex: Int = 0): Int {
262279
val localData = getBackingArrayReference()
@@ -277,6 +294,8 @@ public fun ByteString.indexOf(byte: Byte, startIndex: Int = 0): Int {
277294
*
278295
* @param byteString the value to search for.
279296
* @param startIndex the index (inclusive) starting from which the [byteString] should be searched.
297+
*
298+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.indexOfByteStringSample
280299
*/
281300
public fun ByteString.indexOf(byteString: ByteString, startIndex: Int = 0): Int {
282301
if (byteString.isEmpty()) return max(min(startIndex, size), 0)
@@ -299,6 +318,8 @@ public fun ByteString.indexOf(byteString: ByteString, startIndex: Int = 0): Int
299318
*
300319
* @param byteArray the value to search for.
301320
* @param startIndex the index (inclusive) starting from which the [byteArray] should be searched.
321+
*
322+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.indexOfByteArraySample
302323
*/
303324
public fun ByteString.indexOf(byteArray: ByteArray, startIndex: Int = 0): Int {
304325
if (byteArray.isEmpty()) return max(min(startIndex, size), 0)
@@ -321,6 +342,8 @@ public fun ByteString.indexOf(byteArray: ByteArray, startIndex: Int = 0): Int {
321342
*
322343
* @param byte the value to search for.
323344
* @param startIndex the index (inclusive) starting from which the [byte] should be searched.
345+
*
346+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.lastIndexOfByteSample
324347
*/
325348
public fun ByteString.lastIndexOf(byte: Byte, startIndex: Int = 0): Int {
326349
val localData = getBackingArrayReference()
@@ -341,6 +364,8 @@ public fun ByteString.lastIndexOf(byte: Byte, startIndex: Int = 0): Int {
341364
*
342365
* @param byteString the value to search for.
343366
* @param startIndex the index (inclusive) starting from which the [byteString] should be searched.
367+
*
368+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.lastIndexOfByteStringSample
344369
*/
345370
public fun ByteString.lastIndexOf(byteString: ByteString, startIndex: Int = 0): Int {
346371
if (byteString.isEmpty()) return size
@@ -361,6 +386,8 @@ public fun ByteString.lastIndexOf(byteString: ByteString, startIndex: Int = 0):
361386
*
362387
* @param byteArray the value to search for.
363388
* @param startIndex the index (inclusive) starting from which the [byteArray] should be searched.
389+
*
390+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.lastIndexOfByteArraySample
364391
*/
365392
public fun ByteString.lastIndexOf(byteArray: ByteArray, startIndex: Int = 0): Int {
366393
if (byteArray.isEmpty()) return size
@@ -378,6 +405,8 @@ public fun ByteString.lastIndexOf(byteArray: ByteArray, startIndex: Int = 0): In
378405
* Behavior of this method is compatible with [CharSequence.startsWith].
379406
*
380407
* @param byteArray the prefix to check for.
408+
*
409+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.startsWithByteArraySample
381410
*/
382411
public fun ByteString.startsWith(byteArray: ByteArray): Boolean = when {
383412
byteArray.size > size -> false
@@ -390,6 +419,8 @@ public fun ByteString.startsWith(byteArray: ByteArray): Boolean = when {
390419
* Behavior of this method is compatible with [CharSequence.startsWith].
391420
*
392421
* @param byteString the prefix to check for.
422+
*
423+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.startsWithByteStringSample
393424
*/
394425
public fun ByteString.startsWith(byteString: ByteString): Boolean = when {
395426
byteString.size > size -> false
@@ -403,6 +434,8 @@ public fun ByteString.startsWith(byteString: ByteString): Boolean = when {
403434
* Behavior of this method is compatible with [CharSequence.endsWith].
404435
*
405436
* @param byteArray the suffix to check for.
437+
*
438+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.endsWithByteArraySample
406439
*/
407440
public fun ByteString.endsWith(byteArray: ByteArray): Boolean = when {
408441
byteArray.size > size -> false
@@ -415,6 +448,8 @@ public fun ByteString.endsWith(byteArray: ByteArray): Boolean = when {
415448
* Behavior of this method is compatible with [CharSequence.endsWith].
416449
*
417450
* @param byteString the suffix to check for.
451+
*
452+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.endsWithByteStringSample
418453
*/
419454
public fun ByteString.endsWith(byteString: ByteString): Boolean = when {
420455
byteString.size > size -> false
@@ -461,13 +496,17 @@ public fun ByteString.isNotEmpty(): Boolean = !isEmpty()
461496

462497
/**
463498
* Decodes content of a byte string into a string using UTF-8 encoding.
499+
*
500+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.encodeAndDecodeUtf8String
464501
*/
465502
public fun ByteString.decodeToString(): String {
466503
return getBackingArrayReference().decodeToString()
467504
}
468505

469506
/**
470507
* Encodes a string into a byte sequence using UTF8-encoding and wraps it into a byte string.
508+
*
509+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.encodeAndDecodeUtf8String
471510
*/
472511
public fun String.encodeToByteString(): ByteString {
473512
return ByteString.wrap(encodeToByteArray())

bytestring/common/src/ByteStringBuilder.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import kotlin.math.max
2121
* will be allocated and previously written data will be copied into it.
2222
*
2323
* @param initialCapacity the initial size of an underlying byte sequence.
24+
*
25+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.builderSample
26+
* @sample kotlinx.io.bytestring.samples.ByteStringSamples.builderSampleWithoutAdditionalAllocs
2427
*/
2528
public class ByteStringBuilder(initialCapacity: Int = 0) {
2629
private var buffer = ByteArray(initialCapacity)

0 commit comments

Comments
 (0)