* 计算在同一消息中,当前匹配之前有多少个匹配
(messages: ChatMessage[], currentMatch: SearchMatch)
| 250 | * 计算在同一消息中,当前匹配之前有多少个匹配 |
| 251 | */ |
| 252 | function countMatchesBefore(messages: ChatMessage[], currentMatch: SearchMatch): number { |
| 253 | const messageContent = messages.find((m) => m.id === currentMatch.messageId)?.content ?? '' |
| 254 | const matchText = messageContent.slice(currentMatch.startOffset, currentMatch.endOffset) |
| 255 | |
| 256 | let count = 0 |
| 257 | let searchIndex = 0 |
| 258 | |
| 259 | while (searchIndex < currentMatch.startOffset) { |
| 260 | const idx = messageContent.indexOf(matchText, searchIndex) |
| 261 | if (idx === -1 || idx >= currentMatch.startOffset) break |
| 262 | count++ |
| 263 | searchIndex = idx + 1 |
| 264 | } |
| 265 | |
| 266 | return count |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * 根据拼接文本的偏移量创建 Range |
no outgoing calls
no test coverage detected