Beispiel #1
0
        internal static bool regExpMatch(Interp interp, string inString, TclObject pattern)
        {
            Regexp r = TclRegexp.compile(interp, pattern, false);

            return(r.match(inString, (string[])null));
        }
        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            bool all    = false;
            bool nocase = false;

            try
            {
                int i = 1;

                while (argv[i].ToString().StartsWith("-"))
                {
                    int index = TclIndex.get(interp, argv[i], validOpts, "switch", 0);
                    i++;
                    switch (index)
                    {
                    case OPT_ALL:  {
                        all = true;
                        break;
                    }

                    case OPT_NOCASE:  {
                        nocase = true;
                        break;
                    }

                    case OPT_LAST:  {
                        goto opts_brk;
                    }
                    }
                }

                opts_brk :;


                TclObject exp = argv[i++];

                string inString = argv[i++].ToString();

                string subSpec = argv[i++].ToString();

                string varName = argv[i++].ToString();
                if (i != argv.Length)
                {
                    throw new System.IndexOutOfRangeException();
                }

                Regexp r = TclRegexp.compile(interp, exp, nocase);

                int    count = 0;
                string result;

                if (all == false)
                {
                    result = r.sub(inString, subSpec);
                    if ((System.Object)result == null)
                    {
                        result = inString;
                    }
                    else
                    {
                        count++;
                    }
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    Regsub s = new Regsub(r, inString);
                    while (s.nextMatch())
                    {
                        count++;
                        sb.Append(s.skipped());
                        Regexp.applySubspec(s, subSpec, sb);
                    }
                    sb.Append(s.rest());
                    result = sb.ToString();
                }

                TclObject obj = TclString.newInstance(result);
                try
                {
                    interp.setVar(varName, obj, 0);
                }
                catch (TclException e)
                {
                    throw new TclException(interp, "couldn't set variable \"" + varName + "\"");
                }
                interp.setResult(count);
            }
            catch (System.IndexOutOfRangeException e)
            {
                throw new TclNumArgsException(interp, 1, argv, "?switches? exp string subSpec varName");
            }
            return(TCL.CompletionCode.RETURN);
        }
Beispiel #3
0
        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            bool nocase  = false;
            bool indices = false;

            try
            {
                int i = 1;

                while (argv[i].ToString().StartsWith("-"))
                {
                    int index = TclIndex.get(interp, argv[i], validOpts, "switch", 0);
                    i++;
                    switch (index)
                    {
                    case OPT_INDICES:  {
                        indices = true;
                        break;
                    }

                    case OPT_NOCASE:  {
                        nocase = true;
                        break;
                    }

                    case OPT_LAST:  {
                        goto opts_brk;
                    }
                    }
                }

                opts_brk :;


                TclObject exp = TclString.newInstance(argv[i++].ToString().Replace("\\d", "[0-9]"));

                string inString = argv[i++].ToString();

                int matches = argv.Length - i;

                Regexp r = TclRegexp.compile(interp, exp, nocase);

                int[] args    = new int[matches * 2];
                bool  matched = r.match(inString, args);
                if (matched)
                {
                    for (int match = 0; i < argv.Length; i++)
                    {
                        TclObject obj;

                        int start = args[match++];
                        int end   = args[match++];
                        if (indices)
                        {
                            if (end >= 0)
                            {
                                end--;
                            }
                            obj = TclList.newInstance();
                            TclList.append(interp, obj, TclInteger.newInstance(start));
                            TclList.append(interp, obj, TclInteger.newInstance(end));
                        }
                        else
                        {
                            string range = (start >= 0)?inString.Substring(start, (end) - (start)):"";
                            obj = TclString.newInstance(range);
                        }
                        try
                        {
                            interp.setVar(argv[i].ToString(), obj, 0);
                        }
                        catch (TclException e)
                        {
                            throw new TclException(interp, "couldn't set variable \"" + argv[i] + "\"");
                        }
                    }
                }
                interp.setResult(matched);
            }
            catch (System.IndexOutOfRangeException e)
            {
                throw new TclNumArgsException(interp, 1, argv, "?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?");
            }
            return(TCL.CompletionCode.RETURN);
        }