~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/BackendBindings/XamlBinding/XamlBinding/PropertyPathTokenizer.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Linq;
 
6
using System.Collections.Generic;
 
7
 
 
8
namespace ICSharpCode.XamlBinding
 
9
{
 
10
        public class PropertyPathTokenizer
 
11
        {
 
12
                string value;
 
13
                int offset;
 
14
                
 
15
                public static readonly char[] ControlChars = new char[] { '.', ',', '(', ')', '[', ']', '/' };
 
16
                
 
17
                PropertyPathTokenizer(string value)
 
18
                {
 
19
                        this.value = value;
 
20
                        this.offset = 0;
 
21
                }
 
22
                
 
23
                bool NextToken(out string token)
 
24
                {
 
25
                        token = "";
 
26
                        
 
27
                        if (MoveToNext()) {
 
28
                                switch (value[offset]) {
 
29
                                        case '.':
 
30
                                        case '(':
 
31
                                        case ')':
 
32
                                        case '[':
 
33
                                        case ']':
 
34
                                        case ',':
 
35
                                        case '/':
 
36
                                                token = value[offset].ToString();
 
37
                                                offset++;
 
38
                                                return true;
 
39
                                        default:
 
40
                                                string text = "";
 
41
                                                while (!AtEnd() && char.IsLetterOrDigit(value[offset])) {
 
42
                                                        text += value[offset];
 
43
                                                        offset++;
 
44
                                                }
 
45
                                                
 
46
                                                token = text;
 
47
                                                return true;
 
48
                                }
 
49
                        }
 
50
                        
 
51
                        return false;
 
52
                }
 
53
                
 
54
                bool MoveToNext()
 
55
                {
 
56
                        // skip all invalid chars
 
57
                        while (!AtEnd() && !char.IsLetterOrDigit(value[offset]) && !ControlChars.Contains(value[offset]))
 
58
                                offset++;
 
59
                        
 
60
                        return !AtEnd();
 
61
                }
 
62
                
 
63
                bool AtEnd()
 
64
                {
 
65
                        return offset >= value.Length;
 
66
                }
 
67
                
 
68
                public static IEnumerable<string> Tokenize(string value)
 
69
                {
 
70
                        if (value == null)
 
71
                                throw new ArgumentNullException("value");
 
72
                        
 
73
                        PropertyPathTokenizer tokenizer = new PropertyPathTokenizer(value);
 
74
                        
 
75
                        string token;
 
76
                        
 
77
                        while (tokenizer.NextToken(out token))
 
78
                                yield return token;
 
79
                }
 
80
        }
 
81
}