Пример #1
0
 public bool Overlaps(RangeSet other)
 {
     for (var i = 0; i < SetRanges.Length; i++)
     {
         for (var j = 0; j < other.SetRanges.Length; j++)
         {
             if (SetRanges[i].Contains(other.SetRanges[j].Start) || SetRanges[i].Contains(other.SetRanges[j].Stop))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Пример #2
0
        public static bool TryParse(ref string error, string rangeString, out RangeSet output)
        {
            var tempRange = new List <Range>();
            var length    = rangeString.Length;
            var str       = rangeString.ToCharArray();
            var index     = 0;
            var start     = 0;
            var end       = 0;

            output = null;
            //Phase == 0 -> index
            //Phase == 1 -> start
            //Phase == 2 -> end
            var phase          = 0;
            var lastPlus       = false;
            var tallyingInZero = false;

            if (IsNullOrWhiteSpace(rangeString))
            {
                output = new RangeSet(tempRange);
                return(true);
            }
            for (var i = 0; i < length; i++)
            {
                var c = str[i];
                if (IsWhiteSpace(c) || IsLetter(c))
                {
                    continue;
                }
                lastPlus = false;
                switch (phase)
                {
                case 0:
                    if (IsNumber(c))
                    {
                        index          = ((index << 3) + (index << 1)) + (c - '0');
                        tallyingInZero = true;
                    }
                    else
                    {
                        switch (c)
                        {
                        case ',':
                            tempRange.Add(new Range(index, index));
                            index = 0;
                            start = 0;
                            end   = 0;
                            break;

                        case '-':
                            if (!tallyingInZero)
                            {
                                error = "No number was inserted before a range!";
                                return(false);
                            }
                            start = index;
                            end   = 0;
                            phase = 2;
                            break;

                        case '+':
                            if (!tallyingInZero)
                            {
                                error = "No number was inserted before a range!";
                                return(false);
                            }
                            end = int.MaxValue;
                            tempRange.Add(new Range(start, end));
                            index          = 0;
                            start          = 0;
                            phase          = 0;
                            tallyingInZero = false;
                            lastPlus       = true;
                            break;

                        default:
                            error = "Unrecognized symbol " + c;
                            return(false);
                        }
                    }
                    break;

                case 1:
                    if (IsNumber(c))
                    {
                        start = ((start << 3) + (start << 1)) + (c - '0');
                    }
                    else
                    {
                        switch (c)
                        {
                        case '+':
                            end = int.MaxValue;
                            tempRange.Add(new Range(start, end));
                            index          = 0;
                            start          = 0;
                            phase          = 0;
                            tallyingInZero = false;
                            lastPlus       = true;
                            break;

                        case '-':
                            end   = 0;
                            phase = 2;
                            break;
                        }
                    }
                    break;

                case 2:
                    if (IsNumber(c))
                    {
                        end = ((end << 3) + (end << 1)) + (c - '0');
                    }
                    else if (c == ',')
                    {
                        tempRange.Add(new Range(start, end));
                        index          = 0;
                        phase          = 0;
                        start          = 0;
                        end            = 0;
                        tallyingInZero = false;
                    }
                    break;
                }
            }
            if (phase == 2)
            {
                tempRange.Add(new Range(start, end));
            }
            else if (phase == 0 && tallyingInZero)
            {
                tempRange.Add(new Range(index, index));
            }
            else if (!lastPlus)
            {
                error = "Ended while reading a " + (phase == 0 ? "range's index!" : "range's start value!");
                return(false);
            }
            output = new RangeSet(tempRange);
            return(true);
        }
Пример #3
0
        public static bool TryParse(string rangeString, out RangeSet output)
        {
            string error = null;

            return(TryParse(ref error, rangeString, out output));
        }