System.Text.RegularExpressions.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
        // TODO https://github.com/dotnet/runtime/issues/62573: Obsolete this.
        /// <summary>
        /// This method's body is only kept since it is a protected member that could be called by someone outside
        /// the assembly.
        /// </summary>
        protected internal Match?Scan(Regex regex, string text, int textbeg, int textend, int textstart, int prevlen, bool quick, TimeSpan timeout)
        {
            InitializeTimeout(timeout);

            RegexRunnerMode mode = quick ? RegexRunnerMode.ExistenceRequired : RegexRunnerMode.FullMatchRequired;

            // We set runtext before calling InitializeForScan so that runmatch object is initialized with the text
            runtext = text;

            InitializeForScan(regex, text, textstart, mode);

            // InitializeForScan will default runtextstart and runtextend to 0 and length of string
            // since it is configured to work over a sliced portion of text so we adjust those values.
            runtextstart = textstart;
            runtextend   = textend;

            // Configure the additional value to "bump" the position along each time we loop around
            // to call FindFirstChar again, as well as the stopping position for the loop.  We generally
            // bump by 1 and stop at textend, but if we're examining right-to-left, we instead bump
            // by -1 and stop at textbeg.
            int bump = 1, stoppos = textend;

            if (regex.RightToLeft)
            {
                bump    = -1;
                stoppos = textbeg;
            }

            // If previous match was empty or failed, advance by one before matching.
            if (prevlen == 0)
            {
                if (textstart == stoppos)
                {
                    return(Match.Empty);
                }

                runtextpos += bump;
            }

            Match match = InternalScan(regex, textbeg, textend);

            runtext = null; //drop reference

            if (match.FoundMatch)
            {
                if (quick)
                {
                    return(null);
                }

                runmatch = null;
                match.Tidy(runtextpos, 0, mode);
            }
            else
            {
                runmatch !.Text = null;
            }

            return(match);
        }
All Usage Examples Of System.Text.RegularExpressions.Match::Tidy