~ubuntu-branches/ubuntu/hoary/monodevelop/hoary

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/SourceEditor/Search/SearchStrategy/WildcardSearchStrategy.cs

  • Committer: Bazaar Package Importer
  • Author(s): Brandon Hale
  • Date: 2004-10-07 11:51:11 UTC
  • Revision ID: james.westby@ubuntu.com-20041007115111-pxcqnwfxyq5mhcx5
Tags: 0.5.1-3
Use dh_netdeps in debian/rules and debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// <file>
 
2
//     <copyright see="prj:///doc/copyright.txt"/>
 
3
//     <license see="prj:///doc/license.txt"/>
 
4
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
 
5
//     <version value="$version"/>
 
6
// </file>
 
7
 
 
8
using System;
 
9
using System.Collections;
 
10
 
 
11
using MonoDevelop.Core.Properties;
 
12
using MonoDevelop.Internal.Undo;
 
13
 
 
14
using MonoDevelop.SourceEditor.Gui;
 
15
 
 
16
namespace MonoDevelop.TextEditor.Document
 
17
{
 
18
        /// <summary>
 
19
        /// Implements a wildcard search strategy.
 
20
        /// 
 
21
        /// Wildcard search has following pattern code :
 
22
        ///      * = Zero or more of any character
 
23
        ///      ? = Any single character
 
24
        ///      # = Any single digit
 
25
        ///  [...] = Any one character in the set
 
26
        /// [!...] = Any one character not in the set
 
27
        /// </summary>
 
28
        public class WildcardSearchStrategy : ISearchStrategy
 
29
        {
 
30
                enum CommandType {
 
31
                        Match,
 
32
                        AnyZeroOrMore,
 
33
                        AnySingle,
 
34
                        AnyDigit,
 
35
                        AnyInList,
 
36
                        NoneInList
 
37
                }
 
38
                
 
39
                class Command {
 
40
                        public CommandType CommandType = CommandType.Match;
 
41
                        public char        SingleChar  = '\0';
 
42
                        public string      CharList    = String.Empty;
 
43
                }
 
44
                
 
45
                ArrayList patternProgram = null;
 
46
                int curMatchCharCount = 0;
 
47
                
 
48
                void CompilePattern(string pattern, bool ignoreCase)
 
49
                {
 
50
                        patternProgram = new ArrayList();
 
51
                        for (int i = 0; i < pattern.Length; ++i) {
 
52
                                Command newCommand = new Command();
 
53
                                switch (pattern[i]) {
 
54
                                        case '#':
 
55
                                                newCommand.CommandType = CommandType.AnyDigit;
 
56
                                                break;
 
57
                                        case '*':
 
58
                                                newCommand.CommandType = CommandType.AnyZeroOrMore;
 
59
                                                break;
 
60
                                        case '?':
 
61
                                                newCommand.CommandType = CommandType.AnySingle;
 
62
                                                break;
 
63
                                        case '[':
 
64
                                                int index = pattern.IndexOf(']', i);
 
65
                                                if (index > 0) {
 
66
                                                        newCommand.CommandType = CommandType.AnyInList;
 
67
                                                        string list = pattern.Substring(i + 1, index - i - 1);
 
68
                                                        if (list[0] == '!') {
 
69
                                                                newCommand.CommandType = CommandType.NoneInList;
 
70
                                                                list = list.Substring(1);
 
71
                                                        }
 
72
                                                        newCommand.CharList = ignoreCase ? list.ToUpper() : list;
 
73
                                                        i = index;
 
74
                                                } else {
 
75
                                                        goto default;
 
76
                                                }
 
77
                                                break;
 
78
                                        default:
 
79
                                                newCommand.CommandType = CommandType.Match;
 
80
                                                newCommand.SingleChar  = ignoreCase ? Char.ToUpper(pattern[i]) : pattern[i];
 
81
                                                break;
 
82
                                }
 
83
                                patternProgram.Add(newCommand);
 
84
                        }
 
85
                }
 
86
                
 
87
                int Match (ITextIterator textIterator, bool ignoreCase, int  programStart)
 
88
                {
 
89
                        int matchCharCount = 0;
 
90
                        bool moreChars = true;
 
91
                        for (int pc = programStart; pc < patternProgram.Count; ++pc) 
 
92
                        {
 
93
                                if (!moreChars) return -1;
 
94
                                
 
95
                                char    ch  = ignoreCase ? Char.ToUpper(textIterator.Current) : textIterator.Current;
 
96
                                Command cmd = (Command)patternProgram[pc];
 
97
                                
 
98
                                switch (cmd.CommandType) {
 
99
                                        case CommandType.Match:
 
100
                                                if (ch != cmd.SingleChar) {
 
101
                                                        return -1;
 
102
                                                }
 
103
                                                break;
 
104
                                        case CommandType.AnyZeroOrMore:
 
105
                                                int p = textIterator.Position;
 
106
                                                int subMatch = Match (textIterator, ignoreCase, pc + 1);
 
107
                                                if (subMatch != -1) return matchCharCount + subMatch;
 
108
                                                textIterator.Position = p;
 
109
                                                if (!textIterator.MoveAhead (1)) return -1;
 
110
                                                subMatch = Match (textIterator, ignoreCase, pc);
 
111
                                                if (subMatch != -1) return matchCharCount + subMatch;
 
112
                                                else return -1;
 
113
                                        case CommandType.AnySingle:
 
114
                                                break;
 
115
                                        case CommandType.AnyDigit:
 
116
                                                if (!Char.IsDigit(ch)) {
 
117
                                                        return -1;
 
118
                                                }
 
119
                                                break;
 
120
                                        case CommandType.AnyInList:
 
121
                                                if (cmd.CharList.IndexOf(ch) < 0) {
 
122
                                                        return -1;
 
123
                                                }
 
124
                                                break;
 
125
                                        case CommandType.NoneInList:
 
126
                                                if (cmd.CharList.IndexOf(ch) >= 0) {
 
127
                                                        return -1;
 
128
                                                }
 
129
                                                break;
 
130
                                }
 
131
                                matchCharCount++;
 
132
                                moreChars = textIterator.MoveAhead (1);
 
133
                        }
 
134
                        return matchCharCount;
 
135
                }
 
136
                
 
137
                int InternalFindNext(ITextIterator textIterator, SearchOptions options)
 
138
                {
 
139
                        while (textIterator.MoveAhead(1)) 
 
140
                        {
 
141
                                int pos = textIterator.Position;
 
142
                                int charCount = Match (textIterator, options.IgnoreCase, 0);
 
143
                                textIterator.Position = pos;
 
144
                                if (charCount != -1) {
 
145
                                        if (!options.SearchWholeWordOnly || SearchReplaceUtilities.IsWholeWordAt (textIterator, charCount))
 
146
                                                return charCount;
 
147
                                }
 
148
                        }
 
149
                        return -1;
 
150
                }
 
151
                
 
152
                public void CompilePattern(SearchOptions options)
 
153
                {
 
154
                        CompilePattern(options.SearchPattern, options.IgnoreCase);
 
155
                }
 
156
                
 
157
                public ISearchResult FindNext(ITextIterator textIterator, SearchOptions options)
 
158
                {
 
159
                        int charCount = InternalFindNext(textIterator, options);
 
160
                        return charCount != -1 ? new DefaultSearchResult (textIterator.Position, charCount) : null;
 
161
                }
 
162
        }
 
163
}