From e6a4b802e719880a74f9d7ed615c793426b30c6b Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Sun, 23 Mar 2025 03:10:04 +0100 Subject: [PATCH] Html/Markdown: prune entities of length=0 (fix wiz0u/WTelegramBot#6) --- src/Services.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Services.cs b/src/Services.cs index 99ce13b..d62b877 100644 --- a/src/Services.cs +++ b/src/Services.cs @@ -458,11 +458,13 @@ namespace TL { int newlen = sb.Length; while (--newlen >= 0 && char.IsWhiteSpace(sb[newlen])); - if (++newlen == sb.Length) return; - sb.Length = newlen; - foreach (var entity in entities) - if (entity.offset + entity.length > newlen) - entity.length = newlen - entity.offset; + if (++newlen != sb.Length) sb.Length = newlen; + for (int i = 0; i < entities.Count; i++) + { + var entity = entities[i]; + if (entity.offset + entity.length > newlen) entity.length = newlen - entity.offset; + if (entity.length == 0) entities.RemoveAt(i--); + } } /// Converts the (plain text + entities) format used by Telegram messages into an HTML-formatted text