@@ -27,6 +27,27 @@ import kotlinx.coroutines.flow.StateFlow
2727import timber.log.Timber
2828
2929private const val MAX_RENDERED_STRING_RESULTS = 5_000
30+ private const val MAX_RENDERED_STRING_CHARS = 4_096
31+ private const val MAX_RENDERED_STRING_TOTAL_CHARS = 32_768
32+
33+ internal fun clipFoundStringForRendering (
34+ result : FoundString ,
35+ maxChars : Int = MAX_RENDERED_STRING_CHARS
36+ ): Pair <FoundString , Boolean > {
37+ require(maxChars >= 0 ) { " maxChars must be non-negative" }
38+ if (result.string.length <= maxChars) {
39+ return result to false
40+ }
41+ if (maxChars == 0 ) {
42+ return result.copy(string = " " ) to true
43+ }
44+ val clippedString = if (maxChars <= 3 ) {
45+ result.string.take(maxChars)
46+ } else {
47+ result.string.take(maxChars - 3 ) + " ..."
48+ }
49+ return result.copy(string = clippedString) to true
50+ }
3051
3152class StringSearchResultAccumulator (private val maxResults : Int ) {
3253 private val _results = mutableListOf<FoundString >()
@@ -36,12 +57,27 @@ class StringSearchResultAccumulator(private val maxResults: Int) {
3657 var isTruncated: Boolean = false
3758 private set
3859
60+ private var renderedChars: Int = 0
61+
3962 fun append (result : FoundString ) {
4063 if (_results .size >= maxResults) {
4164 isTruncated = true
4265 return
4366 }
44- _results .add(result)
67+ val remainingChars = MAX_RENDERED_STRING_TOTAL_CHARS - renderedChars
68+ if (remainingChars <= 0 ) {
69+ isTruncated = true
70+ return
71+ }
72+ val (displayResult, wasClipped) = clipFoundStringForRendering(
73+ result,
74+ minOf(MAX_RENDERED_STRING_CHARS , remainingChars)
75+ )
76+ if (wasClipped) {
77+ isTruncated = true
78+ }
79+ _results .add(displayResult)
80+ renderedChars + = displayResult.string.length
4581 }
4682}
4783
@@ -61,8 +97,8 @@ class StringTabData(val data: TabKind.FoundString) : PreparedTabData() {
6197 analyzer.searchStrings(data.range.first, data.range.last) { p, t, fs ->
6298 fs?.let {
6399 accumulator.append(it)
64- if (! accumulator.isTruncated ) {
65- strings.add(it )
100+ if (strings.size < accumulator.results.size ) {
101+ strings.add(accumulator.results.last() )
66102 }
67103 }
68104 if (p == t) { // done
0 commit comments