Scripting: support scripts starting with UTF-8 BOM

#KT-54705 fixed
This commit is contained in:
Ilya Chernikov
2023-01-04 17:48:14 +01:00
parent 989c1ec64d
commit 6c035bb5f4
4 changed files with 27 additions and 2 deletions
@@ -62,7 +62,7 @@ abstract class FileBasedScriptSource() : ExternalSourceCode {
*/
open class FileScriptSource(override val file: File, private val preloadedText: String? = null) : FileBasedScriptSource(), Serializable {
override val externalLocation: URL get() = file.toURI().toURL()
override val text: String by lazy { preloadedText ?: file.readText() }
override val text: String by lazy { preloadedText ?: file.readTextSkipUtf8Bom() }
override val name: String? get() = file.name
override val locationId: String? get() = file.path
@@ -81,7 +81,7 @@ open class FileScriptSource(override val file: File, private val preloadedText:
* The implementation of the SourceCode for a script location pointed by the URL
*/
open class UrlScriptSource(override val externalLocation: URL) : ExternalSourceCode, Serializable {
override val text: String by lazy { externalLocation.readText() }
override val text: String by lazy { externalLocation.readTextSkipUtf8Bom() }
override val name: String? get() = externalLocation.file
override val locationId: String? get() = externalLocation.toString()
@@ -133,3 +133,10 @@ private val ExternalSourceCode.textSafe: String?
} catch (e: Throwable) {
null
}
private const val UTF8_BOM = 0xfeff.toChar().toString()
private fun File.readTextSkipUtf8Bom(): String = readText().removePrefix(UTF8_BOM)
private fun URL.readTextSkipUtf8Bom(): String = readText().removePrefix(UTF8_BOM)