123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- using System;
- using System.Collections.Generic;
- namespace COM3D2.MeidoPhotoStudio.Plugin
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class LexicographicStringComparer : IComparer<string>
- {
-
-
-
- public static int Comparison(string x, string y)
- {
-
-
- if (x == y) return 0;
- if (string.IsNullOrEmpty(x) && !string.IsNullOrEmpty(y)) return -1;
- if (!string.IsNullOrEmpty(x) && string.IsNullOrEmpty(y)) return 1;
- if (string.IsNullOrEmpty(x) && string.IsNullOrEmpty(y)) return 0;
- var yl = y.Length;
- for (int i = 0; i < x.Length; i++)
- {
- if (yl <= i) return 1;
- var cx = x[i];
- var cy = y[i];
- if (Char.IsWhiteSpace(cx) && !Char.IsWhiteSpace(cy)) return -1;
- if (!Char.IsWhiteSpace(cx) && Char.IsWhiteSpace(cy)) return 1;
- if (IsDigit(cx))
- {
- if (!IsDigit(cy))
- {
- return -1;
- }
-
-
- var numCmp = CompareNumbers(x, y, i, out int numChars);
- if (numCmp != 0) return numCmp;
- i += numChars;
- }
- else if (IsDigit(cy))
- {
- return 1;
- }
- else
- {
-
-
-
-
- var cmp = Char.ToUpper(cx).CompareTo(Char.ToUpper(cy));
- if (cmp != 0) return cmp;
- }
- }
-
- if (y.Length > x.Length) return -1;
- return 0;
- }
-
-
-
- public int Compare(string x, string y)
- => Comparison(x, y);
- private static int CompareNumbers(string x, string y, int ix, out int numChars)
- {
- var xParsed = ParseNumber(x, ix);
- var yParsed = ParseNumber(y, ix);
- numChars = yParsed.NumCharsRead > xParsed.NumCharsRead
- ? xParsed.NumCharsRead
- : yParsed.NumCharsRead;
- return xParsed.CompareTo(yParsed);
- }
- private static ParsedNumber ParseNumber(string str, int offset)
- {
- var result = 0;
- var numChars = 0;
- var leadingZeroes = 0;
- var numOverflows = 0;
- bool countZeroes = true;
- for (int j = offset; j < str.Length; j++)
- {
- char c = str[j];
- if (IsDigit(c))
- {
- var cInt = (c - 48);
- checked
- {
- long tmp = (result * 10L) + cInt;
- if (tmp > int.MaxValue)
- {
- numOverflows++;
- tmp = tmp % int.MaxValue;
- }
- result = (int)tmp;
- numChars++;
- }
- if (cInt == 0 && countZeroes)
- {
- leadingZeroes++;
- }
- else
- {
- countZeroes = false;
- }
- }
- else
- {
- break;
- }
- }
- return new ParsedNumber(result, numOverflows, leadingZeroes, numChars);
- }
- private static bool IsDigit(char c) => (c >= '0' && c <= '9');
-
-
-
-
- private struct ParsedNumber : IComparable<ParsedNumber>, IComparer<ParsedNumber>
- {
-
-
-
-
- public int Remainder;
-
-
-
- public int Overflows;
-
-
-
-
-
-
-
-
- public int LeadingZeroesCount;
-
-
-
- public int NumCharsRead;
- public ParsedNumber(int remainder, int overflows, int leadingZeroes, int numChars)
- {
- Remainder = remainder;
- Overflows = overflows;
- LeadingZeroesCount = leadingZeroes;
- NumCharsRead = numChars;
- }
- public int Compare(ParsedNumber x, ParsedNumber y)
- {
-
-
- if (x.Overflows > y.Overflows) return 1;
- if (x.Overflows < y.Overflows) return -1;
-
- if (x.Remainder == y.Remainder)
- {
- if (x.LeadingZeroesCount > y.LeadingZeroesCount) return -1;
- if (x.LeadingZeroesCount < y.LeadingZeroesCount) return 1;
- }
- if (x.Remainder > y.Remainder) return 1;
- if (x.Remainder < y.Remainder) return -1;
- return 0;
- }
- public int CompareTo(ParsedNumber other)
- => Compare(this, other);
- }
- }
- }
|