Comparisons.cs
author Thomas
Thu, 13 Apr 2017 16:54:58 +0200
changeset 1638 501e8e24a249
parent 1290 44776fed0bd2
permissions -rw-r--r--
OUT-250: Add fallback with search for a list of known prefixes (can be extended).
Dean@1290
     1
´╗┐using System.Collections;
Dean@1238
     2
using System.Collections.Generic;
Dean@1290
     3
using System.Linq;
Dean@1238
     4
Dean@1238
     5
namespace pEp
Dean@1238
     6
{
Dean@1238
     7
    public static class Comparisons
Dean@1238
     8
    {
Dean@1238
     9
        /// <summary>
Dean@1238
    10
        /// Compares the given values and checks for equality.
Dean@1238
    11
        /// Null and empty cases are handled.
Dean@1238
    12
        /// </summary>
Dean@1238
    13
        /// <param name="value1">The first value to compare.</param>
Dean@1238
    14
        /// <param name="value2">The second value to compare with.</param>
Dean@1238
    15
        /// <returns>True if the values are considered equal, otherwise false.</returns>
Dean@1238
    16
        new public static bool Equals(object value1,
Dean@1238
    17
                                      object value2)
Dean@1238
    18
        {
Dean@1290
    19
            if ((value1 is IEnumerable) &&
Dean@1290
    20
                (value2 is IEnumerable))
Dean@1290
    21
            {
Dean@1290
    22
                return (Comparisons.Equals((IEnumerable)value1, (IEnumerable)value2));
Dean@1290
    23
            }
Dean@1290
    24
            else
Dean@1290
    25
            {
Dean@1290
    26
                return (object.Equals(value1, value2));
Dean@1290
    27
            }
Dean@1290
    28
        }
Dean@1290
    29
Dean@1290
    30
        /// <summary>
Dean@1290
    31
        /// Compares the given values and checks for equality.
Dean@1290
    32
        /// Null and empty cases are handled.
Dean@1290
    33
        /// </summary>
Dean@1290
    34
        /// <param name="value1">The first value to compare.</param>
Dean@1290
    35
        /// <param name="value2">The second value to compare with.</param>
Dean@1290
    36
        /// <returns>True if the values are considered equal, otherwise false.</returns>
Dean@1290
    37
        public static bool Equals(IEnumerable value1,
Dean@1290
    38
                                  IEnumerable value2)
Dean@1290
    39
        {
Dean@1290
    40
            bool exists1;
Dean@1290
    41
            bool exists2;
Dean@1290
    42
            IEnumerator enumerator1;
Dean@1290
    43
            IEnumerator enumerator2;
Dean@1290
    44
Dean@1290
    45
            if ((value1 == null) ||
Dean@1290
    46
                (value2 == null))
Dean@1290
    47
            {
Dean@1290
    48
                if ((value1 != null) ||
Dean@1290
    49
                    (value2 != null))
Dean@1290
    50
                {
Dean@1290
    51
                    // One is null while the other is not
Dean@1290
    52
                    return (false);
Dean@1290
    53
                }
Dean@1290
    54
            }
Dean@1290
    55
            else
Dean@1290
    56
            {
Dean@1290
    57
                enumerator1 = value1.GetEnumerator();
Dean@1290
    58
                enumerator2 = value2.GetEnumerator();
Dean@1290
    59
Dean@1290
    60
                try
Dean@1290
    61
                {
Dean@1290
    62
                    exists1 = enumerator1.MoveNext();
Dean@1290
    63
                    exists2 = enumerator2.MoveNext();
Dean@1290
    64
Dean@1290
    65
                    while (exists1 && exists2)
Dean@1290
    66
                    {
Dean@1290
    67
                        if (Comparisons.Equals(enumerator1.Current, enumerator2.Current) == false)
Dean@1290
    68
                        {
Dean@1290
    69
                            return (false);
Dean@1290
    70
                        }
Dean@1290
    71
Dean@1290
    72
                        exists1 = enumerator1.MoveNext();
Dean@1290
    73
                        exists2 = enumerator2.MoveNext();
Dean@1290
    74
                    }
Dean@1290
    75
Dean@1290
    76
                    // If one of the enumerators still has a value, the lengths don't match
Dean@1290
    77
                    if (exists1 || exists2)
Dean@1290
    78
                    {
Dean@1290
    79
                        return (false);
Dean@1290
    80
                    }
Dean@1290
    81
                }
Dean@1290
    82
                catch
Dean@1290
    83
                {
Dean@1290
    84
                    return (false);
Dean@1290
    85
                }
Dean@1290
    86
            }
Dean@1290
    87
Dean@1290
    88
            return (true);
Dean@1238
    89
        }
Dean@1238
    90
Dean@1238
    91
        /// <summary>
Dean@1238
    92
        /// Compares the given values and checks for equality.
Dean@1238
    93
        /// Null and empty cases are handled.
Dean@1238
    94
        /// </summary>
Dean@1238
    95
        /// <param name="value1">The first value to compare.</param>
Dean@1238
    96
        /// <param name="value2">The second value to compare with.</param>
Dean@1238
    97
        /// <returns>True if the values are considered equal, otherwise false.</returns>
Dean@1238
    98
        public static bool Equals<T>(IEnumerable<T> value1,
Dean@1238
    99
                                     IEnumerable<T> value2)
Dean@1238
   100
        {
Dean@1238
   101
            if ((value1 == null) ||
Dean@1238
   102
                (value2 == null))
Dean@1238
   103
            {
Dean@1238
   104
                if ((value1 != null) ||
Dean@1238
   105
                    (value2 != null))
Dean@1238
   106
                {
Dean@1238
   107
                    // One is null while the other is not
Dean@1238
   108
                    return (false);
Dean@1238
   109
                }
Dean@1238
   110
            }
Dean@1238
   111
            else
Dean@1238
   112
            {
Dean@1238
   113
                return (Enumerable.SequenceEqual(value1, value2));
Dean@1238
   114
            }
Dean@1238
   115
Dean@1238
   116
            return (true);
Dean@1238
   117
        }
Dean@1238
   118
Dean@1289
   119
        /// <summary>
Dean@1289
   120
        /// Compares the given values and checks for equality.
Dean@1289
   121
        /// Null and empty cases are handled.
Dean@1289
   122
        /// </summary>
Dean@1289
   123
        /// <param name="value1">The first value to compare.</param>
Dean@1289
   124
        /// <param name="value2">The second value to compare with.</param>
Dean@1289
   125
        /// <param name="ignoreSequence">Whether to ignore the sequence of items in the list. If true, this
Dean@1289
   126
        /// will only check that the same value occurs an equal number of times in both lists.</param>
Dean@1289
   127
        /// <returns>True if the values are considered equal, otherwise false.</returns>
Dean@1289
   128
        public static bool Equals<T>(IEnumerable<T> value1,
Dean@1289
   129
                                     IEnumerable<T> value2,
Dean@1289
   130
                                     bool ignoreSequence)
Dean@1289
   131
        {
Dean@1289
   132
            long count;
Dean@1289
   133
            bool[] isCounted;
Dean@1289
   134
Dean@1289
   135
            if (ignoreSequence == false)
Dean@1289
   136
            {
Dean@1289
   137
                return (Comparisons.Equals(value1, value2));
Dean@1289
   138
            }
Dean@1289
   139
            else
Dean@1289
   140
            {
Dean@1289
   141
                if ((value1 == null) ||
Dean@1289
   142
                    (value2 == null))
Dean@1289
   143
                {
Dean@1289
   144
                    if ((value1 != null) ||
Dean@1289
   145
                        (value2 != null))
Dean@1289
   146
                    {
Dean@1289
   147
                        // One is null while the other is not
Dean@1289
   148
                        return (false);
Dean@1289
   149
                    }
Dean@1289
   150
                }
Dean@1289
   151
                else if (value1.Count() != value2.Count())
Dean@1289
   152
                {
Dean@1289
   153
                    return (false);
Dean@1289
   154
                }
Dean@1289
   155
                else
Dean@1289
   156
                {
Dean@1289
   157
                    count = 0;
Dean@1289
   158
                    isCounted = new bool[value2.Count()]; // filled with false
Dean@1289
   159
Dean@1289
   160
                    // Compare every element
Dean@1289
   161
                    for (int i = 0; i < value1.Count(); i++)
Dean@1289
   162
                    {
Dean@1289
   163
                        for (int j = 0; j < value2.Count(); j++)
Dean@1289
   164
                        {
Dean@1289
   165
                            if ((value1.ElementAt(i).Equals(value2.ElementAt(j))) &&
Dean@1289
   166
                                (isCounted[j] == false))
Dean@1289
   167
                            {
Dean@1289
   168
                                count++;
Dean@1289
   169
                                isCounted[j] = true;
Dean@1289
   170
                                break;
Dean@1289
   171
                            }
Dean@1289
   172
                        }
Dean@1289
   173
                    }
Dean@1289
   174
Dean@1289
   175
                    if (count != value1.Count())
Dean@1289
   176
                    {
Dean@1289
   177
                        return (false);
Dean@1289
   178
                    }
Dean@1289
   179
                }
Dean@1289
   180
            }
Dean@1289
   181
Dean@1289
   182
            return (true);
Dean@1289
   183
        }
Thomas@1638
   184
Thomas@1638
   185
        /// <summary>
Thomas@1638
   186
        /// Comparison of two file paths for equality.
Thomas@1638
   187
        /// This is done by checking if the file path strings are equal
Thomas@1638
   188
        /// independently of a set of known prefixes.
Thomas@1638
   189
        /// See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247
Thomas@1638
   190
        /// </summary>
Thomas@1638
   191
        /// <param name="filePath1">The first file path.</param>
Thomas@1638
   192
        /// <param name="filePath2">The second file path.</param>
Thomas@1638
   193
        /// <returns>True if file names are equal</returns>
Thomas@1638
   194
        public static bool FilePathsAreEqual(string filePath1,
Thomas@1638
   195
                                             string filePath2)
Thomas@1638
   196
        {
Thomas@1638
   197
            bool areEqual = false;
Thomas@1638
   198
Thomas@1638
   199
            // A list of known prefixes
Thomas@1638
   200
            List<string> prefixes = new List<string>
Thomas@1638
   201
            {
Thomas@1638
   202
                @"\\?\",
Thomas@1638
   203
                @"\\.\",
Thomas@1638
   204
                @"\??\"
Thomas@1638
   205
            };
Thomas@1638
   206
Thomas@1638
   207
            // If one of the two paths is null, return false
Thomas@1638
   208
            if ((filePath1 == null) ||
Thomas@1638
   209
                (filePath2 == null))
Thomas@1638
   210
            {
Thomas@1638
   211
                return false;
Thomas@1638
   212
            }
Thomas@1638
   213
Thomas@1638
   214
            // Compare strings with and without prepended prefixes
Thomas@1638
   215
            foreach (var prefix in prefixes)
Thomas@1638
   216
            {
Thomas@1638
   217
                if ((filePath1 == filePath2) ||
Thomas@1638
   218
                    (prefix + filePath1 == filePath2) ||
Thomas@1638
   219
                    (filePath1 == prefix + filePath2))
Thomas@1638
   220
                {
Thomas@1638
   221
                    areEqual = true;
Thomas@1638
   222
                    break;
Thomas@1638
   223
                }
Thomas@1638
   224
            }
Thomas@1638
   225
Thomas@1638
   226
            return areEqual;
Thomas@1638
   227
        }
Dean@1238
   228
    }
Dean@1238
   229
}