-
Notifications
You must be signed in to change notification settings - Fork 292
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
Migrate NameAllocator from JVM to common #2024
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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 kotlin.jvm.JvmInline | ||
|
||
@JvmInline | ||
internal value class CodePoint(val code: Int) | ||
|
||
internal expect fun String.codePointAt(index: Int): CodePoint | ||
|
||
internal expect fun CodePoint.isLowerCase(): Boolean | ||
internal expect fun CodePoint.isUpperCase(): Boolean | ||
|
||
internal expect fun CodePoint.isJavaIdentifierStart(): Boolean | ||
internal expect fun CodePoint.isJavaIdentifierPart(): Boolean | ||
|
||
internal expect fun CodePoint.charCount(): Int | ||
|
||
internal expect fun StringBuilder.appendCodePoint(codePoint: CodePoint): StringBuilder |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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 String.codePointAt(index: Int): CodePoint { | ||
val code = jsCodePointAt(this, index) | ||
return CodePoint(code) | ||
} | ||
|
||
internal actual fun CodePoint.isLowerCase(): Boolean { | ||
// TODO Will there be a problem? Is there a better way? | ||
val str = jsFromCodePoint(this.code) | ||
|
||
if (str.length != 1) { | ||
return false | ||
} | ||
|
||
return str.first().isLowerCase() | ||
} | ||
|
||
internal actual fun CodePoint.isUpperCase(): Boolean { | ||
// TODO Will there be a problem? Is there a better way? | ||
val str = jsFromCodePoint(this.code) | ||
|
||
if (str.length != 1) { | ||
return false | ||
} | ||
|
||
return str.first().isUpperCase() | ||
} | ||
|
||
@Suppress("unused") | ||
private fun jsCodePointAt(str: String, index: Int): Int = | ||
js("str.codePointAt(index)").unsafeCast<Int>() | ||
|
||
@Suppress("unused") | ||
private fun jsFromCodePoint(code: Int): String = | ||
js("String.fromCodePoint(code)").toString() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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 kotlin.text.codePointAt as codePointAtKt | ||
|
||
internal actual fun String.codePointAt(index: Int): CodePoint = | ||
CodePoint(codePointAtKt(index)) | ||
|
||
internal actual fun CodePoint.isLowerCase(): Boolean = | ||
Character.isLowerCase(code) | ||
|
||
internal actual fun CodePoint.isUpperCase(): Boolean = | ||
Character.isUpperCase(code) | ||
|
||
internal actual fun CodePoint.isJavaIdentifierStart(): Boolean = | ||
Character.isJavaIdentifierStart(code) | ||
|
||
internal actual fun CodePoint.isJavaIdentifierPart(): Boolean = | ||
Character.isJavaIdentifierPart(code) | ||
|
||
internal actual fun CodePoint.charCount(): Int { | ||
return Character.charCount(code) | ||
} | ||
|
||
internal actual fun StringBuilder.appendCodePoint(codePoint: CodePoint): StringBuilder { | ||
return appendCodePoint(codePoint.code) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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 | ||
|
||
/* | ||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
internal actual fun StringBuilder.appendCodePoint(codePoint: CodePoint): StringBuilder { | ||
// Copied from StringBuilder.kt, | ||
// TODO Is this correct? | ||
val code = codePoint.code | ||
if (code <= Char.MAX_VALUE.code) { | ||
append(code.toChar()) | ||
} else { | ||
append(Char.MIN_HIGH_SURROGATE + ((code - 0x10000) ushr 10)) | ||
append(Char.MIN_LOW_SURROGATE + (code and 0x3ff)) | ||
} | ||
return this | ||
} | ||
|
||
internal actual fun CodePoint.isJavaIdentifierStart(): Boolean { | ||
// TODO How check Java identifier start use code point? | ||
if (charCount() == 1) { | ||
return Char(code).isJavaIdentifierStart() | ||
} | ||
|
||
return true | ||
} | ||
|
||
internal actual fun CodePoint.isJavaIdentifierPart(): Boolean { | ||
// TODO How check Java identifier part use code point? | ||
if (charCount() == 1) { | ||
return Char(code).isJavaIdentifierPart() | ||
} | ||
|
||
return true | ||
} | ||
|
||
internal actual fun CodePoint.charCount(): Int { | ||
return if (code >= 0x10000) 2 else 1 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* 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 String.codePointAt(index: Int): CodePoint { | ||
val str = this | ||
val code = jsCodePointAt(str, index) | ||
return CodePoint(code) | ||
} | ||
|
||
internal actual fun CodePoint.isLowerCase(): Boolean { | ||
// TODO Will there be a problem? Is there a better way? | ||
val code = this.code | ||
val str = jsFromCodePoint(code) | ||
|
||
if (str.length != 1) { | ||
return false | ||
} | ||
|
||
return str.first().isLowerCase() | ||
} | ||
|
||
internal actual fun CodePoint.isUpperCase(): Boolean { | ||
// TODO Will there be a problem? Is there a better way? | ||
val code = this.code | ||
val str = jsFromCodePoint(code) | ||
|
||
if (str.length != 1) { | ||
return false | ||
} | ||
|
||
return str.first().isUpperCase() | ||
} | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
private fun jsCodePointAt(str: String, index: Int): Int = | ||
js("str.codePointAt(index)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting that values returned by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, Kotlin/JS's |
||
|
||
@Suppress("UNUSED_PARAMETER") | ||
private fun jsFromCodePoint(code: Int): String = | ||
js("String.fromCodePoint(code)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we even need these anymore. It's retained from JavaPoet. We should probably write our own rules for the Kotlin language, as it is probably similar but not exactly the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do this in a follow-up.