From 14bcf449cfb728f6eb2a67329e09040a9ebf28ed Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Tue, 15 Oct 2024 20:17:32 +0800 Subject: [PATCH 1/5] Migrating the Util.kt and KModifier to common --- .../com/squareup/kotlinpoet/KModifier.kt | 3 +- .../kotlin/com/squareup/kotlinpoet/Util.kt | 90 +++++++++---------- .../kotlin/com/squareup/kotlinpoet/Util.js.kt | 5 ++ .../com/squareup/kotlinpoet/Util.jvm.kt | 87 ++++++++++++++++++ .../com/squareup/kotlinpoet/Util.nonJvm.kt | 87 ++++++++++++++++++ .../com/squareup/kotlinpoet/Util.wasmJs.kt | 26 ++++++ 6 files changed, 249 insertions(+), 49 deletions(-) rename kotlinpoet/src/{jvmMain => commonMain}/kotlin/com/squareup/kotlinpoet/KModifier.kt (95%) rename kotlinpoet/src/{jvmMain => commonMain}/kotlin/com/squareup/kotlinpoet/Util.kt (83%) create mode 100644 kotlinpoet/src/jsMain/kotlin/com/squareup/kotlinpoet/Util.js.kt create mode 100644 kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt create mode 100644 kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt create mode 100644 kotlinpoet/src/wasmJsMain/kotlin/com/squareup/kotlinpoet/Util.wasmJs.kt diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/KModifier.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/KModifier.kt similarity index 95% rename from kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/KModifier.kt rename to kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/KModifier.kt index 81d03570f8..e0bdabd210 100644 --- a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/KModifier.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/KModifier.kt @@ -19,7 +19,6 @@ import com.squareup.kotlinpoet.KModifier.INTERNAL import com.squareup.kotlinpoet.KModifier.PRIVATE import com.squareup.kotlinpoet.KModifier.PROTECTED import com.squareup.kotlinpoet.KModifier.PUBLIC -import java.util.EnumSet public enum class KModifier( internal val keyword: String, @@ -89,4 +88,4 @@ public enum class KModifier( } } -internal val VISIBILITY_MODIFIERS: Set = EnumSet.of(PUBLIC, INTERNAL, PROTECTED, PRIVATE) +internal val VISIBILITY_MODIFIERS: Set = enumSetOf(PUBLIC, INTERNAL, PROTECTED, PRIVATE) diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt similarity index 83% rename from kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.kt rename to kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt index 53257db02a..0461a1a10e 100644 --- a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt @@ -15,23 +15,17 @@ */ package com.squareup.kotlinpoet -import com.squareup.kotlinpoet.CodeBlock.Companion.isPlaceholder -import java.util.Collections - internal object NullAppendable : Appendable { - override fun append(charSequence: CharSequence) = this - override fun append(charSequence: CharSequence, start: Int, end: Int) = this - override fun append(c: Char) = this + override fun append(value: CharSequence?) = this + override fun append(value: CharSequence?, startIndex: Int, endIndex: Int) = this + override fun append(value: Char) = this } -internal fun Map.toImmutableMap(): Map = - Collections.unmodifiableMap(LinkedHashMap(this)) +internal expect fun Map.toImmutableMap(): Map -internal fun Collection.toImmutableList(): List = - Collections.unmodifiableList(ArrayList(this)) +internal expect fun Collection.toImmutableList(): List -internal fun Collection.toImmutableSet(): Set = - Collections.unmodifiableSet(LinkedHashSet(this)) +internal expect fun Collection.toImmutableSet(): Set internal inline fun > Collection.toEnumSet(): Set = enumValues().filterTo(mutableSetOf(), this::contains) @@ -63,10 +57,14 @@ internal fun characterLiteralWithoutSingleQuotes(c: Char) = when { c == '\"' -> "\"" // \u0022: double quote (") c == '\'' -> "\\'" // \u0027: single quote (') c == '\\' -> "\\\\" // \u005c: backslash (\) - c.isIsoControl -> String.format("\\u%04x", c.code) + c.isIsoControl -> formatIsoControlCode(c.code) else -> c.toString() } +internal expect fun formatIsoControlCode(code: Int): String + +internal expect fun Int.toHexStr(): String + internal fun escapeCharacterLiterals(s: String) = buildString { for (c in s) append(characterLiteralWithoutSingleQuotes(c)) } @@ -138,34 +136,27 @@ internal fun stringLiteralWithQuotes( } } -internal fun CodeBlock.ensureEndsWithNewLine() = trimTrailingNewLine('\n') - -internal fun CodeBlock.trimTrailingNewLine(replaceWith: Char? = null) = if (isEmpty()) { - this -} else { - with(toBuilder()) { - val lastFormatPart = trim().formatParts.last() - if (lastFormatPart.isPlaceholder && args.isNotEmpty()) { - val lastArg = args.last() - if (lastArg is String) { - val trimmedArg = lastArg.trimEnd('\n') - args[args.size - 1] = if (replaceWith != null) { - trimmedArg + replaceWith - } else { - trimmedArg - } - } - } else { - formatParts[formatParts.lastIndexOf(lastFormatPart)] = lastFormatPart.trimEnd('\n') - if (replaceWith != null) { - formatParts += "$replaceWith" - } - } - return@with build() - } -} +// TODO Waiting for `CodeBlock` migration. +// internal fun CodeBlock.ensureEndsWithNewLine() + +// TODO Waiting for `CodeBlock` migration. +// internal fun CodeBlock.trimTrailingNewLine(replaceWith: Char? = null) -private val IDENTIFIER_REGEX = +/** + * Will crash if used `IDENTIFIER_REGEX_VALUE.toRegex()` directly in WasmJs: + * `PatternSyntaxException: No such character class`. + * + * It works in JS and JVM. + * + * For now: + * - Keep the use of `Regex` in JVM and JS. + * - And use `RegExp` directly in WasmJs for matching, + * using it in a similar way as in JS. + * + * See also: [KT-71003](https://youtrack.jetbrains.com/issue/KT-71003) + */ +internal const val IDENTIFIER_REGEX_VALUE = + // language=regexp ( "((\\p{gc=Lu}+|\\p{gc=Ll}+|\\p{gc=Lt}+|\\p{gc=Lm}+|\\p{gc=Lo}+|\\p{gc=Nl}+)+" + "\\d*" + @@ -173,9 +164,8 @@ private val IDENTIFIER_REGEX = "|" + "(`[^\n\r`]+`)" ) - .toRegex() -internal val String.isIdentifier get() = IDENTIFIER_REGEX.matches(this) +internal expect val String.isIdentifier: Boolean // https://kotlinlang.org/docs/reference/keyword-reference.html internal val KEYWORDS = setOf( @@ -317,7 +307,7 @@ internal fun String.escapeAsAlias(validate: Boolean = true): String { val newAlias = StringBuilder("") - if (!Character.isJavaIdentifierStart(first())) { + if (!first().isJavaIdentifierStart()) { newAlias.append('_') } @@ -327,8 +317,8 @@ internal fun String.escapeAsAlias(validate: Boolean = true): String { continue } - if (!Character.isJavaIdentifierPart(ch)) { - newAlias.append("_U").append(Integer.toHexString(ch.code).padStart(4, '0')) + if (!ch.isJavaIdentifierPart()) { + newAlias.append("_U").append(ch.code.toHexStr().padStart(4, '0')) continue } @@ -348,8 +338,8 @@ private fun String.escapeIfAllCharactersAreUnderscore() = if (allCharactersAreUn private fun String.escapeIfNotJavaIdentifier(): String { return if (( - !Character.isJavaIdentifierStart(first()) || - drop(1).any { !Character.isJavaIdentifierPart(it) } + !first().isJavaIdentifierStart() || + drop(1).any { !it.isJavaIdentifierPart() } ) && !alreadyEscaped() ) { @@ -362,3 +352,9 @@ private fun String.escapeIfNotJavaIdentifier(): String { internal fun String.escapeSegmentsIfNecessary(delimiter: Char = '.') = split(delimiter) .filter { it.isNotEmpty() } .joinToString(delimiter.toString()) { it.escapeIfNecessary() } + +internal expect inline fun > enumSetOf(vararg values: E): MutableSet + +internal expect fun Char.isJavaIdentifierStart(): Boolean + +internal expect fun Char.isJavaIdentifierPart(): Boolean diff --git a/kotlinpoet/src/jsMain/kotlin/com/squareup/kotlinpoet/Util.js.kt b/kotlinpoet/src/jsMain/kotlin/com/squareup/kotlinpoet/Util.js.kt new file mode 100644 index 0000000000..f02ea37be0 --- /dev/null +++ b/kotlinpoet/src/jsMain/kotlin/com/squareup/kotlinpoet/Util.js.kt @@ -0,0 +1,5 @@ +package com.squareup.kotlinpoet + +private val IDENTIFIER_REGEX = IDENTIFIER_REGEX_VALUE.toRegex() + +internal actual val String.isIdentifier: Boolean get() = IDENTIFIER_REGEX.matches(this) diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt new file mode 100644 index 0000000000..3a3f335322 --- /dev/null +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * 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 + * + * https://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. + */ +package com.squareup.kotlinpoet + +import com.squareup.kotlinpoet.CodeBlock.Companion.isPlaceholder +import java.util.Collections +import java.util.EnumSet + +internal actual fun Map.toImmutableMap(): Map = + Collections.unmodifiableMap(LinkedHashMap(this)) + +internal actual fun Collection.toImmutableList(): List = + Collections.unmodifiableList(ArrayList(this)) + +internal actual fun Collection.toImmutableSet(): Set = + Collections.unmodifiableSet(LinkedHashSet(this)) + +internal actual fun formatIsoControlCode(code: Int): String = + String.format("\\u%04x", code) + +internal actual fun Int.toHexStr(): String = + Integer.toHexString(this) + +// TODO Waiting for `CodeBlock` migration. +internal fun CodeBlock.ensureEndsWithNewLine() = trimTrailingNewLine('\n') + +// TODO Waiting for `CodeBlock` migration. +internal fun CodeBlock.trimTrailingNewLine(replaceWith: Char? = null) = if (isEmpty()) { + this +} else { + with(toBuilder()) { + val lastFormatPart = trim().formatParts.last() + if (lastFormatPart.isPlaceholder && args.isNotEmpty()) { + val lastArg = args.last() + if (lastArg is String) { + val trimmedArg = lastArg.trimEnd('\n') + args[args.size - 1] = if (replaceWith != null) { + trimmedArg + replaceWith + } else { + trimmedArg + } + } + } else { + formatParts[formatParts.lastIndexOf(lastFormatPart)] = lastFormatPart.trimEnd('\n') + if (replaceWith != null) { + formatParts += "$replaceWith" + } + } + return@with build() + } +} + +private val IDENTIFIER_REGEX = IDENTIFIER_REGEX_VALUE.toRegex() + +internal actual val String.isIdentifier: Boolean + get() = IDENTIFIER_REGEX.matches(this) + +internal actual inline fun > enumSetOf(vararg values: E): MutableSet { + return when(values.size) { + 0 -> EnumSet.noneOf(E::class.java) + 1 -> EnumSet.of(values[0]) + 2 -> EnumSet.of(values[0], values[1]) + 3 -> EnumSet.of(values[0], values[1], values[2]) + 4 -> EnumSet.of(values[0], values[1], values[2], values[3]) + 5 -> EnumSet.of(values[0], values[1], values[2], values[3], values[4]) + else -> EnumSet.copyOf(values.toSet()) + } +} + +internal actual fun Char.isJavaIdentifierStart(): Boolean = + Character.isJavaIdentifierStart(this) + +internal actual fun Char.isJavaIdentifierPart(): Boolean = + Character.isJavaIdentifierPart(this) diff --git a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt new file mode 100644 index 0000000000..b417b6f84d --- /dev/null +++ b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt @@ -0,0 +1,87 @@ +package com.squareup.kotlinpoet + +internal actual fun Map.toImmutableMap(): Map = + toMap() + +internal actual fun Collection.toImmutableList(): List = + toList() + +internal actual fun Collection.toImmutableSet(): Set = + toSet() + +internal actual fun formatIsoControlCode(code: Int): String { + return buildString(6) { + append("\\u") + appendFormat04x(code) + } +} + +@OptIn(ExperimentalStdlibApi::class) +private val HexFormatWithoutLeadingZeros = HexFormat { + number { + removeLeadingZeros = true + } +} + +@OptIn(ExperimentalStdlibApi::class) +internal fun Appendable.appendFormat04x(code: Int) { + val hex = code.toHexString(HexFormatWithoutLeadingZeros) + if (hex.length < 4) { + repeat(4 - hex.length) { append('0') } + } + append(hex) +} + +@OptIn(ExperimentalStdlibApi::class) +internal actual fun Int.toHexStr(): String = + toHexString(HexFormatWithoutLeadingZeros) + +internal actual inline fun > enumSetOf(vararg values: E): MutableSet = + values.toMutableSet() + +internal actual fun Char.isJavaIdentifierStart(): Boolean { + return isLetter() || + this in CharCategory.LETTER_NUMBER || + this == '$' || + this == '_' +} + + +internal actual fun Char.isJavaIdentifierPart(): Boolean { + // TODO + // A character may be part of a Java identifier if any of the following conditions are true: + // - it is a letter + // - it is a currency symbol (such as '$') + // - it is a connecting punctuation character (such as '_') + // - it is a digit + // - it is a numeric letter (such as a Roman numeral character) + // - it is a combining mark + // - it is a non-spacing mark + // isIdentifierIgnorable returns true for the character. + // Also missing here: + // - a combining mark + return isLetter() || + isDigit() || + this in CharCategory.LETTER_NUMBER || + this in CharCategory.NON_SPACING_MARK || + this == '_' || + this == '$' || + isIdentifierIgnorable() + // +} + +internal fun Char.isIdentifierIgnorable(): Boolean { + // The following Unicode characters are ignorable in a Java identifier or a Unicode identifier: + // - ISO control characters that are not whitespace + // - '\u0000' through '\u0008' + // - '\u000E' through '\u001B' + // - '\u007F' through '\u009F' + // - all characters that have the FORMAT general category value + return ( + isISOControl() && ( + this in '\u0000'..'\u0008' || + this in '\u000E'..'\u001B' || + this in '\u007F'..'\u009F' + ) + ) || this in CharCategory.FORMAT +} diff --git a/kotlinpoet/src/wasmJsMain/kotlin/com/squareup/kotlinpoet/Util.wasmJs.kt b/kotlinpoet/src/wasmJsMain/kotlin/com/squareup/kotlinpoet/Util.wasmJs.kt new file mode 100644 index 0000000000..fa425b7a5a --- /dev/null +++ b/kotlinpoet/src/wasmJsMain/kotlin/com/squareup/kotlinpoet/Util.wasmJs.kt @@ -0,0 +1,26 @@ +package com.squareup.kotlinpoet + +internal actual val String.isIdentifier: Boolean + get() { + val regExp = RegExp(IDENTIFIER_REGEX_VALUE, "gu") + regExp.reset() + + val match = regExp.exec(this) ?: return false + return match.index == 0 && regExp.lastIndex == length + } + +internal external interface RegExpMatch { + val index: Int + val length: Int +} + +internal external class RegExp(pattern: String, flags: String? = definedExternally) : JsAny { + fun exec(str: String): RegExpMatch? + override fun toString(): String + + var lastIndex: Int +} + +internal fun RegExp.reset() { + lastIndex = 0 +} From 81046c1a1d857fbfe7f39e537afd96fe4499889b Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Tue, 15 Oct 2024 21:22:43 +0800 Subject: [PATCH 2/5] Apply spotlessApply to fix Copyright --- .../kotlin/com/squareup/kotlinpoet/Util.js.kt | 15 ++++++++++ .../com/squareup/kotlinpoet/Util.jvm.kt | 2 +- .../com/squareup/kotlinpoet/Util.nonJvm.kt | 30 ++++++++++++++----- .../com/squareup/kotlinpoet/Util.wasmJs.kt | 15 ++++++++++ 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/kotlinpoet/src/jsMain/kotlin/com/squareup/kotlinpoet/Util.js.kt b/kotlinpoet/src/jsMain/kotlin/com/squareup/kotlinpoet/Util.js.kt index f02ea37be0..b2cc2dd49d 100644 --- a/kotlinpoet/src/jsMain/kotlin/com/squareup/kotlinpoet/Util.js.kt +++ b/kotlinpoet/src/jsMain/kotlin/com/squareup/kotlinpoet/Util.js.kt @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * 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 + * + * https://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. + */ package com.squareup.kotlinpoet private val IDENTIFIER_REGEX = IDENTIFIER_REGEX_VALUE.toRegex() diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt index 3a3f335322..328f8caaf6 100644 --- a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt @@ -69,7 +69,7 @@ internal actual val String.isIdentifier: Boolean get() = IDENTIFIER_REGEX.matches(this) internal actual inline fun > enumSetOf(vararg values: E): MutableSet { - return when(values.size) { + return when (values.size) { 0 -> EnumSet.noneOf(E::class.java) 1 -> EnumSet.of(values[0]) 2 -> EnumSet.of(values[0], values[1]) diff --git a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt index b417b6f84d..1bde211955 100644 --- a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt +++ b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * 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 + * + * https://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. + */ package com.squareup.kotlinpoet internal actual fun Map.toImmutableMap(): Map = @@ -46,7 +61,6 @@ internal actual fun Char.isJavaIdentifierStart(): Boolean { this == '_' } - internal actual fun Char.isJavaIdentifierPart(): Boolean { // TODO // A character may be part of a Java identifier if any of the following conditions are true: @@ -71,15 +85,15 @@ internal actual fun Char.isJavaIdentifierPart(): Boolean { } internal fun Char.isIdentifierIgnorable(): Boolean { - // The following Unicode characters are ignorable in a Java identifier or a Unicode identifier: - // - ISO control characters that are not whitespace - // - '\u0000' through '\u0008' - // - '\u000E' through '\u001B' - // - '\u007F' through '\u009F' - // - all characters that have the FORMAT general category value + // The following Unicode characters are ignorable in a Java identifier or a Unicode identifier: + // - ISO control characters that are not whitespace + // - '\u0000' through '\u0008' + // - '\u000E' through '\u001B' + // - '\u007F' through '\u009F' + // - all characters that have the FORMAT general category value return ( isISOControl() && ( - this in '\u0000'..'\u0008' || + this in '\u0000'..'\u0008' || this in '\u000E'..'\u001B' || this in '\u007F'..'\u009F' ) diff --git a/kotlinpoet/src/wasmJsMain/kotlin/com/squareup/kotlinpoet/Util.wasmJs.kt b/kotlinpoet/src/wasmJsMain/kotlin/com/squareup/kotlinpoet/Util.wasmJs.kt index fa425b7a5a..81e2c2966b 100644 --- a/kotlinpoet/src/wasmJsMain/kotlin/com/squareup/kotlinpoet/Util.wasmJs.kt +++ b/kotlinpoet/src/wasmJsMain/kotlin/com/squareup/kotlinpoet/Util.wasmJs.kt @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * 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 + * + * https://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. + */ package com.squareup.kotlinpoet internal actual val String.isIdentifier: Boolean From d7c57e4058efa53c4194db2384402f2653d0cb5f Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Tue, 15 Oct 2024 22:20:05 +0800 Subject: [PATCH 3/5] Remove `enumSetOf` and use a regular set --- .../kotlin/com/squareup/kotlinpoet/KModifier.kt | 2 +- .../kotlin/com/squareup/kotlinpoet/Util.kt | 2 -- .../kotlin/com/squareup/kotlinpoet/Util.jvm.kt | 12 ------------ .../kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt | 3 --- 4 files changed, 1 insertion(+), 18 deletions(-) diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/KModifier.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/KModifier.kt index e0bdabd210..0c0ad73fd9 100644 --- a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/KModifier.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/KModifier.kt @@ -88,4 +88,4 @@ public enum class KModifier( } } -internal val VISIBILITY_MODIFIERS: Set = enumSetOf(PUBLIC, INTERNAL, PROTECTED, PRIVATE) +internal val VISIBILITY_MODIFIERS: Set = setOf(PUBLIC, INTERNAL, PROTECTED, PRIVATE) diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt index 0461a1a10e..47eeb40d7b 100644 --- a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt @@ -353,8 +353,6 @@ internal fun String.escapeSegmentsIfNecessary(delimiter: Char = '.') = split(del .filter { it.isNotEmpty() } .joinToString(delimiter.toString()) { it.escapeIfNecessary() } -internal expect inline fun > enumSetOf(vararg values: E): MutableSet - internal expect fun Char.isJavaIdentifierStart(): Boolean internal expect fun Char.isJavaIdentifierPart(): Boolean diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt index 328f8caaf6..bce771de35 100644 --- a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt @@ -68,18 +68,6 @@ private val IDENTIFIER_REGEX = IDENTIFIER_REGEX_VALUE.toRegex() internal actual val String.isIdentifier: Boolean get() = IDENTIFIER_REGEX.matches(this) -internal actual inline fun > enumSetOf(vararg values: E): MutableSet { - return when (values.size) { - 0 -> EnumSet.noneOf(E::class.java) - 1 -> EnumSet.of(values[0]) - 2 -> EnumSet.of(values[0], values[1]) - 3 -> EnumSet.of(values[0], values[1], values[2]) - 4 -> EnumSet.of(values[0], values[1], values[2], values[3]) - 5 -> EnumSet.of(values[0], values[1], values[2], values[3], values[4]) - else -> EnumSet.copyOf(values.toSet()) - } -} - internal actual fun Char.isJavaIdentifierStart(): Boolean = Character.isJavaIdentifierStart(this) diff --git a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt index 1bde211955..8d4352629b 100644 --- a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt +++ b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt @@ -51,9 +51,6 @@ internal fun Appendable.appendFormat04x(code: Int) { internal actual fun Int.toHexStr(): String = toHexString(HexFormatWithoutLeadingZeros) -internal actual inline fun > enumSetOf(vararg values: E): MutableSet = - values.toMutableSet() - internal actual fun Char.isJavaIdentifierStart(): Boolean { return isLetter() || this in CharCategory.LETTER_NUMBER || From 0920d3e8a10b16f7b71484c7c642a48019ce2a7d Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Tue, 15 Oct 2024 22:43:29 +0800 Subject: [PATCH 4/5] Simplify hex operations in Util, keep them only in common --- .../kotlin/com/squareup/kotlinpoet/Util.kt | 6 +++-- .../com/squareup/kotlinpoet/Util.jvm.kt | 7 ----- .../com/squareup/kotlinpoet/Util.nonJvm.kt | 26 ------------------- 3 files changed, 4 insertions(+), 35 deletions(-) diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt index 47eeb40d7b..e500d66d20 100644 --- a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt @@ -61,9 +61,11 @@ internal fun characterLiteralWithoutSingleQuotes(c: Char) = when { else -> c.toString() } -internal expect fun formatIsoControlCode(code: Int): String +internal fun formatIsoControlCode(code: Int): String = + "\\u${code.toHexStr().padStart(4, '0')}" -internal expect fun Int.toHexStr(): String +internal fun Int.toHexStr(): String = + toUInt().toString(16) internal fun escapeCharacterLiterals(s: String) = buildString { for (c in s) append(characterLiteralWithoutSingleQuotes(c)) diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt index bce771de35..aa3cf8a2ca 100644 --- a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Util.jvm.kt @@ -17,7 +17,6 @@ package com.squareup.kotlinpoet import com.squareup.kotlinpoet.CodeBlock.Companion.isPlaceholder import java.util.Collections -import java.util.EnumSet internal actual fun Map.toImmutableMap(): Map = Collections.unmodifiableMap(LinkedHashMap(this)) @@ -28,12 +27,6 @@ internal actual fun Collection.toImmutableList(): List = internal actual fun Collection.toImmutableSet(): Set = Collections.unmodifiableSet(LinkedHashSet(this)) -internal actual fun formatIsoControlCode(code: Int): String = - String.format("\\u%04x", code) - -internal actual fun Int.toHexStr(): String = - Integer.toHexString(this) - // TODO Waiting for `CodeBlock` migration. internal fun CodeBlock.ensureEndsWithNewLine() = trimTrailingNewLine('\n') diff --git a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt index 8d4352629b..453a929352 100644 --- a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt +++ b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt @@ -24,32 +24,6 @@ internal actual fun Collection.toImmutableList(): List = internal actual fun Collection.toImmutableSet(): Set = toSet() -internal actual fun formatIsoControlCode(code: Int): String { - return buildString(6) { - append("\\u") - appendFormat04x(code) - } -} - -@OptIn(ExperimentalStdlibApi::class) -private val HexFormatWithoutLeadingZeros = HexFormat { - number { - removeLeadingZeros = true - } -} - -@OptIn(ExperimentalStdlibApi::class) -internal fun Appendable.appendFormat04x(code: Int) { - val hex = code.toHexString(HexFormatWithoutLeadingZeros) - if (hex.length < 4) { - repeat(4 - hex.length) { append('0') } - } - append(hex) -} - -@OptIn(ExperimentalStdlibApi::class) -internal actual fun Int.toHexStr(): String = - toHexString(HexFormatWithoutLeadingZeros) internal actual fun Char.isJavaIdentifierStart(): Boolean { return isLetter() || From 523b0125063e39bba94a2ff23be927b8aa037bcb Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Tue, 15 Oct 2024 22:47:48 +0800 Subject: [PATCH 5/5] Apply spotlessApply to fix blank line --- .../src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt index 453a929352..7e52926fac 100644 --- a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt +++ b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/Util.nonJvm.kt @@ -24,7 +24,6 @@ internal actual fun Collection.toImmutableList(): List = internal actual fun Collection.toImmutableSet(): Set = toSet() - internal actual fun Char.isJavaIdentifierStart(): Boolean { return isLetter() || this in CharCategory.LETTER_NUMBER ||