Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating the Util.kt and KModifier from JVM to common #2006

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ForteScarlet
Copy link
Contributor

Part of #304, #1959

Migrating Util.kt and KModifier from JVM to common.

  • Non-JVM platform implementations have some questionable implementations (They mark the TODO).
  • There is a known issue (KT-71003) regarding the use of Kotlin Regex in WasmJS.
  • there are some functions that need to wait for other types of migration.

On the JVM, however, the effect is the same before and after migration.

Let me know if there's anything I need to do, thanks.
If these changes are not reviewed until after 2.0, you can always tell me, convert it to draft, or close it~

Comment on lines 71 to 81
internal actual inline fun <reified E : Enum<E>> enumSetOf(vararg values: E): MutableSet<E> {
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())
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to do this. Just use a regular set and mutable set in common code. We're no longer gaining any advantage of EnumSet having to go through this allocation-heavy path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I get it.

Comment on lines 27 to 48
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)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it could be simplified to toString(16).padStart(4, '0') and placed directly in common code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! It seems so. Will simplify it later

Comment on lines 51 to 52
internal actual fun Int.toHexStr(): String =
toHexString(HexFormatWithoutLeadingZeros)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just toString(16)? If so, we can use that directly in common code (without relying on unstable APIs, as well).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it seems so. I'll simplify it

Comment on lines +18 to +25
internal actual fun <K, V> Map<K, V>.toImmutableMap(): Map<K, V> =
toMap()

internal actual fun <T> Collection<T>.toImmutableList(): List<T> =
toList()

internal actual fun <T> Collection<T>.toImmutableSet(): Set<T> =
toSet()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're not going to put these in an unmodifiable wrapper on other targets, do we still need to do it on JVM? Are we really worried about Java callers mutating things? I don't think I am.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original implementation was copied when we forked from JavaPoet, for what it's worth.

Copy link
Contributor Author

@ForteScarlet ForteScarlet Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you have a point. If we no longer care about calls that mutate things on the JVM, do we keep toImmutable* in common and have its result just return a to* (like toSet), or do we simply remove them and use to* instead where appropriate?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's defending against something that is not a real problem in practice, and I would eliminate the concept entirely and use Kotlin's collection APIs directly everywhere. This should probably be done in its own PR rather than cluttering this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants