Skip to content

Commit c9e2cb0

Browse files
fix(mobile): Android diff ellipsis no longer measures text quadratically
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent dc84b09 commit c9e2cb0

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

  • apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff

‎apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,15 +1423,22 @@ private fun centeredBaseline(top: Int, bottom: Int, paint: Paint): Float {
14231423
return (top + bottom) / 2f - (metrics.ascent + metrics.descent) / 2f
14241424
}
14251425

1426+
// A single-line preview never shows this many characters, so longer text is cut before measuring.
1427+
private const val ELLIPSIZE_MAX_CHARS = 500
1428+
14261429
private fun ellipsize(value: String, paint: Paint, width: Float): String {
1427-
if (paint.measureText(value) <= width) return value
1430+
val truncated = value.length > ELLIPSIZE_MAX_CHARS
1431+
val text = if (truncated) value.substring(0, surrogateSafeEnd(value, ELLIPSIZE_MAX_CHARS)) else value
1432+
if (!truncated && paint.measureText(text) <= width) return value
14281433
val suffix = "..."
14291434
val available = max(0f, width - paint.measureText(suffix))
1430-
var end = value.length
1431-
while (end > 0 && paint.measureText(value, 0, end) > available) end -= 1
1432-
return value.substring(0, end) + suffix
1435+
val end = surrogateSafeEnd(text, paint.breakText(text, true, available, null))
1436+
return text.substring(0, end) + suffix
14331437
}
14341438

1439+
private fun surrogateSafeEnd(value: String, end: Int): Int =
1440+
if (end in 1 until value.length && Character.isSurrogatePair(value[end - 1], value[end])) end - 1 else end
1441+
14351442
private fun indexFileHeaders(rows: List<DiffRow>): IntArray {
14361443
val headers = ArrayList<Int>()
14371444
rows.forEachIndexed { index, row ->

0 commit comments

Comments
 (0)