Browse Source

Fix #3612: WholeProjectDecompiler.CleanUpName does not count bytes on Unix.

pull/3638/head
Siegfried Pammer 2 weeks ago
parent
commit
1579a1698c
  1. 18
      ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs

18
ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs

@ -692,18 +692,23 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
} }
// Whitelist allowed characters, replace everything else: // Whitelist allowed characters, replace everything else:
StringBuilder b = new StringBuilder(text.Length + (extension?.Length ?? 0)); StringBuilder b = new StringBuilder(text.Length + (extension?.Length ?? 0));
bool countBytes = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
foreach (var c in text) foreach (var c in text)
{ {
currentSegmentLength++;
if (char.IsLetterOrDigit(c) || c == '-' || c == '_') if (char.IsLetterOrDigit(c) || c == '-' || c == '_')
{ {
unsafe
{
currentSegmentLength += countBytes ? Encoding.UTF8.GetByteCount(&c, 1) : 1;
}
// if the current segment exceeds maxSegmentLength characters, // if the current segment exceeds maxSegmentLength characters,
// skip until the end of the segment. // skip until the end of the segment.
if (currentSegmentLength <= maxSegmentLength) if (currentSegmentLength <= maxSegmentLength)
b.Append(c); b.Append(c);
} }
else if (c == '.' && b.Length > 0 && b[b.Length - 1] != '.') else if (c == '.' && b.Length > 0 && b[^1] != '.')
{ {
currentSegmentLength++;
// if the current segment exceeds maxSegmentLength characters, // if the current segment exceeds maxSegmentLength characters,
// skip until the end of the segment. // skip until the end of the segment.
if (separateAtDots || currentSegmentLength <= maxSegmentLength) if (separateAtDots || currentSegmentLength <= maxSegmentLength)
@ -713,7 +718,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
if (separateAtDots) if (separateAtDots)
currentSegmentLength = 0; currentSegmentLength = 0;
} }
else if (treatAsPath && (c is '/' or '\\') && currentSegmentLength > 1) else if (treatAsPath && (c is '/' or '\\') && currentSegmentLength > 0)
{ {
// if we treat this as a file name, we've started a new segment // if we treat this as a file name, we've started a new segment
b.Append(Path.DirectorySeparatorChar); b.Append(Path.DirectorySeparatorChar);
@ -721,6 +726,13 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
} }
else else
{ {
if (char.IsHighSurrogate(c))
{
// only add one replacement character for surrogate pairs
continue;
}
currentSegmentLength++;
// if the current segment exceeds maxSegmentLength characters, // if the current segment exceeds maxSegmentLength characters,
// skip until the end of the segment. // skip until the end of the segment.
if (currentSegmentLength <= maxSegmentLength) if (currentSegmentLength <= maxSegmentLength)

Loading…
Cancel
Save