using System.Text;
using System.Xml;
namespace AIStudio.Tools.OpenXml;
///
/// Prepares texts for the Office documents AI Studio writes.
///
internal static class OpenXmlText
{
///
/// Leaves out the characters XML cannot carry, which a model may produce now and then. The file
/// could not be saved with them.
///
///
/// A character outside the Basic Multilingual Plane, such as an emoji, takes two chars; such a
/// pair stays, whereas a half of one alone goes.
///
/// The text.
/// The text without the characters XML cannot carry.
public static string XmlSafe(string text)
{
if (text.All(XmlConvert.IsXmlChar))
return text;
var result = new StringBuilder(text.Length);
for (var index = 0; index < text.Length; index++)
{
if (XmlConvert.IsXmlChar(text[index]))
result.Append(text[index]);
else if (index + 1 < text.Length && XmlConvert.IsXmlSurrogatePair(text[index + 1], text[index]))
result.Append(text[index]).Append(text[++index]);
}
return result.ToString();
}
}