fix: irc messages not getting split by line

This commit is contained in:
Stone_Red
2026-07-23 23:06:14 +02:00
committed by HueByte
parent b1aeb8cbb2
commit 329dfe422b
+12 -5
View File
@@ -138,18 +138,24 @@ public static class IrcMessageFormatter
} }
/// <summary> /// <summary>
/// Split a message into chunks of approximately maxBytes (UTF-8), at word boundaries. /// Split a message into chunks of approximately maxBytes (UTF-8), at line and word boundaries.
/// </summary> /// </summary>
public static List<string> SplitMessage(string content, int maxBytes) public static List<string> SplitMessage(string content, int maxBytes)
{ {
if (Encoding.UTF8.GetByteCount(content) <= maxBytes)
return [content];
var chunks = new List<string>(); var chunks = new List<string>();
foreach (var line in content.Split('\n'))
{
if (Encoding.UTF8.GetByteCount(line) <= maxBytes)
{
chunks.Add(line);
continue;
}
var current = new StringBuilder(); var current = new StringBuilder();
var currentBytes = 0; var currentBytes = 0;
foreach (var word in content.Split(' ')) foreach (var word in line.Split(' '))
{ {
var wordBytes = Encoding.UTF8.GetByteCount(word) + 1; // +1 for space var wordBytes = Encoding.UTF8.GetByteCount(word) + 1; // +1 for space
@@ -166,6 +172,7 @@ public static class IrcMessageFormatter
if (current.Length > 0) if (current.Length > 0)
chunks.Add(current.ToString().TrimEnd()); chunks.Add(current.ToString().TrimEnd());
}
return chunks; return chunks;
} }