Skip to content

Commit 5fde3db

Browse files
committed
Add & utilize unpack inline functions
1 parent a919b1a commit 5fde3db

File tree

2 files changed

+61
-82
lines changed

2 files changed

+61
-82
lines changed

library/endian/src/commonMain/kotlin/org/kotlincrypto/bitops/endian/Endian.kt

Lines changed: 18 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,31 +1907,17 @@ public sealed class Endian private constructor() {
19071907
public override fun shortFrom(
19081908
source: ByteArray,
19091909
offset: Int,
1910-
): Short = B0(source[offset]).toBEShort(
1911-
source[offset + 1],
1912-
)
1910+
): Short = source.unpackBEShort(offset)
19131911

19141912
public override fun intFrom(
19151913
source: ByteArray,
19161914
offset: Int,
1917-
): Int = B0(source[offset]).toBEInt(
1918-
source[offset + 1],
1919-
source[offset + 2],
1920-
source[offset + 3],
1921-
)
1915+
): Int = source.unpackBEInt(offset)
19221916

19231917
public override fun longFrom(
19241918
source: ByteArray,
19251919
offset: Int
1926-
): Long = B0(source[offset]).toBELong(
1927-
source[offset + 1],
1928-
source[offset + 2],
1929-
source[offset + 3],
1930-
source[offset + 4],
1931-
source[offset + 5],
1932-
source[offset + 6],
1933-
source[offset + 7],
1934-
)
1920+
): Long = source.unpackBELong(offset)
19351921

19361922
public override fun packUnsafe(
19371923
source: Short,
@@ -1975,15 +1961,12 @@ public sealed class Endian private constructor() {
19751961
sourceIndexStart: Int,
19761962
sourceIndexEnd: Int,
19771963
): ShortArray = dest.packArray(
1964+
source = source,
19781965
destOffset = destOffset,
19791966
sourceIndexStart = sourceIndexStart,
19801967
sourceIndexEnd = sourceIndexEnd,
19811968
numberSizeBytes = Short.SIZE_BYTES,
1982-
unpackNumber = { sourcePos, destPos ->
1983-
dest[destPos] = B0(source[sourcePos]).toBEShort(
1984-
source[sourcePos + 1],
1985-
)
1986-
},
1969+
unpackNumber = { sourcePos, destPos -> dest[destPos] = unpackBEShort(sourcePos) },
19871970
)
19881971

19891972
public override fun packUnsafe(
@@ -2028,17 +2011,12 @@ public sealed class Endian private constructor() {
20282011
sourceIndexStart: Int,
20292012
sourceIndexEnd: Int,
20302013
): IntArray = dest.packArray(
2014+
source = source,
20312015
destOffset = destOffset,
20322016
sourceIndexStart = sourceIndexStart,
20332017
sourceIndexEnd = sourceIndexEnd,
20342018
numberSizeBytes = Int.SIZE_BYTES,
2035-
unpackNumber = { sourcePos, destPos ->
2036-
dest[destPos] = B0(source[sourcePos]).toBEInt(
2037-
source[sourcePos + 1],
2038-
source[sourcePos + 2],
2039-
source[sourcePos + 3],
2040-
)
2041-
},
2019+
unpackNumber = { sourcePos, destPos -> dest[destPos] = unpackBEInt(sourcePos) },
20422020
)
20432021

20442022
public override fun packUnsafe(
@@ -2083,21 +2061,12 @@ public sealed class Endian private constructor() {
20832061
sourceIndexStart: Int,
20842062
sourceIndexEnd: Int,
20852063
): LongArray = dest.packArray(
2064+
source = source,
20862065
destOffset = destOffset,
20872066
sourceIndexStart = sourceIndexStart,
20882067
sourceIndexEnd = sourceIndexEnd,
20892068
numberSizeBytes = Long.SIZE_BYTES,
2090-
unpackNumber = { sourcePos, destPos ->
2091-
dest[destPos] = B0(source[sourcePos]).toBELong(
2092-
source[sourcePos + 1],
2093-
source[sourcePos + 2],
2094-
source[sourcePos + 3],
2095-
source[sourcePos + 4],
2096-
source[sourcePos + 5],
2097-
source[sourcePos + 6],
2098-
source[sourcePos + 7],
2099-
)
2100-
},
2069+
unpackNumber = { sourcePos, destPos -> dest[destPos] = source.unpackBELong(sourcePos) },
21012070
)
21022071
}
21032072

@@ -2357,31 +2326,17 @@ public sealed class Endian private constructor() {
23572326
public override fun shortFrom(
23582327
source: ByteArray,
23592328
offset: Int,
2360-
): Short = B0(source[offset]).toLEShort(
2361-
source[offset + 1],
2362-
)
2329+
): Short = source.unpackLEShort(offset)
23632330

23642331
public override fun intFrom(
23652332
source: ByteArray,
23662333
offset: Int,
2367-
): Int = B0(source[offset]).toLEInt(
2368-
source[offset + 1],
2369-
source[offset + 2],
2370-
source[offset + 3],
2371-
)
2334+
): Int = source.unpackLEInt(offset)
23722335

23732336
public override fun longFrom(
23742337
source: ByteArray,
23752338
offset: Int
2376-
): Long = B0(source[offset]).toLELong(
2377-
source[offset + 1],
2378-
source[offset + 2],
2379-
source[offset + 3],
2380-
source[offset + 4],
2381-
source[offset + 5],
2382-
source[offset + 6],
2383-
source[offset + 7],
2384-
)
2339+
): Long = source.unpackLELong(offset)
23852340

23862341
public override fun packUnsafe(
23872342
source: Short,
@@ -2425,15 +2380,12 @@ public sealed class Endian private constructor() {
24252380
sourceIndexStart: Int,
24262381
sourceIndexEnd: Int,
24272382
): ShortArray = dest.packArray(
2383+
source = source,
24282384
destOffset = destOffset,
24292385
sourceIndexStart = sourceIndexStart,
24302386
sourceIndexEnd = sourceIndexEnd,
24312387
numberSizeBytes = Short.SIZE_BYTES,
2432-
unpackNumber = { sourcePos, destPos ->
2433-
dest[destPos] = B0(source[sourcePos]).toLEShort(
2434-
source[sourcePos + 1],
2435-
)
2436-
},
2388+
unpackNumber = { sourcePos, destPos -> dest[destPos] = source.unpackLEShort(sourcePos) },
24372389
)
24382390

24392391
public override fun packUnsafe(
@@ -2478,17 +2430,12 @@ public sealed class Endian private constructor() {
24782430
sourceIndexStart: Int,
24792431
sourceIndexEnd: Int,
24802432
): IntArray = dest.packArray(
2433+
source = source,
24812434
destOffset = destOffset,
24822435
sourceIndexStart = sourceIndexStart,
24832436
sourceIndexEnd = sourceIndexEnd,
24842437
numberSizeBytes = Int.SIZE_BYTES,
2485-
unpackNumber = { sourcePos, destPos ->
2486-
dest[destPos] = B0(source[sourcePos]).toLEInt(
2487-
source[sourcePos + 1],
2488-
source[sourcePos + 2],
2489-
source[sourcePos + 3],
2490-
)
2491-
},
2438+
unpackNumber = { sourcePos, destPos -> dest[destPos] = source.unpackLEInt(sourcePos) },
24922439
)
24932440

24942441
public override fun packUnsafe(
@@ -2533,21 +2480,12 @@ public sealed class Endian private constructor() {
25332480
sourceIndexStart: Int,
25342481
sourceIndexEnd: Int,
25352482
): LongArray = dest.packArray(
2483+
source = source,
25362484
destOffset = destOffset,
25372485
sourceIndexStart = sourceIndexStart,
25382486
sourceIndexEnd = sourceIndexEnd,
25392487
numberSizeBytes = Long.SIZE_BYTES,
2540-
unpackNumber = { sourcePos, destPos ->
2541-
dest[destPos] = B0(source[sourcePos]).toLELong(
2542-
source[sourcePos + 1],
2543-
source[sourcePos + 2],
2544-
source[sourcePos + 3],
2545-
source[sourcePos + 4],
2546-
source[sourcePos + 5],
2547-
source[sourcePos + 6],
2548-
source[sourcePos + 7],
2549-
)
2550-
},
2488+
unpackNumber = { sourcePos, destPos -> dest[destPos] = source.unpackLELong(sourcePos) },
25512489
)
25522490
}
25532491

library/endian/src/commonMain/kotlin/org/kotlincrypto/bitops/endian/internal/-EndianUtils.kt

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,46 @@ internal inline fun ByteArray.packLELong(source: Long, offset: Int): ByteArray {
118118
return this
119119
}
120120

121+
internal inline fun ByteArray.unpackBEShort(offset: Int): Short = B0(this[offset]).toBEShort(
122+
this[offset + 1],
123+
)
124+
125+
internal inline fun ByteArray.unpackLEShort(offset: Int): Short = B0(this[offset]).toLEShort(
126+
this[offset + 1],
127+
)
128+
129+
internal inline fun ByteArray.unpackBEInt(offset: Int): Int = B0(this[offset]).toBEInt(
130+
this[offset + 1],
131+
this[offset + 2],
132+
this[offset + 3],
133+
)
134+
135+
internal inline fun ByteArray.unpackLEInt(offset: Int): Int = B0(this[offset]).toLEInt(
136+
this[offset + 1],
137+
this[offset + 2],
138+
this[offset + 3],
139+
)
140+
141+
internal inline fun ByteArray.unpackBELong(offset: Int): Long = B0(this[offset]).toBELong(
142+
this[offset + 1],
143+
this[offset + 2],
144+
this[offset + 3],
145+
this[offset + 4],
146+
this[offset + 5],
147+
this[offset + 6],
148+
this[offset + 7],
149+
)
150+
151+
internal inline fun ByteArray.unpackLELong(offset: Int): Long = B0(this[offset]).toLELong(
152+
this[offset + 1],
153+
this[offset + 2],
154+
this[offset + 3],
155+
this[offset + 4],
156+
this[offset + 5],
157+
this[offset + 6],
158+
this[offset + 7],
159+
)
160+
121161
internal inline fun ByteArray.packNumberAllElsePartial(
122162
destOffset: Int,
123163
sourceIndexStart: Int,
@@ -158,16 +198,17 @@ internal inline fun ByteArray.packArray(
158198
}
159199

160200
internal inline fun <Dest: Any> Dest.packArray(
201+
source: ByteArray,
161202
destOffset: Int,
162203
sourceIndexStart: Int,
163204
sourceIndexEnd: Int,
164205
numberSizeBytes: Int,
165-
unpackNumber: (sourcePos: Int, destPos: Int) -> Unit,
206+
unpackNumber: ByteArray.(sourcePos: Int, destPos: Int) -> Unit,
166207
): Dest {
167208
var destPos = destOffset
168209
var sourcePos = sourceIndexStart
169210
while (sourcePos < sourceIndexEnd) {
170-
unpackNumber(sourcePos, destPos++)
211+
unpackNumber(source, sourcePos, destPos++)
171212
sourcePos += numberSizeBytes
172213
}
173214
return this

0 commit comments

Comments
 (0)