@@ -112,42 +112,30 @@ object StringUtils {
112
112
if (entities.isNullOrEmpty()) return Pair (pangu.spacingText(text), entities)
113
113
114
114
val panguText = pangu.spacingText(text)
115
- val panguEntities = arrayListOf<TLRPC .MessageEntity >()
116
115
117
116
if (panguText.length == text.length) return Pair (panguText, entities) // processed or unnecessary
118
117
119
- entities.forEach {
118
+ var skip = 0
119
+ for (i in 0 until text.lastIndex) {
120
+ if (i + skip >= panguText.length) break
121
+ if (text[i] == panguText[i + skip]) continue
120
122
121
- val char = mutableListOf< Char >(). also { list ->
122
- for (i in it.offset until (it.offset + it.length).coerceAtMost(text.length)) {
123
- list.add(text[i])
124
- }
125
- }. also { list ->
126
- if (list.isEmpty()) return @forEach
123
+ entities.forEach {
124
+ if ( it.offset >= i + skip) { // text is after this entity
125
+ it.offset + = 1
126
+ } else if (it.offset + it.length >= i + skip) { // text is in this entity
127
+ it.length + = 1
128
+ } // text is before this entity
127
129
}
130
+ skip + = 1
131
+ }
128
132
129
- var length = 0
130
- var start = it.offset
131
- var matched = false // matched first character
132
- for (i in it.offset until panguText.length) {
133
- if (start > i) continue
134
- if (panguText[i] == char[0 ]) { // match
135
- char.removeAt(0 )
136
- if (! matched) {
137
- start = i
138
- matched = true
139
- }
140
- }
141
- if (matched) length++
142
- if (char.isEmpty()) { // empty processing list
143
- panguEntities.add(it.apply {
144
- it.offset = start
145
- it.length = length.coerceAtMost(panguText.lastIndex)
146
- })
147
- break
148
- }
149
- }
133
+ // ensure offset and length is valid
134
+ entities.forEach {
135
+ it.offset = it.offset.coerceAtLeast(0 ).coerceAtMost(panguText.length)
136
+ it.length = it.length.coerceAtLeast(0 ).coerceAtMost(panguText.length - it.offset)
150
137
}
151
- return Pair (panguText, panguEntities)
138
+
139
+ return Pair (panguText, entities)
152
140
}
153
141
}
0 commit comments