diff --git a/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs b/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs index 4e1a3385f..27fea11a5 100644 --- a/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs @@ -692,18 +692,23 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler } // Whitelist allowed characters, replace everything else: StringBuilder b = new StringBuilder(text.Length + (extension?.Length ?? 0)); + bool countBytes = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows); foreach (var c in text) { - currentSegmentLength++; if (char.IsLetterOrDigit(c) || c == '-' || c == '_') { + unsafe + { + currentSegmentLength += countBytes ? Encoding.UTF8.GetByteCount(&c, 1) : 1; + } // if the current segment exceeds maxSegmentLength characters, // skip until the end of the segment. if (currentSegmentLength <= maxSegmentLength) 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, // skip until the end of the segment. if (separateAtDots || currentSegmentLength <= maxSegmentLength) @@ -713,7 +718,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler if (separateAtDots) 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 b.Append(Path.DirectorySeparatorChar); @@ -721,6 +726,13 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler } else { + if (char.IsHighSurrogate(c)) + { + // only add one replacement character for surrogate pairs + continue; + } + + currentSegmentLength++; // if the current segment exceeds maxSegmentLength characters, // skip until the end of the segment. if (currentSegmentLength <= maxSegmentLength)