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

Move builders to package ...encoding.base* #126

Merged
merged 5 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions library/encoding-base16/api/encoding-base16.api
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ public final class io/matthewnelson/encoding/base16/Base16$Config : io/matthewne
public synthetic fun <init> (ZBZLkotlin/jvm/internal/DefaultConstructorMarker;)V
}

public final class io/matthewnelson/encoding/base16/Base16ConfigBuilder {
public field encodeToLowercase Z
public field isLenient Z
public field lineBreakInterval B
public fun <init> ()V
public fun <init> (Lio/matthewnelson/encoding/base16/Base16$Config;)V
public final fun build ()Lio/matthewnelson/encoding/base16/Base16$Config;
public final fun strict ()Lio/matthewnelson/encoding/base16/Base16ConfigBuilder;
}

public final class io/matthewnelson/encoding/base16/BuildersKt {
public static final fun Base16 ()Lio/matthewnelson/encoding/base16/Base16;
public static final fun Base16 (Lio/matthewnelson/encoding/base16/Base16$Config;Lkotlin/jvm/functions/Function1;)Lio/matthewnelson/encoding/base16/Base16;
public static final fun Base16 (Lkotlin/jvm/functions/Function1;)Lio/matthewnelson/encoding/base16/Base16;
public static final fun Base16 (Z)Lio/matthewnelson/encoding/base16/Base16;
public static synthetic fun Base16$default (ZILjava/lang/Object;)Lio/matthewnelson/encoding/base16/Base16;
}

public final class io/matthewnelson/encoding/builders/Base16ConfigBuilder {
public field encodeToLowercase Z
public field isLenient Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.matthewnelson.component.encoding.base16

import io.matthewnelson.encoding.builders.Base16
import io.matthewnelson.encoding.base16.Base16
import io.matthewnelson.encoding.core.Decoder.Companion.decodeToByteArrayOrNull
import io.matthewnelson.encoding.core.Encoder.Companion.encodeToByteArray
import io.matthewnelson.encoding.core.Encoder.Companion.encodeToCharArray
Expand All @@ -28,7 +28,7 @@ import io.matthewnelson.encoding.core.Encoder.Companion.encodeToString
replaceWith = ReplaceWith(
expression = "this.decodeToByteArrayOrNull(Base16())",
imports = [
"io.matthewnelson.encoding.builders.Base16",
"io.matthewnelson.encoding.base16.Base16",
"io.matthewnelson.encoding.core.Decoder.Companion.decodeToByteArrayOrNull",
],
),
Expand All @@ -44,7 +44,7 @@ public inline fun String.decodeBase16ToArray(): ByteArray? {
replaceWith = ReplaceWith(
expression = "this.decodeToByteArrayOrNull(Base16())",
imports = [
"io.matthewnelson.encoding.builders.Base16",
"io.matthewnelson.encoding.base16.Base16",
"io.matthewnelson.encoding.core.Decoder.Companion.decodeToByteArrayOrNull",
],
),
Expand All @@ -59,7 +59,7 @@ public fun CharArray.decodeBase16ToArray(): ByteArray? {
replaceWith = ReplaceWith(
expression = "this.encodeToString(Base16())",
imports = [
"io.matthewnelson.encoding.builders.Base16",
"io.matthewnelson.encoding.base16.Base16",
"io.matthewnelson.encoding.core.Encoder.Companion.encodeToString",
],
),
Expand All @@ -75,7 +75,7 @@ public inline fun ByteArray.encodeBase16(): String {
replaceWith = ReplaceWith(
expression = "this.encodeToCharArray(Base16())",
imports = [
"io.matthewnelson.encoding.builders.Base16",
"io.matthewnelson.encoding.base16.Base16",
"io.matthewnelson.encoding.core.Encoder.Companion.encodeToCharArray",
],
),
Expand All @@ -91,7 +91,7 @@ public inline fun ByteArray.encodeBase16ToCharArray(): CharArray {
ReplaceWith(
expression = "this.encodeToByteArray(Base16())",
imports = [
"io.matthewnelson.encoding.builders.Base16",
"io.matthewnelson.encoding.base16.Base16",
"io.matthewnelson.encoding.core.Encoder.Companion.encodeToByteArray",
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package io.matthewnelson.encoding.base16

import io.matthewnelson.encoding.builders.Base16
import io.matthewnelson.encoding.builders.Base16ConfigBuilder
import io.matthewnelson.encoding.core.*
import io.matthewnelson.encoding.core.util.DecoderInput
import io.matthewnelson.encoding.core.util.FeedBuffer
Expand Down Expand Up @@ -49,7 +47,7 @@ import kotlin.jvm.JvmSynthetic
* val decoded = encoded.decodeToByteArray(Base16).decodeToString()
* assertEquals(text, decoded)
*
* @see [io.matthewnelson.encoding.builders.Base16]
* @see [io.matthewnelson.encoding.base16.Base16]
* @see [Base16.Config]
* @see [Base16.CHARS_UPPER]
* @see [Base16.CHARS_LOWER]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright (c) 2023 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
@file:Suppress("SpellCheckingInspection")

package io.matthewnelson.encoding.base16

import io.matthewnelson.encoding.base16.Base16.Config
import io.matthewnelson.encoding.core.EncodingException
import kotlin.jvm.JvmField
import kotlin.jvm.JvmOverloads

/**
* Creates a configured [Base16] encoder/decoder.
*
* @param [config] inherit settings from.
* @see [Base16ConfigBuilder]
* */
public fun Base16(
config: Config?,
block: Base16ConfigBuilder.() -> Unit
): Base16 {
val builder = Base16ConfigBuilder(config)
block.invoke(builder)
return Base16(builder.build())
}

/**
* Creates a configured [Base16] encoder/decoder.
*
* @see [Base16ConfigBuilder]
* */
public fun Base16(
block: Base16ConfigBuilder.() -> Unit
): Base16 {
return Base16(config = null, block)
}

/**
* Creates a configured [Base16] encoder/decoder
* using the default settings.
*
* @param [strict] If true, configures the encoder/decoder
* to be in strict accordance with RFC 4648.
* @see [Base16ConfigBuilder]
* */
@JvmOverloads
public fun Base16(strict: Boolean = false): Base16 = Base16 { if (strict) strict() }


/**
* Builder for creating a [Base16.Config].
*
* @see [strict]
* @see [io.matthewnelson.encoding.base16.Base16]
* */
public class Base16ConfigBuilder {

public constructor()
public constructor(config: Config?): this() {
if (config == null) return
isLenient = config.isLenient ?: true
lineBreakInterval = config.lineBreakInterval
encodeToLowercase = config.encodeToLowercase
}

/**
* If true, spaces and new lines ('\n', '\r', ' ', '\t')
* will be skipped over when decoding (against RFC 4648).
*
* If false, an [EncodingException] will be thrown if
* those characters are encountered when decoding.
* */
@JvmField
public var isLenient: Boolean = true

/**
* For every [lineBreakInterval] of encoded data, a
* line break will be output.
*
* Will **ONLY** output line breaks if [isLenient] is
* set to **true**.
*
* e.g.
*
* isLenient = true
* lineBreakInterval = 0
* // 48656C6C6F20576F726C6421
*
* isLenient = true
* lineBreakInterval = 16
* // 48656C6C6F20576F
* // 726C6421
*
* isLenient = false
* lineBreakInterval = 16
* // 48656C6C6F20576F726C6421
*
* Enable by setting to a value between 1 and 127, and
* setting [isLenient] to true.
*
* A great value is 64
* */
@JvmField
public var lineBreakInterval: Byte = 0

/**
* If true, will output lowercase characters when
* encoding (against RFC 4648).
*
* If false, will output uppercase characters when
* encoding.
* */
@JvmField
public var encodeToLowercase: Boolean = false

/**
* A shortcut for configuring things to be in strict
* adherence with RFC 4648.
* */
public fun strict(): Base16ConfigBuilder {
isLenient = false
lineBreakInterval = 0
encodeToLowercase = false
return this
}

/**
* Builds a [Base16.Config] for the provided settings.
* */
public fun build(): Config = Config.from(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,30 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
@file:Suppress("SpellCheckingInspection")
@file:Suppress("SpellCheckingInspection", "DEPRECATION")

package io.matthewnelson.encoding.builders

import io.matthewnelson.encoding.base16.Base16
import io.matthewnelson.encoding.base16.Base16.Config
import io.matthewnelson.encoding.base16.Base16
import io.matthewnelson.encoding.core.EncodingException
import kotlin.jvm.JvmField
import kotlin.jvm.JvmOverloads

/**
* Creates a configured [Base16] encoder/decoder.
* Deprecated
*
* @param [config] inherit settings from.
* @see [Base16ConfigBuilder]
* @see [io.matthewnelson.encoding.base16.Base16]
* */
@Deprecated(
message = "Moved to package io.matthewnelson.encoding.base16",
replaceWith = ReplaceWith(
expression = "Base16(config) { block() }",
imports = [
"io.matthewnelson.encoding.base16.Base16"
]
)
)
public fun Base16(
config: Config?,
block: Base16ConfigBuilder.() -> Unit
Expand All @@ -39,34 +47,57 @@ public fun Base16(
}

/**
* Creates a configured [Base16] encoder/decoder.
* Deprecated
*
* @see [Base16ConfigBuilder]
* @see [io.matthewnelson.encoding.base16.Base16]
* */
@Deprecated(
message = "Moved to package io.matthewnelson.encoding.base16",
replaceWith = ReplaceWith(
expression = "Base16 { block() }",
imports = [
"io.matthewnelson.encoding.base16.Base16"
]
)
)
public fun Base16(
block: Base16ConfigBuilder.() -> Unit
): Base16 {
return Base16(config = null, block)
}

/**
* Creates a configured [Base16] encoder/decoder
* using the default settings.
* Deprecated
*
* @param [strict] If true, configures the encoder/decoder
* to be in strict accordance with RFC 4648.
* @see [Base16ConfigBuilder]
* @see [io.matthewnelson.encoding.base16.Base16]
* */
@Deprecated(
message = "Moved to package io.matthewnelson.encoding.base16",
replaceWith = ReplaceWith(
expression = "Base16(strict)",
imports = [
"io.matthewnelson.encoding.base16.Base16"
]
)
)
@JvmOverloads
public fun Base16(strict: Boolean = false): Base16 = Base16 { if (strict) strict() }


/**
* Builder for creating a [Base16.Config].
* Deprecated
*
* @see [strict]
* @see [io.matthewnelson.encoding.builders.Base16]
* @see [io.matthewnelson.encoding.base16.Base16ConfigBuilder]
* */
@Deprecated(
message = "Moved to package io.matthewnelson.encoding.base16",
replaceWith = ReplaceWith(
expression = "Base16ConfigBuilder",
imports = [
"io.matthewnelson.encoding.base16.Base16ConfigBuilder"
]
)
)
public class Base16ConfigBuilder {

public constructor()
Expand Down Expand Up @@ -141,5 +172,11 @@ public class Base16ConfigBuilder {
/**
* Builds a [Base16.Config] for the provided settings.
* */
public fun build(): Config = Config.from(this)
public fun build(): Config {
val b = io.matthewnelson.encoding.base16.Base16ConfigBuilder()
b.isLenient = isLenient
b.lineBreakInterval = lineBreakInterval
b.encodeToLowercase = encodeToLowercase
return b.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
**/
package io.matthewnelson.encoding.base16

import io.matthewnelson.encoding.builders.Base16
import io.matthewnelson.encoding.core.Decoder.Companion.decodeToByteArray
import io.matthewnelson.encoding.core.Decoder.Companion.decodeToByteArrayOrNull
import io.matthewnelson.encoding.core.Encoder.Companion.encodeToString
Expand Down