Skip to content

Commit

Permalink
Deprecate Base64 functions (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
twyatt authored Feb 28, 2023
1 parent 9c35a49 commit 4757ffe
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions encoding/src/commonMain/kotlin/Base64.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ private fun CharSequence.decodeQuartet(index: Int): Int =
(getOrPad(index + 3).decode())

/** Decodes a base64 character sequence to bytes. Decoding is done eagerly. */
@Deprecated(
message = "Use Kotlin stdlib Base64 support.",
replaceWith = ReplaceWith(
expression = "Base64.decode(this)",
imports = ["kotlin.io.encoding.Base64"],
),
)
public fun CharSequence.decodeBase64(): ByteArray {
val numBytes = when {
isEmpty() -> return byteArrayOf()
Expand Down Expand Up @@ -65,6 +72,13 @@ public fun CharSequence.decodeBase64(): ByteArray {
}

/** Decodes a base64 character sequence to bytes. Decoding is done lazily. */
@Deprecated(
message = "Use Kotlin stdlib Base64 support.",
replaceWith = ReplaceWith(
expression = "Base64.decode(this).asSequence()",
imports = ["kotlin.io.encoding.Base64"],
),
)
public fun CharSequence.decodeBase64Sequence(): Sequence<Byte> = sequence {
var index = 0
while (index < length) {
Expand All @@ -90,8 +104,31 @@ private fun Int.encode(): Char = when (this) {
else -> error("Cannot encode more than 6 bits. Received `${this.toString(radix = 2)}`.")
}

@Deprecated(
message = "Use Kotlin stdlib Base64 support.",
replaceWith = ReplaceWith(
expression = "Base64.encode(this)",
imports = ["kotlin.io.encoding.Base64"],
),
)
public fun ByteArray.encodeBase64(): String = iterator().encodeBase64()

@Deprecated(
message = "Use Kotlin stdlib Base64 support.",
replaceWith = ReplaceWith(
expression = "Base64.encode(this.toList().toByteArray())",
imports = ["kotlin.io.encoding.Base64"],
),
)
public fun Sequence<Byte>.encodeBase64(): String = iterator().encodeBase64()

@Deprecated(
message = "Use Kotlin stdlib Base64 support.",
replaceWith = ReplaceWith(
expression = "Base64.encode(this.toByteArray())",
imports = ["kotlin.io.encoding.Base64"],
),
)
public fun Iterable<Byte>.encodeBase64(): String = iterator().encodeBase64()

private fun Iterator<Byte>.encodeBase64(): String {
Expand Down

0 comments on commit 4757ffe

Please sign in to comment.