mirror of
https://github.com/RedWizardsLab/EchoHub.git
synced 2026-09-04 08:36:11 +02:00
fix: irc messages not getting split by line
This commit is contained in:
@@ -138,34 +138,41 @@ 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>();
|
||||||
var current = new StringBuilder();
|
|
||||||
var currentBytes = 0;
|
|
||||||
|
|
||||||
foreach (var word in content.Split(' '))
|
foreach (var line in content.Split('\n'))
|
||||||
{
|
{
|
||||||
var wordBytes = Encoding.UTF8.GetByteCount(word) + 1; // +1 for space
|
if (Encoding.UTF8.GetByteCount(line) <= maxBytes)
|
||||||
|
|
||||||
if (currentBytes + wordBytes > maxBytes && current.Length > 0)
|
|
||||||
{
|
{
|
||||||
chunks.Add(current.ToString().TrimEnd());
|
chunks.Add(line);
|
||||||
current.Clear();
|
continue;
|
||||||
currentBytes = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
current.Append(word).Append(' ');
|
var current = new StringBuilder();
|
||||||
currentBytes += wordBytes;
|
var currentBytes = 0;
|
||||||
}
|
|
||||||
|
|
||||||
if (current.Length > 0)
|
foreach (var word in line.Split(' '))
|
||||||
chunks.Add(current.ToString().TrimEnd());
|
{
|
||||||
|
var wordBytes = Encoding.UTF8.GetByteCount(word) + 1; // +1 for space
|
||||||
|
|
||||||
|
if (currentBytes + wordBytes > maxBytes && current.Length > 0)
|
||||||
|
{
|
||||||
|
chunks.Add(current.ToString().TrimEnd());
|
||||||
|
current.Clear();
|
||||||
|
currentBytes = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
current.Append(word).Append(' ');
|
||||||
|
currentBytes += wordBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current.Length > 0)
|
||||||
|
chunks.Add(current.ToString().TrimEnd());
|
||||||
|
}
|
||||||
|
|
||||||
return chunks;
|
return chunks;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user