|
1 | | -// SPDX-FileCopyrightText: 2024 Emulsion contributors <https://github.com/codingteam/emulsion> |
| 1 | +// SPDX-FileCopyrightText: 2025 Emulsion contributors <https://github.com/codingteam/emulsion> |
2 | 2 | // |
3 | 3 | // SPDX-License-Identifier: MIT |
4 | 4 |
|
@@ -105,16 +105,30 @@ module MessageConverter = |
105 | 105 | pos <- linkEndOffset |
106 | 106 | result.Append(text.Substring(pos, text.Length - pos)).ToString() |
107 | 107 |
|
108 | | - let private applyLimits limits text = |
| 108 | + let private applyLimits limits (text: string) = |
109 | 109 | let applyMessageLengthLimit (original: {| text: string; wasLimited: bool |}) = |
110 | 110 | match limits.messageLengthLimit with |
111 | 111 | | None -> original |
112 | 112 | | Some limit when original.text.Length <= limit -> original |
113 | 113 | | Some limit -> |
114 | | - let newText = original.text.Substring(0, |
115 | | - Math.Clamp(limit - limits.dataRedactedMessage.Length, |
116 | | - 0, |
117 | | - original.text.Length)) |
| 114 | + assert (limit >= limits.dataRedactedMessage.Length) |
| 115 | + |
| 116 | + let mutable newTextLength = Math.Clamp( |
| 117 | + limit - limits.dataRedactedMessage.Length, |
| 118 | + 0, |
| 119 | + original.text.Length |
| 120 | + ) |
| 121 | + |
| 122 | + // We should never split surrogate pairs present in the initial message. So, if the message ends with a |
| 123 | + // high part of such a pair, cut it more, to remove the part of the pair. |
| 124 | + // |
| 125 | + // Technically, this will also strip a part of an invalid Unicode sequence if the message originally |
| 126 | + // contained such an orphan part of the pair without even following it by a high surrogate. But we don't |
| 127 | + // care. |
| 128 | + if newTextLength > 0 && Char.IsHighSurrogate(text[newTextLength - 1]) then |
| 129 | + newTextLength <- newTextLength - 1 |
| 130 | + |
| 131 | + let newText = original.text.Substring(0, newTextLength) |
118 | 132 | {| text = newText; wasLimited = true |} |
119 | 133 |
|
120 | 134 | let applyLineLimit (original: {| text: string; wasLimited: bool |}) = |
|
0 commit comments