Skip to content

Commit 0b7caf2

Browse files
fix(mobile): Android diff ellipsis no longer measures text quadratically (#26174)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f0c1da3 commit 0b7caf2

1 file changed

Lines changed: 15 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: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,13 +1423,24 @@ 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 capEnd = surrogateSafeEnd(value, ELLIPSIZE_MAX_CHARS)
1432+
val text = if (truncated) value.substring(0, capEnd) else value
1433+
if (!truncated && paint.measureText(text) <= width) return value
14281434
val suffix = "..."
14291435
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
1436+
val end = surrogateSafeEnd(text, paint.breakText(text, true, available, null))
1437+
return text.substring(0, end) + suffix
1438+
}
1439+
1440+
private fun surrogateSafeEnd(value: String, end: Int): Int {
1441+
val splitsPair = end in 1 until value.length &&
1442+
Character.isSurrogatePair(value[end - 1], value[end])
1443+
return if (splitsPair) end - 1 else end
14331444
}
14341445

14351446
private fun indexFileHeaders(rows: List<DiffRow>): IntArray {

0 commit comments

Comments
 (0)