java - Kotlin字符串最大长度?(带有长字符串的 Kotlin 文件未编译)
问题描述
根据这个答案, Java 最多可以容纳 2^31 - 1 个字符。我正在尝试进行基准测试和其他东西,所以我尝试创建大量字符串并将其写入如下文件:
import java.io.*
fun main() {
val out = File("ouput.txt").apply { createNewFile() }.printWriter()
sequence {
var x = 0
while (true) {
yield("${++x} ${++x} ${++x} ${++x} ${++x}")
}
}.take(5000).forEach { out.println(it) }
out.close()
}
然后output.txt
文件包含这样的内容:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
// ... 5000 lines
然后我将文件的所有内容复制到一个字符串中,以便对某些函数进行一些基准测试,所以它看起来是这样的:
import kotlin.system.*
fun main() {
val inputs = """
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
// ... 5000 lines
24986 24987 24988 24989 24990
24991 24992 24993 24994 24995
24996 24997 24998 24999 25000
""".trimIndent()
measureNanoTime {
inputs.reader().forEachLine { line ->
val (a, b, c, d, e) = line.split(" ").map(String::toInt)
}
}.div(5000).let(::println)
}
文件/字符串的总字符数为 138894
字符串最多可以容纳 2147483647
但是 Kotlin 代码没有编译(最后一个文件)它抛出编译错误:
e: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: wrong bytecode generated
// more lines
The root cause java.lang.IllegalArgumentException was thrown at: org.jetbrains.org.objectweb.asm.ByteVector.putUTF8(ByteVector.java:246)
at org.jetbrains.kotlin.codegen.TransformationMethodVisitor.visitEnd(TransformationMethodVisitor.kt:92)
at org.jetbrains.kotlin.codegen.FunctionCodegen.endVisit(FunctionCodegen.java:971)
... 43 more
Caused by: java.lang.IllegalArgumentException: UTF8 string too large
这是堆栈跟踪异常的总日志:https ://gist.github.com/Animeshz/1a18a7e99b0c0b913027b7fb36940c07
解决方案
java类文件有限制,字符串常量的长度必须适合16位,即。65535 字节(不是字符)是源代码中字符串的最大长度。
推荐阅读
- json - Visual Studio 中没有所有 ARM 模板功能
- android - 在资源方面,是否可以无限循环使用 context.getString() 中的字符串刷新 TextView(每 n 秒)?
- microsoft-teams - isFullScreen 清单设置不执行任何操作
- python - Python中的混合继承
- rust - Cargo Doc 中的下标和上标字符?
- stm32 - SWV 异常跟踪在 Atollic Studio 中不起作用
- python - sys.stdout.write() 在交互模式下表现不同
- twilio - Twilio 可编程视频 SDK 问题
- flutter - 使用 Flutter WEB 将内容分享到社交媒体
- python - 两个字符之间的正则表达式匹配,包括 Python