예제 #1
0
        public void parse(String input, String expected)
        {
            List <IndexRange> ranges = IndexRange.parse(input);
            String            actual = String.Join("|", ranges);

            Assert.Equal(expected, actual);
        }
예제 #2
0
        private void compileFlags(String flags)
        {
            if (String.IsNullOrEmpty(flags))
            {
                replaceIndex = 1;
                return;
            }

            Match match = FLAG_PATTERN.Match(flags);

            if (!match.Success)
            {
                throw new InvalidArgumentException("Invalid flags: {0}", flags);
            }

            String textFlags  = match.Groups[1].Value;
            String textRanges = match.Groups[2].Value;

            ignoreCase = textFlags.Contains("i");
            global     = textFlags.Contains("g");

            List <IndexRange> ranges = IndexRange.parse(textRanges);

            if (ranges.Count == 1 && ranges[0].isSingleIndex())
            {
                replaceIndex = ranges[0].low;
                if (replaceIndex <= 0)
                {
                    throw new InvalidArgumentException("Invalid index: {0}. Must be greater than zero", replaceIndex);
                }
            }
            else if (ranges.Count > 0)
            {
                if (global)
                {
                    throw new InvalidArgumentException("Global flag cannot be used with a range of replacement indexes");
                }

                replaceRanges = ranges;

                foreach (IndexRange toCheck in replaceRanges)
                {
                    if (toCheck.high < toCheck.low)
                    {
                        throw new InvalidArgumentException("Invalid range: {0}. End index must be greater than start index", toCheck);
                    }

                    if (toCheck.low <= 0)
                    {
                        throw new InvalidArgumentException("Invalid index: {0}. Must be greater than zero", toCheck.low);
                    }
                }
            }

            if (replaceIndex == null && replaceRanges == null && !global)
            {
                replaceIndex = 1;            // defaults to replace first match, if nothing is explicitly specified
            }
        }