👔 Text sanitizer no longer remove new lines and tabs

This commit is contained in:
LittleSheep 2025-06-09 01:40:06 +08:00
parent f155b4e2ac
commit eef64df81a

View File

@ -9,15 +9,24 @@ public abstract class TextSanitizer
{
if (string.IsNullOrEmpty(text)) return text;
// List of control characters to preserve
var preserveControlChars = new[] { '\n', '\r', '\t', ' ' };
var filtered = new StringBuilder();
foreach (var ch in from ch in text
let category = CharUnicodeInfo.GetUnicodeCategory(ch)
where category is not (UnicodeCategory.Control or UnicodeCategory.Format
or UnicodeCategory.NonSpacingMark)
select ch)
foreach (var ch in text)
{
var category = CharUnicodeInfo.GetUnicodeCategory(ch);
// Keep whitespace and other specified control characters
if (category is not UnicodeCategory.Control || preserveControlChars.Contains(ch))
{
// Still filter out Format and NonSpacingMark categories
if (category is not (UnicodeCategory.Format or UnicodeCategory.NonSpacingMark))
{
filtered.Append(ch);
}
}
}
return filtered.ToString();
}