Superscript support

#138
This commit is contained in:
Sad Ellie 2023-12-12 11:35:37 +03:00
parent 0793f0a19b
commit e6f74c56f8
3 changed files with 34 additions and 2 deletions

View File

@ -75,3 +75,15 @@ fun String.isExpression(): Boolean {
// Rest of the string must be just like positive
return this.drop(1).isExpression()
}
fun String.normalizeSuperscript(): String = this
.replace('⁰', '0')
.replace('¹', '1')
.replace('²', '2')
.replace('³', '3')
.replace('⁴', '4')
.replace('⁵', '5')
.replace('⁶', '6')
.replace('⁷', '7')
.replace('⁸', '8')
.replace('⁹', '9')

View File

@ -0,0 +1,13 @@
package com.sadellie.unitto.data.common
import org.junit.Assert.assertEquals
import org.junit.Test
class NormalizeSuperscriptTest {
@Test
fun normalizeSuperscript() {
val input = "⁰¹²³⁴⁵⁶⁷⁸⁹"
assertEquals("0123456789", input.normalizeSuperscript())
}
}

View File

@ -20,6 +20,7 @@ package com.sadellie.unitto.data.model.unit
import android.content.Context
import com.sadellie.unitto.data.common.lev
import com.sadellie.unitto.data.common.normalizeSuperscript
import com.sadellie.unitto.data.model.UnitGroup
import java.math.BigDecimal
@ -52,7 +53,10 @@ fun Sequence<AbstractUnit>.filterByLev(stringA: String, context: Context): Seque
// List of pair: Unit and it's levDist
val unitsWithDist = mutableListOf<Pair<AbstractUnit, Int>>()
this.forEach { unit ->
val unitShortName: String = context.getString(unit.shortName).lowercase()
val unitShortName: String = context
.getString(unit.shortName)
.lowercase()
.normalizeSuperscript()
/**
* There is a chance that we search for unit with a specific short name. Such cases are
* should be higher in the list. Best possible match.
@ -62,7 +66,10 @@ fun Sequence<AbstractUnit>.filterByLev(stringA: String, context: Context): Seque
return@forEach
}
val unitFullName: String = context.getString(unit.displayName).lowercase()
val unitFullName: String = context
.getString(unit.displayName)
.lowercase()
.normalizeSuperscript()
/**
* There is chance that unit name doesn't need any edits (contains part of the query)