Repository: ktorio/ktor
File: ktor-http/ktor-http-cio/common/src/io/ktor/http/cio/internals/Chars.kt (Kotlin)

Code before:
        val v = get(pos).code.toLowerCase()
        hashCode = 31 * hashCode + v
    }

    return hashCode
}

internal fun CharSequence.equalsLowerCase(start: Int = 0, end: Int = length, other: CharSequence): Boolean {
    if (end - start != other.length) return false

    for (pos in start until end) {
        if (get(pos).code.toLowerCase() != other[pos - start].code.toLowerCase()) return false
    }

    return true
}

@Suppress("NOTHING_TO_INLINE")
private inline fun Int.toLowerCase() =
    if (this in 'A'.code..'Z'.code) 'a'.code + (this - 'A'.code) else this

internal val DefaultHttpMethods =
    AsciiCharTree.build(HttpMethod.DefaultMethods, { it.value.length }, { m, idx -> m.value[idx] })

@InternalAPI
public val HexTable: LongArray = (0..0xff).map { v ->
    when {
        v in 0x30..0x39 -> v - 0x30L
        v >= 'a'.code.toLong() && v <= 'f'.code.toLong() -> v - 'a'.code.toLong() + 10
        v >= 'A'.code.toLong() && v <= 'F'.code.toLong() -> v - 'A'.code.toLong() + 10
        else -> -1L
    }
}.toLongArray()

internal val HexLetterTable: ByteArray = (0..0xf).map {
    if (it < 0xa) (0x30 + it).toByte() else ('a' + it - 0x0a).code.toByte()
}.toByteArray()

@OptIn(InternalAPI::class)
internal fun CharSequence.parseHexLong(): Long {
    var result = 0L
    val table = HexTable
    for (i in indices) {
        val v = this[i].code and 0xffff
        val digit = if (v < 0xff) table[v] else -1L
        if (digit == -1L) hexNumberFormatException(this, i)
        result = (result shl 4) or digit
    }

    return result
}

Review comment (suggestion):
The HexTable is being exposed as a public API with @InternalAPI annotation. While this is necessary for the parseChunkSize function to access it, exposing internal lookup tables as public API increases the API surface area and maintenance burden. Consider keeping HexTable internal and moving the parseChunkSize function to the same file (Chars.kt) where HexTable is defined, or making parseChunkSize use a parameter to receive the table.

Code after:
        val v = get(pos).code.toLowerCase()
        hashCode = 31 * hashCode + v
    }

    return hashCode
}

internal fun CharSequence.equalsLowerCase(start: Int = 0, end: Int = length, other: CharSequence): Boolean {
    if (end - start != other.length) return false

    for (pos in start until end) {
        if (get(pos).code.toLowerCase() != other[pos - start].code.toLowerCase()) return false
    }

    return true
}

@Suppress("NOTHING_TO_INLINE")
private inline fun Int.toLowerCase() =
    if (this in 'A'.code..'Z'.code) 'a'.code + (this - 'A'.code) else this

internal val DefaultHttpMethods =
    AsciiCharTree.build(HttpMethod.DefaultMethods, { it.value.length }, { m, idx -> m.value[idx] })

internal val HexTable: LongArray = (0..0xff).map { v ->
    when {
        v in 0x30..0x39 -> v - 0x30L
        v >= 'a'.code.toLong() && v <= 'f'.code.toLong() -> v - 'a'.code.toLong() + 10
        v >= 'A'.code.toLong() && v <= 'F'.code.toLong() -> v - 'A'.code.toLong() + 10
        else -> -1L
    }
}.toLongArray()

internal val HexLetterTable: ByteArray = (0..0xf).map {
    if (it < 0xa) (0x30 + it).toByte() else ('a' + it - 0x0a).code.toByte()
}.toByteArray()

/**
 * Converts [CharSequence] representation in decimal format to [Long]
 *
 * [Report a problem](https://ktor.io/feedback/?fqname=io.ktor.http.cio.internals.parseDecLong)
 */
public fun CharSequence.parseDecLong(): Long {
    val length = length
    if (length > 19) numberFormatException(this)
    if (length == 19) return parseDecLongWithCheck()

    var result = 0L
    for (i in 0 until length) {
        val digit = this[i].code.toLong() - 0x30L
        if (digit < 0 || digit > 9) numberFormatException(this, i)