Handle zero-terminated strings in deprecated utf8 to String conversions (#3068)
This commit is contained in:
committed by
Nikolay Igotti
parent
409a9e9473
commit
a994f51402
@@ -166,7 +166,6 @@ public actual open class NumberFormatException : IllegalArgumentException {
|
|||||||
actual constructor(message: String?) : super(message)
|
actual constructor(message: String?) : super(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Use CharacterCodingException instead", ReplaceWith("CharacterCodingException"))
|
|
||||||
public open class IllegalCharacterConversionException : IllegalArgumentException {
|
public open class IllegalCharacterConversionException : IllegalArgumentException {
|
||||||
|
|
||||||
constructor(): super()
|
constructor(): super()
|
||||||
|
|||||||
@@ -9,31 +9,34 @@ package kotlin.native
|
|||||||
/**
|
/**
|
||||||
* Converts an UTF-8 array into a [String]. Replaces invalid input sequences with a default character.
|
* Converts an UTF-8 array into a [String]. Replaces invalid input sequences with a default character.
|
||||||
*/
|
*/
|
||||||
@Deprecated("Use decodeToString instead", ReplaceWith("decodeToString()"))
|
|
||||||
public fun ByteArray.stringFromUtf8() : String {
|
public fun ByteArray.stringFromUtf8() : String {
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
return this.stringFromUtf8(0, this.size)
|
return this.stringFromUtf8(0, this.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts an UTF-8 array into a [String]. Replaces invalid input sequences with a default character.
|
* Converts an UTF-8 array into a [String]. Replaces invalid input sequences with a default character.
|
||||||
*/
|
*/
|
||||||
@Deprecated("Use decodeToString instead", ReplaceWith("decodeToString(start, start + size)"))
|
|
||||||
public fun ByteArray.stringFromUtf8(start: Int = 0, size: Int = this.size) : String {
|
public fun ByteArray.stringFromUtf8(start: Int = 0, size: Int = this.size) : String {
|
||||||
checkBoundsIndexes(start, start + size, this.size)
|
checkBoundsIndexes(start, start + size, this.size)
|
||||||
return stringFromUtf8Impl(start, size)
|
return stringFromUtf8Impl(start, realSize(this, size))
|
||||||
}
|
}
|
||||||
|
|
||||||
@SymbolName("Kotlin_ByteArray_stringFromUtf8")
|
@SymbolName("Kotlin_ByteArray_stringFromUtf8")
|
||||||
internal external fun ByteArray.stringFromUtf8Impl(start: Int, size: Int) : String
|
internal external fun ByteArray.stringFromUtf8Impl(start: Int, size: Int) : String
|
||||||
|
|
||||||
|
private fun realSize(byteArray: ByteArray, size: Int): Int {
|
||||||
|
var realSize = 0
|
||||||
|
while (realSize < size && byteArray[realSize] != 0.toByte()) {
|
||||||
|
realSize++
|
||||||
|
}
|
||||||
|
return realSize
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts an UTF-8 array into a [String].
|
* Converts an UTF-8 array into a [String].
|
||||||
* @throws [IllegalCharacterConversionException] if the input is invalid.
|
* @throws [IllegalCharacterConversionException] if the input is invalid.
|
||||||
*/
|
*/
|
||||||
@Deprecated("Use decodeToString instead", ReplaceWith("decodeToString(throwOnInvalidSequence = true)"))
|
|
||||||
public fun ByteArray.stringFromUtf8OrThrow() : String {
|
public fun ByteArray.stringFromUtf8OrThrow() : String {
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
return this.stringFromUtf8OrThrow(0, this.size)
|
return this.stringFromUtf8OrThrow(0, this.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,13 +44,11 @@ public fun ByteArray.stringFromUtf8OrThrow() : String {
|
|||||||
* Converts an UTF-8 array into a [String].
|
* Converts an UTF-8 array into a [String].
|
||||||
* @throws [IllegalCharacterConversionException] if the input is invalid.
|
* @throws [IllegalCharacterConversionException] if the input is invalid.
|
||||||
*/
|
*/
|
||||||
@Deprecated("Use decodeToString instead", ReplaceWith("decodeToString(start, start + size, throwOnInvalidSequence = true)"))
|
|
||||||
public fun ByteArray.stringFromUtf8OrThrow(start: Int = 0, size: Int = this.size) : String {
|
public fun ByteArray.stringFromUtf8OrThrow(start: Int = 0, size: Int = this.size) : String {
|
||||||
checkBoundsIndexes(start, start + size, this.size)
|
checkBoundsIndexes(start, start + size, this.size)
|
||||||
try {
|
try {
|
||||||
return stringFromUtf8OrThrowImpl(start, size)
|
return stringFromUtf8OrThrowImpl(start, realSize(this, size))
|
||||||
} catch (e: CharacterCodingException) {
|
} catch (e: CharacterCodingException) {
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
throw IllegalCharacterConversionException()
|
throw IllegalCharacterConversionException()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,7 +97,6 @@ public fun String.toUtf8OrThrow(start: Int = 0, size: Int = this.length) : ByteA
|
|||||||
try {
|
try {
|
||||||
return toUtf8OrThrowImpl(start, size)
|
return toUtf8OrThrowImpl(start, size)
|
||||||
} catch (e: CharacterCodingException) {
|
} catch (e: CharacterCodingException) {
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
throw IllegalCharacterConversionException()
|
throw IllegalCharacterConversionException()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user