RangeBlock.cs 1.86 KB
Newer Older
Eric Domke's avatar
Eric Domke committed
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.Collections.Generic;

namespace ExCSS.Model.TextBlocks
{
    internal class RangeBlock : Block
    {
        public RangeBlock()
        {
            GrammarSegment = GrammarSegment.Range;
        }

        internal bool IsEmpty
        {
            get { return SelectedRange == null || SelectedRange.Length == 0; }
        }

        internal string[] SelectedRange { get; private set; }

        internal RangeBlock SetRange(string start, string end)
        {
            var startValue = int.Parse(start, System.Globalization.NumberStyles.HexNumber);

            if (startValue > Specification.MaxPoint)
            {
                return this;
            }

            if (end == null)
            {
                SelectedRange = new [] { char.ConvertFromUtf32(startValue) };
            }
            else
            {
                var list = new List<string>();
                var endValue = int.Parse(end, System.Globalization.NumberStyles.HexNumber);

                if (endValue > Specification.MaxPoint)
                {
                    endValue = Specification.MaxPoint;
                }

                for (; startValue <= endValue; startValue++)
                {
                    list.Add(char.ConvertFromUtf32(startValue));
                }

                SelectedRange = list.ToArray();
            }

            return this;
        }

        public override string ToString()
        {
            if (IsEmpty)
            {
                return string.Empty;
            }

            if (SelectedRange.Length == 1)
            {
                return "#" + char.ConvertToUtf32(SelectedRange[0], 0).ToString("x");
            }

            return "#" + char.ConvertToUtf32(SelectedRange[0], 0).ToString("x") + "-#" + 
                char.ConvertToUtf32(SelectedRange[SelectedRange.Length - 1], 0).ToString("x");
        }
    }
}