kopia lustrzana https://github.com/ryukoposting/Signal-Android
rodzic
57e233413a
commit
b3086e595f
|
@ -1,9 +1,14 @@
|
||||||
package org.thoughtcrime.securesms.util
|
package org.thoughtcrime.securesms.util
|
||||||
|
|
||||||
import android.text.TextUtils
|
import org.signal.core.util.CharacterIterable
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
object NameUtil {
|
object NameUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \p{L} is letter, \p{Nd} is digit, \p{S} is whitespace/separator
|
||||||
|
* https://www.regular-expressions.info/unicode.html#category
|
||||||
|
*/
|
||||||
private val PATTERN = Pattern.compile("[^\\p{L}\\p{Nd}\\p{S}]+")
|
private val PATTERN = Pattern.compile("[^\\p{L}\\p{Nd}\\p{S}]+")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -11,24 +16,20 @@ object NameUtil {
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getAbbreviation(name: String): String? {
|
fun getAbbreviation(name: String): String? {
|
||||||
val parts = name.split(" ").toTypedArray()
|
val parts = name
|
||||||
val builder = StringBuilder()
|
.split(" ")
|
||||||
var count = 0
|
.map { it.trim() }
|
||||||
var i = 0
|
.map { PATTERN.matcher(it).replaceFirst("") }
|
||||||
|
.filter { it.isNotEmpty() }
|
||||||
|
|
||||||
while (i < parts.size && count < 2) {
|
return when {
|
||||||
val cleaned = PATTERN.matcher(parts[i]).replaceFirst("")
|
parts.isEmpty() -> null
|
||||||
if (!TextUtils.isEmpty(cleaned)) {
|
parts.size == 1 -> parts[0].firstGrapheme()
|
||||||
builder.appendCodePoint(cleaned.codePointAt(0))
|
else -> "${parts[0].firstGrapheme()}${parts[1].firstGrapheme()}"
|
||||||
count++
|
|
||||||
}
|
}
|
||||||
i++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return if (builder.isEmpty()) {
|
private fun String.firstGrapheme(): String {
|
||||||
null
|
return CharacterIterable(this).first()
|
||||||
} else {
|
|
||||||
builder.toString()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
package org.thoughtcrime.securesms.util
|
||||||
|
|
||||||
|
import android.app.Application
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.robolectric.ParameterizedRobolectricTestRunner
|
||||||
|
import org.robolectric.annotation.Config
|
||||||
|
|
||||||
|
@RunWith(value = ParameterizedRobolectricTestRunner::class)
|
||||||
|
@Config(manifest = Config.NONE, application = Application::class)
|
||||||
|
class NameUtil_getAbbreviation(
|
||||||
|
private val name: String,
|
||||||
|
private val expected: String?
|
||||||
|
) {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun test_getAbbreviation() {
|
||||||
|
val actual: String? = NameUtil.getAbbreviation(name)
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
@ParameterizedRobolectricTestRunner.Parameters(name = "{index}: getAbbreviation({0})={1}")
|
||||||
|
fun params() = listOf(
|
||||||
|
arrayOf("Gwen Stacy", "GS"),
|
||||||
|
arrayOf("Gwen", "G"),
|
||||||
|
arrayOf("gwen stacy", "gs"),
|
||||||
|
arrayOf("Mary Jane Watson", "MJ"),
|
||||||
|
arrayOf("Mary-Jane Watson", "MW"),
|
||||||
|
arrayOf("αlpha Ωmega", "αΩ"),
|
||||||
|
arrayOf("љabc ђ123", "љђ"),
|
||||||
|
// Works on device, but for whatever reason doesn't work in robolectric
|
||||||
|
// arrayOf("Bob \uD83C\uDDE8\uD83C\uDDFF", "B\uD83C\uDDE8\uD83C\uDDFF"),
|
||||||
|
arrayOf("", null),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
Ładowanie…
Reference in New Issue