Skip to content

Commit 3f7c251

Browse files
authored
Use exception constructor invocation instead of unnecessary function object creation (#120)
1 parent adaf87e commit 3f7c251

File tree

5 files changed

+10
-14
lines changed

5 files changed

+10
-14
lines changed

library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-Buffer.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ internal inline fun Buffer.commonDigestInto(
179179
callsInPlace(bufPosSet, InvocationKind.AT_MOST_ONCE)
180180
}
181181

182-
dest.commonCheckArgs(destOffset, digestLength, onShortInput = {
183-
ShortBufferException("Not enough room in dest for $digestLength bytes")
184-
})
182+
dest.commonCheckArgs(destOffset, digestLength, onShortInput = ::ShortBufferException)
185183

186184
// Zero out any stale input that may be left in the buffer
187185
value.fill(0, bufPos)

library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-CommonPlatform.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ internal inline fun Digest.commonToString(): String {
3131
internal inline fun ByteArray.commonCheckArgs(
3232
offset: Int,
3333
len: Int,
34-
onShortInput: () -> Exception = { IllegalArgumentException("Input too short") },
35-
onOutOfBounds: (message: String) -> Exception = { message -> IndexOutOfBoundsException(message) },
34+
onShortInput: (message: String) -> Exception = ::IllegalArgumentException,
35+
onOutOfBounds: (message: String) -> Exception = ::IndexOutOfBoundsException,
3636
) {
3737
contract {
3838
callsInPlace(onShortInput, InvocationKind.AT_MOST_ONCE)
3939
callsInPlace(onOutOfBounds, InvocationKind.AT_MOST_ONCE)
4040
}
4141

42-
if (size - offset < len) throw onShortInput()
42+
if (size - offset < len) throw onShortInput("Too Short. size[$size] - offset[$offset] < len[$len]")
4343
if (offset < 0) throw onOutOfBounds("offset[$offset] < 0")
4444
if (len < 0) throw onOutOfBounds("len[$len] < 0")
4545
if (offset > size - len) throw onOutOfBounds("offset[$offset] > size[$size] - len[$len]")

library/digest/src/jvmMain/kotlin/org/kotlincrypto/core/digest/Digest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public actual abstract class Digest: MessageDigest, Algorithm, Cloneable, Copyab
253253
@Deprecated("Use digestInto", ReplaceWith("digestInto(buf, offset)"))
254254
public final override fun digest(buf: ByteArray?, offset: Int, len: Int): Int {
255255
requireNotNull(buf) { "buf cannot be null" }
256-
buf.commonCheckArgs(offset, len, onOutOfBounds = { message -> DigestException(message) })
256+
buf.commonCheckArgs(offset, len, onOutOfBounds = ::DigestException)
257257
@Suppress("DEPRECATION")
258258
return engineDigest(buf, offset, len)
259259
}

library/mac/src/commonMain/kotlin/org/kotlincrypto/core/mac/internal/-CommonPlatform.kt

+4-6
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ internal inline fun Mac.commonToString(): String {
4040
internal inline fun ByteArray.commonCheckArgs(
4141
offset: Int,
4242
len: Int,
43-
onShortInput: () -> Exception = { IllegalArgumentException("Input too short") },
44-
onOutOfBounds: (message: String) -> Exception = { message -> IndexOutOfBoundsException(message) },
43+
onShortInput: (message: String) -> Exception = ::IllegalArgumentException,
44+
onOutOfBounds: (message: String) -> Exception = ::IndexOutOfBoundsException,
4545
) {
4646
contract {
4747
callsInPlace(onShortInput, InvocationKind.AT_MOST_ONCE)
4848
callsInPlace(onOutOfBounds, InvocationKind.AT_MOST_ONCE)
4949
}
5050

51-
if (size - offset < len) throw onShortInput()
51+
if (size - offset < len) throw onShortInput("Too Short. size[$size] - offset[$offset] < len[$len]")
5252
if (offset < 0) throw onOutOfBounds("offset[$offset] < 0")
5353
if (len < 0) throw onOutOfBounds("len[$len] < 0")
5454
if (offset > size - len) throw onOutOfBounds("offset[$offset] > size[$size] - len[$len]")
@@ -87,9 +87,7 @@ internal inline fun Mac.commonDoFinalInto(
8787
}
8888

8989
val len = macLength()
90-
dest.commonCheckArgs(destOffset, len, onShortInput = {
91-
ShortBufferException("Not enough room in dest for $len bytes")
92-
})
90+
dest.commonCheckArgs(destOffset, len, onShortInput = ::ShortBufferException)
9391
engineDoFinalInto(dest, destOffset)
9492
if (engineResetOnDoFinal) engineReset()
9593
return len

library/mac/src/nonJvmMain/kotlin/org/kotlincrypto/core/mac/Mac.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public actual abstract class Mac: Algorithm, Copyable<Mac>, Resettable, Updatabl
101101
}
102102
// See Updatable interface documentation
103103
public actual final override fun update(input: ByteArray, offset: Int, len: Int) {
104-
input.commonCheckArgs(offset, len, onOutOfBounds = { message -> IllegalArgumentException(message) })
104+
input.commonCheckArgs(offset, len, onOutOfBounds = ::IllegalArgumentException)
105105
engine.update(input, offset, len)
106106
}
107107

0 commit comments

Comments
 (0)