fix: use little-endian byte order for protobuf fixed32 values

pull/627/head
andrekir 2023-04-24 22:23:40 -03:00
rodzic 6f5ed93db3
commit 7834cb1f0c
1 zmienionych plików z 13 dodań i 16 usunięć

Wyświetl plik

@ -132,16 +132,18 @@ fun EditIPv4Preference(
onValueChanged: (Int) -> Unit, onValueChanged: (Int) -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
) { ) {
val pattern = """\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b""".toRegex()
fun convertIntToIpAddress(int: Int): String { fun convertIntToIpAddress(int: Int): String {
return "${int shr 24 and 0xff}.${int shr 16 and 0xff}.${int shr 8 and 0xff}.${int and 0xff}" return "${int and 0xff}.${int shr 8 and 0xff}.${int shr 16 and 0xff}.${int shr 24 and 0xff}"
} }
fun convertIpAddressToInt(ipAddress: String): Int? {
return ipAddress.split(".") fun convertIpAddressToInt(ipAddress: String): Int? = ipAddress.split(".")
.map { it.toIntOrNull() } .map { it.toIntOrNull() }.reversed() // little-endian byte order
.fold(0) { total, next -> .fold(0) { total, next ->
if (next == null) return null else total shl 8 or next if (next == null) return null else total shl 8 or next
} }
}
var valueState by remember(value) { mutableStateOf(convertIntToIpAddress(value)) } var valueState by remember(value) { mutableStateOf(convertIntToIpAddress(value)) }
EditTextPreference( EditTextPreference(
@ -154,13 +156,8 @@ fun EditIPv4Preference(
), ),
keyboardActions = keyboardActions, keyboardActions = keyboardActions,
onValueChanged = { onValueChanged = {
val pattern = """\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b""".toRegex()
val isValid = pattern.matches(it)
if (it.isEmpty() || !isValid) valueState = it
else convertIpAddressToInt(it)?.let { int ->
valueState = it valueState = it
onValueChanged(int) if (pattern.matches(it)) convertIpAddressToInt(it)?.let { int -> onValueChanged(int) }
}
}, },
onFocusChanged = {}, onFocusChanged = {},
modifier = modifier modifier = modifier
@ -299,7 +296,7 @@ private fun EditTextPreferencePreview() {
) )
EditIPv4Preference( EditIPv4Preference(
title = "IP Address", title = "IP Address",
value = 3232235521.toInt(), value = 16820416,
enabled = true, enabled = true,
keyboardActions = KeyboardActions {}, keyboardActions = KeyboardActions {},
onValueChanged = {} onValueChanged = {}