Exemplo n.º 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));
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        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 = null;
                if (i != argv.Length)
                {
                    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
                {
                    StringBuilder sb = new 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);
                if (varName == null)
                {
                    interp.SetResult(result);
                }
                else
                {
                    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);
        }