Example #1
0
        public RString SubAt(params object[] args)
        {
            bool    iter    = false;
            RString repl    = null;
            bool    tainted = false;

/*
 *          if (args != null && args.Length == 1 && ruby.IsBlockGiven)
 *          {
 *              iter = true;
 *          }
 *          else
 */
            if (args != null && args.Length == 2)
            {
                repl    = StringToRString(ruby, args[1]);
                tainted = repl.IsTainted;
            }
            else
            {
                throw new eArgError(String.Format("wrong # of arguments({0} for 2)",
                                                  (args == null) ? 0 : args.Length));
            }
            RRegexp pat = GetPat(args[0]);

            if (pat.Search(ptr, 0, false, IsTainted) >= 0)
            {
                RThread    th    = ruby.GetCurrentContext();
                RMatchData match = (RMatchData)th.BackRef;
                int        beg   = match.Begin(0);
                int        len   = match.End(0) - beg;
                if (iter)
                {
/*
 *                  RRegexpClass.matchBusy(match);
 *                  repl = RString.AsRString(ruby, ruby.Yield(match[0]));
 *                  th.BackRef = match;        // rescue from yield.
 */
                }
                else
                {
                    repl = new RString(ruby, match.Sub(repl.ToString(), ptr));
                }
                if (repl.IsTainted)
                {
                    tainted = true;
                }
                StringBuilder sb = new StringBuilder(ptr);
                sb.Remove(beg, len);
                sb.Insert(beg, repl.ptr, 1);
                ptr = sb.ToString();
                if (tainted)
                {
                    Taint();
                }
                return(this);
            }
            return(null);
        }
Example #2
0
        public RString GsubAt(params object[] args)
        {
            bool    iter    = false;
            RString repl    = null;
            bool    tainted = false;

/*
 *          if (args != null && args.Length == 1 && ruby.IsBlockGiven)
 *          {
 *              iter = true;
 *          }
 *          else
 */
            if (args != null && args.Length == 2)
            {
                repl    = StringToRString(ruby, args[1]);
                tainted = repl.IsTainted;
            }
            else
            {
                throw new eArgError(String.Format("wrong # of arguments({0} for 2)",
                                                  (args == null) ? 0 : args.Length));
            }
            RRegexp pat = GetPat(args[0]);
            int     beg = pat.Search(ptr, 0, false, IsTainted);

            if (beg < 0)
            {
                return(null);
            }

            StringBuilder sb     = new StringBuilder();
            RThread       th     = ruby.GetCurrentContext();
            int           offset = 0;
            RMatchData    match  = null;

            while (beg >= 0)
            {
                string val;
                match = (RMatchData)th.BackRef;
                beg   = match.Begin(0);
                int len = match.End(0) - beg;

                /*
                 * if (iter)
                 * {
                 *
                 *  RRegexpClass.matchBusy(match);
                 *  repl = RString.AsRString(ruby, ruby.Yield(match[0]));
                 *  th.BackRef = match;        // rescue from yield.
                 *  if (repl.IsTainted) tainted = true;
                 *  val = repl.ToString();
                 *
                 * }
                 * else
                 */
                {
                    val = match.Sub(repl.ToString(), ptr);
                }
                if (beg > offset)
                {
                    sb.Append(ptr, offset, beg - offset);
                }
                sb.Append(val);
                if (len == 0)
                {
                    if (ptr.Length > match.End(0))
                    {
                        sb.Append(ptr, match.End(0), 1);
                    }
                    offset = beg + 1;
                }
                else
                {
                    offset = beg + len;
                }
                if (offset > ptr.Length)
                {
                    break;
                }
                beg = pat.Search(ptr, offset, false, IsTainted);
            }
            if (ptr.Length > offset)
            {
                sb.Append(ptr, offset, ptr.Length - offset);
            }
            th.BackRef = match;        // rescue from yield.
            ptr        = sb.ToString();
            if (tainted)
            {
                Taint();
            }
            return(this);
        }