jagregory / boolangstudio forked from olsonjeffery/boolangstudio

Boo language integration for Visual Studio 2008

This URL has Read+Write access

boolangstudio / Source / BooLangService / StringParsing / ExcludeStringMatcher.cs
100644 45 lines (39 sloc) 1.292 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
namespace Boo.BooLangService.StringParsing
{
    /// <summary>
    ///
    /// </summary>
    /// <remarks>
    /// What does this name even mean?! I walk a string, looking for a match to what you asked for that ISN'T inside a string declaration.
    ///
    /// e.g. Find the next closing parenthesis, starting from the first opening paren.
    ///
    /// MyFun(x, "some string ()");
    ///
    /// This would find the last paren, NOT the one inside the string.
    /// </remarks>
    public class ExcludeStringMatcher
    {
        private readonly string source;
        private int startIndex;
 
        public ExcludeStringMatcher(string source)
        {
            this.source = source;
        }
 
        public int? FindNextIndex(char value)
        {
            var stringWalker = new StringWalker();
            var startingSource = source.Substring(startIndex);
 
            foreach (var position in stringWalker.Iterate(startingSource))
            {
                if (position.Character == value && !stringWalker.StateIs(StringWalkerState.InsideString))
                    return position.Index + startIndex;
            }
 
            return null;
        }
 
        public void SetStartIndex(int index)
        {
            startIndex = index;
        }
    }
}