Example #1
0
        internal static void rb_str_subpat_set(String str, Regexp re, int nth, object val, Frame caller)
        {
            Match match;
            int start, len;

            if (re.rb_reg_search(str, 0, false, caller) < 0)
            {
                throw new IndexError("regexp not matched").raise(caller);
            }
            match = Regexp.rb_backref_get(caller);
            if (nth >= match.value.Groups.Count)
            {
                throw new IndexError(string.Format(CultureInfo.InvariantCulture, "index {0} out of regexp", nth)).raise(caller);
            }
            if (nth < 0)
            {
                if (-nth >= match.value.Groups.Count)
                {
                    throw new IndexError(string.Format(CultureInfo.InvariantCulture, "index {0} out of regexp", nth)).raise(caller);
                }
                nth += match.value.Groups.Count;
            }

            start = match.value.Groups[nth].Index;
            if (start == -1)
            {
                throw new IndexError(string.Format(CultureInfo.InvariantCulture, "regexp group {0} not matched", nth)).raise(caller);
            }
            len = match.value.Groups[nth].Length;
            rb_str_splice(caller, str, start, len, val);
        }
Example #2
0
        private object scan_once(String str, Regexp pat, ref int start, Frame caller)
        {
            Match match;
            if (pat.rb_reg_search(str, start, false, caller) >= 0)
            {
                match = Regexp.rb_backref_get(caller);

                if (match.value.Length == 0)
                    /*
                     * Always consume at least one character of the input string
                     */
                    start = match.value.Index + 1;
                else
                    start = match.value.Index + match.value.Length;

                if (match.value.Groups.Count == 1)
                    return Regexp.rb_reg_nth_match(0, match);
                else
                {
                    Array result = new Array();
                    for (int i = 1; i < match.value.Groups.Count; i++)
                        result = result.Add(Regexp.rb_reg_nth_match(i, match));
                    return result;
                }
            }
            else
                return null;
        }
Example #3
0
 internal static String rb_str_subpat(String str, Regexp re, int nth, Frame caller)
 {
     if (re.rb_reg_search(str, 0, false, caller) >= 0)
     {
         return Regexp.rb_reg_nth_match(nth, Regexp.rb_backref_get(caller));
     }
     return null;
 }