Skip to content

Use exception constructor invocation instead of unnecessary function object creation #120

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

Merged
merged 1 commit into from
Feb 6, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ internal inline fun Buffer.commonDigestInto(
callsInPlace(bufPosSet, InvocationKind.AT_MOST_ONCE)
}

dest.commonCheckArgs(destOffset, digestLength, onShortInput = {
ShortBufferException("Not enough room in dest for $digestLength bytes")
})
dest.commonCheckArgs(destOffset, digestLength, onShortInput = ::ShortBufferException)

// Zero out any stale input that may be left in the buffer
value.fill(0, bufPos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ internal inline fun Digest.commonToString(): String {
internal inline fun ByteArray.commonCheckArgs(
offset: Int,
len: Int,
onShortInput: () -> Exception = { IllegalArgumentException("Input too short") },
onOutOfBounds: (message: String) -> Exception = { message -> IndexOutOfBoundsException(message) },
onShortInput: (message: String) -> Exception = ::IllegalArgumentException,
onOutOfBounds: (message: String) -> Exception = ::IndexOutOfBoundsException,
) {
contract {
callsInPlace(onShortInput, InvocationKind.AT_MOST_ONCE)
callsInPlace(onOutOfBounds, InvocationKind.AT_MOST_ONCE)
}

if (size - offset < len) throw onShortInput()
if (size - offset < len) throw onShortInput("Too Short. size[$size] - offset[$offset] < len[$len]")
if (offset < 0) throw onOutOfBounds("offset[$offset] < 0")
if (len < 0) throw onOutOfBounds("len[$len] < 0")
if (offset > size - len) throw onOutOfBounds("offset[$offset] > size[$size] - len[$len]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public actual abstract class Digest: MessageDigest, Algorithm, Cloneable, Copyab
@Deprecated("Use digestInto", ReplaceWith("digestInto(buf, offset)"))
public final override fun digest(buf: ByteArray?, offset: Int, len: Int): Int {
requireNotNull(buf) { "buf cannot be null" }
buf.commonCheckArgs(offset, len, onOutOfBounds = { message -> DigestException(message) })
buf.commonCheckArgs(offset, len, onOutOfBounds = ::DigestException)
@Suppress("DEPRECATION")
return engineDigest(buf, offset, len)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ internal inline fun Mac.commonToString(): String {
internal inline fun ByteArray.commonCheckArgs(
offset: Int,
len: Int,
onShortInput: () -> Exception = { IllegalArgumentException("Input too short") },
onOutOfBounds: (message: String) -> Exception = { message -> IndexOutOfBoundsException(message) },
onShortInput: (message: String) -> Exception = ::IllegalArgumentException,
onOutOfBounds: (message: String) -> Exception = ::IndexOutOfBoundsException,
) {
contract {
callsInPlace(onShortInput, InvocationKind.AT_MOST_ONCE)
callsInPlace(onOutOfBounds, InvocationKind.AT_MOST_ONCE)
}

if (size - offset < len) throw onShortInput()
if (size - offset < len) throw onShortInput("Too Short. size[$size] - offset[$offset] < len[$len]")
if (offset < 0) throw onOutOfBounds("offset[$offset] < 0")
if (len < 0) throw onOutOfBounds("len[$len] < 0")
if (offset > size - len) throw onOutOfBounds("offset[$offset] > size[$size] - len[$len]")
Expand Down Expand Up @@ -87,9 +87,7 @@ internal inline fun Mac.commonDoFinalInto(
}

val len = macLength()
dest.commonCheckArgs(destOffset, len, onShortInput = {
ShortBufferException("Not enough room in dest for $len bytes")
})
dest.commonCheckArgs(destOffset, len, onShortInput = ::ShortBufferException)
engineDoFinalInto(dest, destOffset)
if (engineResetOnDoFinal) engineReset()
return len
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public actual abstract class Mac: Algorithm, Copyable<Mac>, Resettable, Updatabl
}
// See Updatable interface documentation
public actual final override fun update(input: ByteArray, offset: Int, len: Int) {
input.commonCheckArgs(offset, len, onOutOfBounds = { message -> IllegalArgumentException(message) })
input.commonCheckArgs(offset, len, onOutOfBounds = ::IllegalArgumentException)
engine.update(input, offset, len)
}

Expand Down
Loading