Skip to content

Commit

Permalink
Remove all list operators in favor of androidx.compose.ui.util
Browse files Browse the repository at this point in the history
  • Loading branch information
saket committed Sep 14, 2024
1 parent 78a90ae commit 2846561
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import androidx.compose.ui.text.ExperimentalTextApi
import androidx.compose.ui.text.LinkAnnotation
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.text.buildAnnotatedString
import me.saket.extendedspans.internal.fastFold
import me.saket.extendedspans.internal.fastForEach
import me.saket.extendedspans.internal.fastMap
import androidx.compose.ui.util.fastFold
import androidx.compose.ui.util.fastForEach
import androidx.compose.ui.util.fastMap

@Stable
class ExtendedSpans(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.sp
import androidx.compose.ui.util.fastForEach
import androidx.compose.ui.util.fastForEachIndexed
import me.saket.extendedspans.internal.deserializeToColor
import me.saket.extendedspans.internal.fastForEach
import me.saket.extendedspans.internal.fastForEachIndexed
import me.saket.extendedspans.internal.serialize

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import androidx.compose.ui.text.style.TextDecoration.Companion.Underline
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.sp
import me.saket.extendedspans.internal.deserializeToColor
import me.saket.extendedspans.internal.fastFirstOrNull
import me.saket.extendedspans.internal.fastForEach
import androidx.compose.ui.util.fastFirstOrNull
import androidx.compose.ui.util.fastForEach
import me.saket.extendedspans.internal.fastMapRange
import me.saket.extendedspans.internal.deserializeToColor
import me.saket.extendedspans.internal.serialize
import kotlin.math.ceil
import kotlin.math.sin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,6 @@ package me.saket.extendedspans.internal
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract

/**
* Copied from [androidx](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:text/text/src/main/java/androidx/compose/ui/text/android/TempListUtils.kt;l=33;drc=b2e3d878411b7fb1147455b1a204cddb7bee1a1b).
*/
@OptIn(ExperimentalContracts::class)
internal inline fun <T> List<T>.fastForEach(action: (T) -> Unit) {
contract { callsInPlace(action) }
for (index in indices) {
val item = get(index)
action(item)
}
}

@OptIn(ExperimentalContracts::class)
internal inline fun <T> List<T>.fastForEachIndexed(action: (index: Int, T) -> Unit) {
contract { callsInPlace(action) }
for (index in indices) {
val item = get(index)
action(index, item)
}
}

@OptIn(ExperimentalContracts::class)
internal inline fun <T> List<T>.fastFirstOrNull(predicate: (T) -> Boolean): T? {
contract { callsInPlace(predicate) }
for (index in indices) {
val item = get(index)
if (predicate(item)) {
return item
}
}
return null
}

/**
* Copied from [androidx](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:text/text/src/main/java/androidx/compose/ui/text/android/TempListUtils.kt;l=50;drc=b2e3d878411b7fb1147455b1a204cddb7bee1a1b).
*/
@OptIn(ExperimentalContracts::class)
internal inline fun <T, R> List<T>.fastMap(
transform: (T) -> R
): List<R> {
contract { callsInPlace(transform) }
val destination = ArrayList<R>(/* initialCapacity = */ size)
fastForEach { item ->
destination.add(transform(item))
}
return destination
}

/**
* Copied from [androidx](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/ui/ui-text/src/commonMain/kotlin/androidx/compose/ui/text/TempListUtils.kt;l=107;drc=ceaa7640c065146360515e598a3d09f6f66553dd).
*/
@Suppress("BanInlineOptIn") // Treat Kotlin Contracts as non-experimental.
@OptIn(ExperimentalContracts::class)
internal inline fun <T, R> List<T>.fastFold(initial: R, operation: (acc: R, T) -> R): R {
contract { callsInPlace(operation) }
var accumulator = initial
fastForEach { e ->
accumulator = operation(accumulator, e)
}
return accumulator
}

@OptIn(ExperimentalContracts::class)
internal inline fun <R> fastMapRange(
start: Int,
Expand Down

0 comments on commit 2846561

Please sign in to comment.