Skip to content

Commit 1579a16

Browse files
Fix #3612: WholeProjectDecompiler.CleanUpName does not count bytes on Unix.
1 parent 17016c7 commit 1579a16

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,18 +692,23 @@ static string CleanUpName(string text, bool separateAtDots, bool treatAsFileName
692692
}
693693
// Whitelist allowed characters, replace everything else:
694694
StringBuilder b = new StringBuilder(text.Length + (extension?.Length ?? 0));
695+
bool countBytes = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
695696
foreach (var c in text)
696697
{
697-
currentSegmentLength++;
698698
if (char.IsLetterOrDigit(c) || c == '-' || c == '_')
699699
{
700+
unsafe
701+
{
702+
currentSegmentLength += countBytes ? Encoding.UTF8.GetByteCount(&c, 1) : 1;
703+
}
700704
// if the current segment exceeds maxSegmentLength characters,
701705
// skip until the end of the segment.
702706
if (currentSegmentLength <= maxSegmentLength)
703707
b.Append(c);
704708
}
705-
else if (c == '.' && b.Length > 0 && b[b.Length - 1] != '.')
709+
else if (c == '.' && b.Length > 0 && b[^1] != '.')
706710
{
711+
currentSegmentLength++;
707712
// if the current segment exceeds maxSegmentLength characters,
708713
// skip until the end of the segment.
709714
if (separateAtDots || currentSegmentLength <= maxSegmentLength)
@@ -713,14 +718,21 @@ static string CleanUpName(string text, bool separateAtDots, bool treatAsFileName
713718
if (separateAtDots)
714719
currentSegmentLength = 0;
715720
}
716-
else if (treatAsPath && (c is '/' or '\\') && currentSegmentLength > 1)
721+
else if (treatAsPath && (c is '/' or '\\') && currentSegmentLength > 0)
717722
{
718723
// if we treat this as a file name, we've started a new segment
719724
b.Append(Path.DirectorySeparatorChar);
720725
currentSegmentLength = 0;
721726
}
722727
else
723728
{
729+
if (char.IsHighSurrogate(c))
730+
{
731+
// only add one replacement character for surrogate pairs
732+
continue;
733+
}
734+
735+
currentSegmentLength++;
724736
// if the current segment exceeds maxSegmentLength characters,
725737
// skip until the end of the segment.
726738
if (currentSegmentLength <= maxSegmentLength)

0 commit comments

Comments
 (0)