Improve reusability of code

This commit is contained in:
Benoit Marty 2021-10-04 14:13:25 +02:00
parent ba80bf5ba1
commit f385e74662
2 changed files with 10 additions and 9 deletions

View File

@ -17,6 +17,8 @@
package org.matrix.android.sdk.api
import org.matrix.android.sdk.BuildConfig
import org.matrix.android.sdk.internal.util.removeInvalidRoomNameChars
import org.matrix.android.sdk.internal.util.replaceSpaceChars
import timber.log.Timber
/**
@ -130,8 +132,8 @@ object MatrixPatterns {
fun isEventId(str: String?): Boolean {
return str != null &&
(str matches PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER ||
str matches PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V3 ||
str matches PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V4)
str matches PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V3 ||
str matches PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V4)
}
/**
@ -164,12 +166,9 @@ object MatrixPatterns {
fun candidateAliasFromRoomName(roomName: String, domain: String): String {
return roomName.lowercase()
// Replace spaces by '_'
.let { Regex("\\s").replace(it, "_") }
// Remove all invalid chars
.let { "[^a-z0-9._%#@=+-]".toRegex().replace(it, "") }
// limit length
.substring(0, MatrixConstants.maxAliasLocalPartLength(domain))
.replaceSpaceChars(replacement = "_")
.removeInvalidRoomNameChars()
.take(MatrixConstants.maxAliasLocalPartLength(domain))
}
/**

View File

@ -78,7 +78,7 @@ internal val spaceChars = "[\u00A0\u2000-\u200B\u2800\u3000]".toRegex()
/**
* Strip all the UTF-8 chars which are actually spaces
*/
internal fun String.replaceSpaceChars() = replace(spaceChars, "")
internal fun String.replaceSpaceChars(replacement: String = "") = replace(spaceChars, replacement)
// String.capitalize is now deprecated
internal fun String.safeCapitalize(): String {
@ -90,3 +90,5 @@ internal fun String.safeCapitalize(): String {
}
}
}
internal fun String.removeInvalidRoomNameChars() = "[^a-z0-9._%#@=+-]".toRegex().replace(this, "")