Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion pwiz_tools/Skyline/SkylineNightly/Nightly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using Ionic.Zip;
using Microsoft.Win32.TaskScheduler;
Expand Down Expand Up @@ -996,7 +997,7 @@ public string Post(RunMode mode, string xmlFile = null)
{
try
{
doc.Root.Add(new XElement("Log", log));
doc.Root.Add(new XElement("Log", ReplaceInvalidXmlChars(log)));
xml = doc.ToString();
}
catch (Exception e)
Expand Down Expand Up @@ -1374,6 +1375,42 @@ public interface IEndTimeSetter
[OperationContract]
void SetEndTime(DateTime endTime);
}
/// <summary>
/// Replace all invalid characters in the string with a backslash followed by 'u' and the hexadecimal code of the character.
/// Invalid characters are most of the control characters as well as surrogate characters which are not a high surrogate
/// followed by a low surrogate.
/// </summary>
public static string ReplaceInvalidXmlChars(string s)
{
StringBuilder stringBuilder = null;
for (int i = 0; i < s.Length; i++)
{
var ch = s[i];
if (XmlConvert.IsXmlChar(ch) && !char.IsSurrogate(ch))
{
stringBuilder?.Append(ch);
continue;
}
if (char.IsHighSurrogate(ch) && i < s.Length - 1)
{
var chLow = s[i + 1];
if (XmlConvert.IsXmlSurrogatePair(chLow, ch))
{
stringBuilder?.Append(ch);
stringBuilder?.Append(chLow);
i++;
continue;
}
}
if (stringBuilder == null)
{
stringBuilder = new StringBuilder(s.Length);
stringBuilder.Append(s.Substring(0, i));
}
stringBuilder.Append("\\u" + ((int)ch).ToString("X4"));
}
return stringBuilder?.ToString() ?? s;
}
}

// ReSharper restore LocalizableElement
Expand Down