From fa21b6d2241d3dc1bdcbf867d36836ed6f7b4b52 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Mon, 6 Jun 2022 14:16:01 +0100 Subject: [PATCH] creating generic extension for converting empty strings to null and making use for finding option regex match --- .../java/org/matrix/android/sdk/api/extensions/Strings.kt | 5 +++++ .../android/sdk/internal/auth/version/HomeServerVersion.kt | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/extensions/Strings.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/extensions/Strings.kt index 5e1350e327..2a8848e80a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/extensions/Strings.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/extensions/Strings.kt @@ -27,3 +27,8 @@ fun CharSequence.ensurePrefix(prefix: CharSequence): CharSequence { * Append a new line and then the provided string. */ fun StringBuilder.appendNl(str: String) = append("\n").append(str) + +/** + * Returns null if the string is empty. + */ +fun String.ensureNotEmpty() = ifEmpty { null } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersion.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersion.kt index 62c94700d2..239adfdbbd 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersion.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersion.kt @@ -16,6 +16,8 @@ package org.matrix.android.sdk.internal.auth.version +import org.matrix.android.sdk.api.extensions.ensureNotEmpty + /** * Values will take the form "rX.Y.Z". * Ref: https://matrix.org/docs/spec/client_server/latest#get-matrix-client-versions @@ -45,7 +47,7 @@ internal data class HomeServerVersion( return HomeServerVersion( major = result.groupValues[1].toInt(), minor = result.groupValues[2].toInt(), - patch = result.groupValues.getOptional(index = 3, default = "0").toInt() + patch = result.groupValues.getOrNull(index = 3)?.ensureNotEmpty()?.toInt() ?: 0 ) } @@ -59,5 +61,3 @@ internal data class HomeServerVersion( val v1_3_0 = HomeServerVersion(major = 1, minor = 3, patch = 0) } } - -private fun List.getOptional(index: Int, default: String) = getOrNull(index)?.ifEmpty { default } ?: default