mirror of
https://github.com/vector-im/element-android.git
synced 2024-11-16 02:05:06 +08:00
Improve reusability of code
This commit is contained in:
parent
ba80bf5ba1
commit
f385e74662
@ -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))
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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, "")
|
||||
|
Loading…
Reference in New Issue
Block a user