AcTools.Utils.Helpers.AlphanumComparatorFast.Compare C# (CSharp) Метод

Compare() публичный статический Метод

public static Compare ( string x, string y ) : int
x string
y string
Результат int
        public static int Compare(string x, string y) {
            var len1 = x.Length;
            var len2 = y.Length;
            var marker1 = 0;
            var marker2 = 0;

            // Walk through two the strings with two markers.
            while (marker1 < len1 && marker2 < len2) {
                var ch1 = x[marker1];
                var ch2 = y[marker2];

                // Some buffers we can build up characters in for each chunk.
                var space1 = new char[len1];
                var loc1 = 0;
                var space2 = new char[len2];
                var loc2 = 0;

                // Walk through all following characters that are digits or
                // characters in BOTH strings starting at the appropriate marker.
                // Collect char arrays.
                do {
                    space1[loc1++] = ch1;
                    marker1++;

                    if (marker1 < len1) {
                        ch1 = x[marker1];
                    } else {
                        break;
                    }
                } while (char.IsDigit(ch1) == char.IsDigit(space1[0]));

                do {
                    space2[loc2++] = ch2;
                    marker2++;

                    if (marker2 < len2) {
                        ch2 = y[marker2];
                    } else {
                        break;
                    }
                } while (char.IsDigit(ch2) == char.IsDigit(space2[0]));

                // If we have collected numbers, compare them numerically.
                // Otherwise, if we have strings, compare them alphabetically.
                var str1 = new string(space1);
                var str2 = new string(space2);

                int result;

                if (char.IsDigit(space1[0]) && char.IsDigit(space2[0])) {
                    var thisNumericChunk = int.Parse(str1, CultureInfo.InvariantCulture);
                    var thatNumericChunk = int.Parse(str2, CultureInfo.InvariantCulture);
                    result = thisNumericChunk.CompareTo(thatNumericChunk);
                } else {
                    result = string.Compare(str1, str2, StringComparison.Ordinal);
                }

                if (result != 0) {
                    return result;
                }
            }
            return len1 - len2;
        }

Same methods

AlphanumComparatorFast::Compare ( object x, object y ) : int

Usage Example

Пример #1
0
        public static int CompareAsVersionTo([CanBeNull] this string a, [CanBeNull] string b)
        {
            if (a == null)
            {
                return(b == null ? 0 : -1);
            }
            if (b == null)
            {
                return(1);
            }

            var ap = a.Trim().ApartFromFirst("v").Split('.');
            var bp = b.Trim().ApartFromFirst("v").Split('.');

            for (var i = 0; i < ap.Length && i < bp.Length; i++)
            {
                var c = AlphanumComparatorFast.Compare(ap[i].Trim(), bp[i].Trim());
                if (c != 0)
                {
                    return(c);
                }
            }

            return(ap.Length - bp.Length);
        }
AlphanumComparatorFast