Pchp.Library.PerlRegex.Match.Tidy C# (CSharp) Method

Tidy() private method

private Tidy ( int textpos ) : void
textpos int
return void
        internal virtual void Tidy(int textpos)
        {
            int[] interval;

            interval = _matches[0];
            _index = interval[0];
            _length = interval[1];
            _textpos = textpos;
            _capcount = _matchcount[0];

            if (_balancing)
            {
                // The idea here is that we want to compact all of our unbalanced captures.  To do that we
                // use j basically as a count of how many unbalanced captures we have at any given time
                // (really j is an index, but j/2 is the count).  First we skip past all of the real captures
                // until we find a balance captures.  Then we check each subsequent entry.  If it's a balance
                // capture (it's negative), we decrement j.  If it's a real capture, we increment j and copy
                // it down to the last free position.
                for (int cap = 0; cap < _matchcount.Length; cap++)
                {
                    int limit;
                    int[] matcharray;

                    limit = _matchcount[cap] * 2;
                    matcharray = _matches[cap];

                    int i = 0;
                    int j;

                    for (i = 0; i < limit; i++)
                    {
                        if (matcharray[i] < 0)
                            break;
                    }

                    for (j = i; i < limit; i++)
                    {
                        if (matcharray[i] < 0)
                        {
                            // skip negative values
                            j--;
                        }
                        else
                        {
                            // but if we find something positive (an actual capture), copy it back to the last
                            // unbalanced position.
                            if (i != j)
                                matcharray[j] = matcharray[i];
                            j++;
                        }
                    }

                    _matchcount[cap] = j / 2;
                }

                _balancing = false;
            }
        }

Usage Example

Example #1
0
        /// <summary>
        /// Put match in its canonical form before returning it.
        /// </summary>
        private Match TidyMatch(bool quick)
        {
            if (!quick)
            {
                Match match = runmatch;

                runmatch = null;

                match.Tidy(runtextpos);
                return(match);
            }
            else
            {
                // in quick mode, a successful match returns null, and
                // the allocated match object is left in the cache

                return(null);
            }
        }