ProjectArcade

Форк
0
160 строк · 6.0 Кб
1
using System.Linq;
2

3
namespace DokanNet
4
{
5
    /// <summary>
6
    /// %Dokan functions helpers for user <see cref="IDokanOperations"/> implementation.
7
    /// </summary>
8
    public static class DokanHelper
9
    {
10
        /// <summary>
11
        /// Matches zero or more characters until encountering and matching the final . in the name.
12
        /// </summary>
13
        private const char DOS_STAR = '<';
14

15
        /// <summary>
16
        /// Matches any single character or, upon encountering a period or end
17
        /// of name string, advances the expression to the end of the set of
18
        /// contiguous DOS_QMs.
19
        /// </summary>
20
        private const char DOS_QM = '>';
21

22
        /// <summary>
23
        /// Matches either a period or zero characters beyond the name string.
24
        /// </summary>
25
        private const char DOS_DOT = '"';
26

27
        /// <summary>
28
        /// Matches zero or more characters.
29
        /// </summary>
30
        private const char ASTERISK = '*';
31

32
        /// <summary>
33
        /// Matches a single character.
34
        /// </summary>
35
        private const char QUESTION_MARK = '?';
36

37
        private readonly static char[] CharsThatMatchEmptyStringsAtEnd = { DOS_DOT, DOS_STAR, ASTERISK };
38

39
        /// <summary>
40
        /// Check whether <paramref name="name">Name</paramref> matches <paramref name="expression">Expression</paramref>.
41
        /// </summary>
42
        /// <remarks>
43
        /// This method is mainly used in <see cref="IDokanOperations.FindFilesWithPattern"/> to filter a list of possible files.
44
        /// For example "F0_&lt;&quot;*" match "f0_001.txt"
45
        /// \see <a href="http://msdn.microsoft.com/en-us/library/ff546850(v=VS.85).aspx">See FsRtlIsNameInExpression routine (MSDN)</a>
46
        /// </remarks>
47
        /// <param name="expression">The matching pattern. Can contain: ?, *, &lt;, &quot;, &gt;.</param>
48
        /// <param name="name">The string that will be tested.</param>
49
        /// <param name="ignoreCase">When set to true a case insensitive match will be performed.</param>
50
        /// <returns>Returns true if Expression match Name, false otherwise.</returns>
51
        public static bool DokanIsNameInExpression(string expression, string name, bool ignoreCase)
52
        {
53
            var ei = 0;
54
            var ni = 0;
55

56
            while (ei < expression.Length && ni < name.Length)
57
            {
58
                switch (expression[ei])
59
                {
60
                    case ASTERISK:
61
                        ei++;
62
                        if (ei > expression.Length)
63
                            return true;
64

65
                        while (ni < name.Length)
66
                        {
67
                            if (DokanIsNameInExpression(expression.Substring(ei), name.Substring(ni), ignoreCase))
68
                                return true;
69
                            ni++;
70
                        }
71

72
                        break;
73
                    case DOS_STAR:
74
                        var lastDotIndex = name.LastIndexOf('.');
75
                        ei++;
76

77
                        var endReached = false;
78
                        while (!endReached)
79
                        {
80
                            endReached = (ni >= name.Length || lastDotIndex > -1 && ni > lastDotIndex);
81

82
                            if (!endReached)
83
                            {
84
                                if (DokanIsNameInExpression(expression.Substring(ei), name.Substring(ni), ignoreCase))
85
                                    return true;
86
                                ni++;
87
                            }
88
                        }
89

90
                        break;
91
                    case DOS_QM:
92
                        ei++;
93
                        if (name[ni] != '.')
94
                        {
95
                            ni++;
96
                        }
97
                        else
98
                        {
99
                            var p = ni + 1;
100
                            while (p < name.Length)
101
                            {
102
                                if (name[p] == '.')
103
                                    break;
104
                                p++;
105
                            }
106

107
                            if (p < name.Length && name[p] == '.')
108
                                ni++;
109
                        }
110

111
                        break;
112
                    case DOS_DOT:
113
                        if (ei < expression.Length)
114
                        {
115
                            if (name[ni] != '.')
116
                                return false;
117
                            else
118
                                ni++;
119
                        }
120
                        else
121
                        {
122
                            if (name[ni] == '.')
123
                                ni++;
124
                        }
125
                        ei++;
126
                        break;
127
                    case QUESTION_MARK:
128
                        ei++;
129
                        ni++;
130
                        break;
131
                    default:
132
                        if (ignoreCase && char.ToUpperInvariant(expression[ei]) == char.ToUpperInvariant(name[ni]))
133
                        {
134
                            ei++;
135
                            ni++;
136
                        }
137
                        else if (!ignoreCase && expression[ei] == name[ni])
138
                        {
139
                            ei++;
140
                            ni++;
141
                        }
142
                        else
143
                        {
144
                            return false;
145
                        }
146

147
                        break;
148
                }
149
            }
150

151
            var nextExpressionChars = expression.Substring(ei);
152
            var areNextExpressionCharsAllNullMatchers = expression.Any() && !string.IsNullOrEmpty(nextExpressionChars) && nextExpressionChars.All(x => CharsThatMatchEmptyStringsAtEnd.Contains(x));
153
            var isNameCurrentCharTheLast = ni == name.Length;
154
            if (ei == expression.Length && isNameCurrentCharTheLast || isNameCurrentCharTheLast && areNextExpressionCharsAllNullMatchers)
155
                return true;
156

157
            return false;
158
        }
159
    }
160
}
161

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.