-
Notifications
You must be signed in to change notification settings - Fork 868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: TOC items order issue #10585
Open
filzrev
wants to merge
5
commits into
dotnet:main
Choose a base branch
from
filzrev:fix-toc-orders-issue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix: TOC items order issue #10585
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
53b74f3
fix: toc items orders issue
filzrev fd6774c
test(snapshot): update snapshots d643f62958c90da131e35a613d8dc476ab8a…
filzrev 9097727
chore: add test class for issue10332
filzrev 1b04af4
test(snapshot): update snapshots d6e53ef16df420422b345380b5e6b4e8ed73…
filzrev 42e8d00
Update test/Docfx.Dotnet.Tests/Docfx.Dotnet.Tests.csproj
filzrev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
namespace Docfx.Dotnet; | ||
|
||
/// <summary> | ||
/// StringComparer that simulate <see cref="StringComparer.InvariantCulture"> behavior for ASCII chars. | ||
/// </summary> | ||
/// <remarks> | ||
/// .NET StringComparer ignores non-printable chars on string comparison | ||
/// (e.g. StringComparer.InvariantCulture.Compare("\x0000 ZZZ \x0000"," ZZZ ")) returns 0). | ||
/// This feature is not implement by this comparer. | ||
/// </remarks> | ||
internal sealed class SymbolStringComparer : IComparer<string> | ||
{ | ||
public static readonly SymbolStringComparer Instance = new(); | ||
|
||
private SymbolStringComparer() { } | ||
|
||
public int Compare(string? x, string? y) | ||
{ | ||
if (ReferenceEquals(x, y)) return 0; | ||
if (x == null) return -1; | ||
if (y == null) return 1; | ||
|
||
var xSpan = x.AsSpan(); | ||
var ySpan = y.AsSpan(); | ||
|
||
int minLength = Math.Min(xSpan.Length, ySpan.Length); | ||
|
||
int savedFirstDiff = 0; | ||
for (int i = 0; i < minLength; ++i) | ||
{ | ||
var xChar = xSpan[i]; | ||
var yChar = ySpan[i]; | ||
|
||
if (xChar == yChar) | ||
continue; | ||
|
||
if (char.IsAscii(xChar) && char.IsAscii(yChar)) | ||
{ | ||
// Gets custom char order | ||
var xOrder = AsciiCharSortOrders[xChar]; | ||
var yOrder = AsciiCharSortOrders[yChar]; | ||
|
||
var result = xOrder.CompareTo(yOrder); | ||
if (result == 0) | ||
continue; | ||
|
||
// Custom order logics for method parameters. | ||
if ((xChar == ',' && yChar == ')') || (xChar == ')' && yChar == ',')) | ||
return -result; // Returns result with inverse sign. | ||
|
||
// Save first char case comparison result. (To simulate `StringComparer.InvariantCulture` behavior). | ||
if (char.ToUpper(xChar) == char.ToUpper(yChar)) | ||
{ | ||
if (savedFirstDiff == 0) | ||
savedFirstDiff = result; | ||
continue; | ||
} | ||
|
||
return result; | ||
} | ||
else | ||
{ | ||
// Compare non-ASCII char with ordinal order | ||
int result = xChar.CompareTo(yChar); | ||
if (result != 0) | ||
return result; | ||
} | ||
} | ||
|
||
// Return saved result if case difference exists and string length is same. | ||
if (savedFirstDiff != 0 && x.Length == y.Length) | ||
return savedFirstDiff; | ||
|
||
// Otherwise compare text length. | ||
return x.Length.CompareTo(y.Length); | ||
} | ||
|
||
// ASCII character order lookup table. | ||
// This table is based on StringComparer.InvariantCulture's charactor sort order. | ||
private static readonly byte[] AsciiCharSortOrders = | ||
[ | ||
0, // NUL | ||
0, // SOH | ||
0, // STX | ||
0, // ETX | ||
0, // EOT | ||
0, // ENQ | ||
0, // ACK | ||
0, // BEL | ||
0, // BS | ||
28, // HT (Horizontal Tab) | ||
29, // LF (Line Feed) | ||
30, // VT (Vertical Tab) | ||
31, // FF (Form Feed) | ||
32, // CR (Carriage Return) | ||
0, // SO | ||
0, // SI | ||
0, // DLE | ||
0, // DC1 | ||
0, // DC2 | ||
0, // DC3 | ||
0, // DC4 | ||
0, // NAK | ||
0, // SYN | ||
0, // ETB | ||
0, // CAN | ||
0, // EM | ||
0, // SUB | ||
0, // ESC | ||
0, // FS | ||
0, // GS | ||
0, // RS | ||
0, // US | ||
33, // SP (Space) | ||
39, // ! | ||
43, // " | ||
55, // # | ||
65, // $ | ||
56, // % | ||
54, // & | ||
42, // ' | ||
44, // ( | ||
45, // ) | ||
51, // * | ||
59, // + | ||
36, // , | ||
35, // - | ||
41, // . | ||
52, // / | ||
66, // 0 | ||
67, // 1 | ||
68, // 2 | ||
69, // 3 | ||
70, // 4 | ||
71, // 5 | ||
72, // 6 | ||
73, // 7 | ||
74, // 8 | ||
75, // 9 | ||
38, // : | ||
37, // ; | ||
60, // < | ||
61, // = | ||
62, // > | ||
40, // ? | ||
50, // @ | ||
77, // A | ||
79, // B | ||
81, // C | ||
83, // D | ||
85, // E | ||
87, // F | ||
89, // G | ||
91, // H | ||
93, // I | ||
95, // J | ||
97, // K | ||
99, // L | ||
101, // M | ||
103, // N | ||
105, // O | ||
107, // P | ||
109, // Q | ||
111, // R | ||
113, // S | ||
115, // T | ||
117, // U | ||
119, // V | ||
121, // W | ||
123, // X | ||
125, // Y | ||
127, // Z | ||
46, // [ | ||
53, // \ | ||
47, // ] | ||
58, // ^ | ||
34, // _ | ||
57, // ` | ||
76, // a | ||
78, // b | ||
80, // c | ||
82, // d | ||
84, // e | ||
86, // f | ||
88, // g | ||
90, // h | ||
92, // i | ||
94, // j | ||
96, // k | ||
98, // l | ||
100, // m | ||
102, // n | ||
104, // o | ||
106, // p | ||
108, // q | ||
110, // r | ||
112, // s | ||
114, // t | ||
116, // u | ||
118, // v | ||
120, // w | ||
122, // x | ||
124, // y | ||
126, // z | ||
48, // { | ||
63, // | | ||
49, // } | ||
64, // ~ | ||
0, // ESC | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
test/Docfx.Dotnet.Tests/SymbolStringComparerTest.TestData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
#nullable enable | ||
|
||
namespace Docfx.Dotnet.Tests; | ||
|
||
public partial class SymbolStringComparerTest | ||
{ | ||
private static class TestData | ||
{ | ||
/// <summary> | ||
/// Test data for string array order tests. | ||
/// </summary> | ||
public static TheoryData<string[]> StringArrays => | ||
[ | ||
// Contains underscore | ||
[ | ||
"__", | ||
"__a", | ||
"_1", | ||
"1_", | ||
"a_a", | ||
"A_a", | ||
"a_aa", | ||
"a_ab", | ||
"aaa" | ||
], | ||
// Case differences | ||
[ | ||
"aaa", | ||
"AAA", | ||
"AAA<ABC>", | ||
"AAAA", | ||
"aaab", | ||
], | ||
// Mixed generics | ||
[ | ||
"IRoutedView", | ||
"IRoutedView_`1", | ||
"IRoutedView`1", | ||
"IRoutedView<TViewModel>", | ||
"IRoutedView1", | ||
"IRoutedViewModel", | ||
"Null(object? obj)", | ||
"Null<T>(T obj)", | ||
"NullOrEmpty(string? text)", | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The apiPage output does not use a custom string comparer and seems to sort by
name
directly, does it work to sort by name also formref
output?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ApiPage resolve names with
SymbolFormatter.GetName(symbol, SyntaxLanguage.CSharp)
So name is represented as followings.
So generics are sorted as expected when it's sorted by ordinal .
Because
<
char code is smaller than alphabet char.On the other hand, when using
Mref
output format.name is represented as followings.
So it requires SymbolStringComparer to get expected results.
I thought SymbolStringComparer should be also applied to ApiPage/Markdown output format.