Skip to content

Commit

Permalink
Cleanup code: remove redundant inline and remove legacy OptIns
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed May 1, 2023
1 parent dd269d0 commit 5fa3c52
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 41 deletions.
5 changes: 1 addition & 4 deletions yamlkt/src/commonMain/kotlin/net.mamoe.yamlkt/YamlElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public fun YamlElement.asLiteralOrNull(): YamlLiteral? {
/**
* @return [this] as a [YamlLiteral], `null` otherwise
*/
@OptIn(ExperimentalContracts::class)
public inline fun YamlElement.asLiteral(
crossinline lazyMessage: (YamlElement) -> String = { "$this is not a YamlLiteral" }
): YamlLiteral {
Expand All @@ -125,7 +124,6 @@ public fun YamlElement.asPrimitiveOrNull(): YamlPrimitive? {
/**
* @return [this] as a [YamlPrimitive], `null` otherwise
*/
@OptIn(ExperimentalContracts::class)
public inline fun YamlElement.asPrimitive(
crossinline lazyMessage: (YamlElement) -> String = { "$this is not a YamlPrimitive" }
): YamlPrimitive {
Expand All @@ -135,7 +133,6 @@ public inline fun YamlElement.asPrimitive(
/**
* @return [YamlLiteral.content] if this is a [YamlLiteral], `null` otherwise
*/
@OptIn(ExperimentalContracts::class)
public val YamlElement.literalContentOrNull: String?
get() = (this as? YamlLiteral)?.content

Expand Down Expand Up @@ -515,7 +512,7 @@ internal fun Any?.asYamlElementOrNullImpl(): YamlElement? = when (this) {
else -> null
}

@Suppress("UNCHECKED_CAST", "IMPLICIT_CAST_TO_ANY")
@Suppress("UNCHECKED_CAST")
@PublishedApi
internal fun <R : Any> YamlElement.smartCastPrimitive(clazz: KClass<R>): R { // reduce inlined bytecode size
require(this is YamlPrimitive) { "the element is not YamlPrimitive" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import kotlin.jvm.JvmName
import kotlin.math.pow

// No generic type: Long will be transformed to primitive `long` on JVM, best performance
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
// TODO(#27): This breaks the compiler
// @kotlin.internal.InlineOnly
private inline fun CharSequence.foldFromRightOffsetIndexed(
private fun CharSequence.foldFromRightOffsetIndexed(
offset: Int,
initial: Long,
operation: (index: Int, acc: Long, Char) -> Long
Expand All @@ -29,9 +26,6 @@ private inline fun CharSequence.foldFromRightOffsetIndexed(
}

// No generic type: Long will be transformed to primitive `long` on JVM, best performance
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
// TODO(#27): This breaks the compiler
// @kotlin.internal.InlineOnly
private inline fun CharArray.foldFromRightOffsetIndexed(
offset: Int,
length: Int,
Expand Down Expand Up @@ -91,7 +85,6 @@ internal object HexConverter {
* The converter that converts binary [String]s from or to numbers
*/
internal object BinaryConverter {
@OptIn(ExperimentalUnsignedTypes::class)
private val BINARY_POW_TABLE: Array<Long> = Array(Long.SIZE_BITS) { 2.0.pow(it).toULong().toLong() }

fun binToLong(value: String, offset: Int): Long {
Expand Down Expand Up @@ -124,14 +117,12 @@ internal fun Long.limitToInt(): Int {
}

// this is very frequently used
@Suppress("NOTHING_TO_INLINE")
internal inline fun String.optimizeNull(): String? = when (this) {
internal fun String.optimizeNull(): String? = when (this) {
"~", "null", "NULL" -> null
else -> this
}


@Suppress("NOTHING_TO_INLINE")
internal inline fun Any.classSimpleName(): String? {
internal fun Any.classSimpleName(): String? {
return this::class.simpleName
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ internal interface IYamlDynamicSerializer

internal expect fun IYamlDynamicSerializer.serializeImpl(encoder: Encoder, value: Any)

@Suppress("RemoveExplicitTypeArguments") // compiler bug
internal object AnyTypedArraySerializer :
KSerializer<Array<Any?>> by ArraySerializer<Any, Any?>(YamlNullableDynamicSerializer)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@file:JvmMultifileClass
@file:JvmName("YamlUtils")
@file:Suppress("NOTHING_TO_INLINE")

package net.mamoe.yamlkt.internal

Expand Down Expand Up @@ -486,7 +485,7 @@ private tailrec fun TokenStream.runNewLineSkippingAndEscapingForUnquoted(
}
}

internal inline fun TokenStream.ensureNotEOF() {
internal fun TokenStream.ensureNotEOF() {
if (endOfInput) throw contextualDecodingException("Unexpected EOF")
}

Expand Down Expand Up @@ -601,7 +600,7 @@ internal inline fun <R> TokenStream.peekNext(block: (ch: Char) -> R?): R? {
return source[cur + 1].let(block)
}

internal inline fun Char.isHexDigit(): Boolean = this in '0'..'9' || this in 'a'..'f' || this in 'A'..'F'
internal fun Char.isHexDigit(): Boolean = this in '0'..'9' || this in 'a'..'f' || this in 'A'..'F'

internal fun String.toEscapedString(
buf: StringBufHolder,
Expand Down Expand Up @@ -641,7 +640,7 @@ private fun String.toDoubleQuotedString(buf: StringBufHolder): String = with(buf
takeStringBuf()
}

private inline infix fun Int.hasFlag(flag: Int): Boolean = this and flag != 0
private infix fun Int.hasFlag(flag: Int): Boolean = this and flag != 0

private const val SINGLE = /* */ 0b001
private const val UNQUOTED = /* */ 0b010
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.modules.SerializersModule

@OptIn(ExperimentalUnsignedTypes::class)
internal class InlineEncoder(
private val writer: YamlWriter,
private val encoder: Encoder,
Expand All @@ -18,7 +17,6 @@ internal class InlineEncoder(
override fun encodeShort(value: Short) = writer.write(value.toUShort().toString())
}

@OptIn(ExperimentalUnsignedTypes::class)
internal class InlineDecoder(
private val delegate: YamlDecoder,
) : Decoder by delegate {
Expand All @@ -35,7 +33,6 @@ internal class InlineDecoder(
delegate.run { decodeString().withIntegerValue("ULong", null, -1).toULong().toLong() }
}

@OptIn(ExperimentalUnsignedTypes::class)
internal class InlineElementDecoder(
private val yamlDecoder: YamlDecoder,
private val compositeDecoder: YamlDecoder.AbstractDecoder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ internal class YamlDecoder(
}
}

internal inline fun Int.isOdd(): Boolean {
internal fun Int.isOdd(): Boolean {
return this and 0b1 != 0
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
@file:JvmMultifileClass
@file:JvmName("YamlUtils")

@file:Suppress("NOTHING_TO_INLINE")

package net.mamoe.yamlkt.internal

import kotlin.jvm.JvmField
Expand Down Expand Up @@ -32,44 +30,44 @@ internal class YamlWriter(
level--
}

inline fun append(char: Char) {
fun append(char: Char) {
if (char == '\n') {
currentIndent = 0
} else currentIndent++
output.append(char)
}

inline fun append(char: String) { // String doesn't matter indenting
fun append(char: String) { // String doesn't matter indenting
currentIndent += char.length
output.append(char)
}

inline operator fun String.unaryPlus() {
operator fun String.unaryPlus() {
write(this)
}

inline operator fun Char.unaryPlus() {
operator fun Char.unaryPlus() {
write(this)
}

@JvmField
internal val escapeBuf: StringBufHolder = StringBufHolder()
}

internal inline fun YamlWriter.write(char: Char) {
internal fun YamlWriter.write(char: Char) {
append(char)
}

internal inline fun YamlWriter.writeln(char: Char) {
internal fun YamlWriter.writeln(char: Char) {
write(char)
writeln()
}

internal inline fun YamlWriter.writeln() {
internal fun YamlWriter.writeln() {
write('\n')
}

internal inline fun YamlWriter.write(chars: String) {
internal fun YamlWriter.write(chars: String) {
append(chars)
}

Expand All @@ -84,17 +82,17 @@ internal inline fun YamlWriter.writeLineIndented(line: YamlWriter.() -> Unit) {
writeln()
}

internal inline fun YamlWriter.writeln(chars: String) {
internal fun YamlWriter.writeln(chars: String) {
write(chars)
writeln()
}

internal inline fun YamlWriter.writelnIndented(chars: String) {
internal fun YamlWriter.writelnIndented(chars: String) {
writeIndented(chars)
writeln()
}

internal inline fun YamlWriter.writeIndented(chars: String) {
internal fun YamlWriter.writeIndented(chars: String) {
writeIndent()
write(chars)
}
Expand All @@ -115,18 +113,18 @@ internal fun YamlWriter.writeIndentSmart() {
}
}

internal inline fun YamlWriter.writelnIndented(char: Char) {
internal fun YamlWriter.writelnIndented(char: Char) {
writeIndented(char)
writeln()
}

internal inline fun YamlWriter.writeIndent() {
internal fun YamlWriter.writeIndent() {
repeat(level) {
write(INDENT_STRING)
}
}

internal inline fun YamlWriter.writeIndented(char: Char) {
internal fun YamlWriter.writeIndented(char: Char) {
writeIndent()
write(char)
}

0 comments on commit 5fa3c52

Please sign in to comment.