Code cleanup

This commit is contained in:
Robinson 2023-01-25 01:37:04 +01:00
parent 2cdf516040
commit 0e9a694e83
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
4 changed files with 23 additions and 33 deletions

View File

@ -1,3 +1,19 @@
/*
* Copyright 2023 dorkbox, llc
*
* 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
*
* http://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.
*/
/*
* Copyright 2011 Google Inc.
* Copyright 2018 Andreas Schildbach
@ -151,9 +167,9 @@ fun String.decodeBase58(): ByteArray {
// Convert the base58-encoded ASCII chars to a base58 byte sequence (base58 digits).
val input58 = ByteArray(length)
for (i in 0 until length) {
for (i in indices) {
val c = this[i]
val digit = if (c.code < 128) Base58.alphabetIndices[c.toInt()] else -1
val digit = if (c.code < 128) Base58.alphabetIndices[c.code] else -1
if (digit < 0) {
throw NumberFormatException("Illegal character $c at position $i")
}

View File

@ -535,34 +535,7 @@ class ByteArrayBuffer {
*/
private fun readVarInt(optimizePositive: Boolean): Int {
val buffer = bytes
if (capacity - position < 5) {
return readInt_slow(optimizePositive)
}
var b = buffer[position++].toInt()
var result = b and 0x7F
if (b and 0x80 != 0) {
b = buffer[position++].toInt()
result = result or (b and 0x7F shl 7)
if (b and 0x80 != 0) {
b = buffer[position++].toInt()
result = result or (b and 0x7F shl 14)
if (b and 0x80 != 0) {
b = buffer[position++].toInt()
result = result or (b and 0x7F shl 21)
if (b and 0x80 != 0) {
b = buffer[position++].toInt()
result = result or (b and 0x7F shl 28)
}
}
}
}
return if (optimizePositive) result else result ushr 1 xor -(result and 1)
}
private fun readInt_slow(optimizePositive: Boolean): Int {
val buffer = bytes
// The buffer is guaranteed to have at least 1 byte.
var b = buffer[position++].toInt()
var result = b and 0x7F
if (b and 0x80 != 0) {
@ -1056,7 +1029,7 @@ class ByteArrayBuffer {
val n = capacity
while (i < n) {
chars[ii] = buffer[i].toChar()
chars[ii] = buffer[i].toInt().toChar()
i++
ii++
}

View File

@ -42,6 +42,7 @@ import java.io.InputStream
*
* Modified from KRYO ByteBufferInput to use ByteBuf instead of ByteBuffer.
*/
@Suppress("NAME_SHADOWING")
class ByteBufInput : Input {
companion object {
/**

View File

@ -63,7 +63,7 @@ import java.io.OutputStream
*
* Modified from KRYO to use ByteBuf.
*/
@Suppress("MemberVisibilityCanBePrivate")
@Suppress("MemberVisibilityCanBePrivate", "NAME_SHADOWING")
class ByteBufOutput : Output {
companion object {
/**
@ -806,7 +806,7 @@ class ByteBufOutput : Output {
require(count shl 1)
val n = offset + count
while (offset < n) {
val value = array[offset].toInt()
val value = array[offset].code
byteBuf.writeByte(value.toByte().toInt())
byteBuf.writeByte((value ushr 8).toByte().toInt())
offset++