示例#1
0
        /// <summary>
        /// Given a Match, emits into the builder the evaluated
        /// Right-to-Left substitution pattern.
        /// </summary>
        public void ReplacementImplRTL(ref SegmentStringBuilder segments, Match match)
        {
            for (int i = _rules.Length - 1; i >= 0; i--)
            {
                int rule = _rules[i];

                ReadOnlyMemory <char> segment =
                    rule >= 0 ? _strings[rule].AsMemory() :                          // string lookup
                    rule < -Specials?match.GroupToStringImpl(-Specials - 1 - rule) : // group lookup
                        (-Specials - 1 - rule) switch                                // special insertion patterns
                {
                    LeftPortion => match.GetLeftSubstring(),
                    RightPortion => match.GetRightSubstring(),
                    LastGroup => match.LastGroupToStringImpl(),
                    WholeString => match.Text.AsMemory(),
                    _ => default
                };

                // Add the segment to the list if it's not empty.  A common case for it being
                // empty is if the developer is using Regex.Replace as a way to implement
                // Regex.Remove, where the replacement string is empty.
                if (segment.Length != 0)
                {
                    segments.Add(segment);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Given a Match, emits into the builder the evaluated
        /// Right-to-Left substitution pattern.
        /// </summary>
        public void ReplacementImplRTL(ref SegmentStringBuilder segments, Match match)
        {
            for (int i = _rules.Length - 1; i >= 0; i--)
            {
                int r = _rules[i];
                if (r >= 0)
                {
                    // string lookup
                    segments.Add(_strings[r].AsMemory());
                }
                else if (r < -Specials)
                {
                    // group lookup
                    segments.Add(match.GroupToStringImpl(-Specials - 1 - r));
                }
                else
                {
                    // special insertion patterns
                    switch (-Specials - 1 - r)
                    {
                    case LeftPortion:
                        segments.Add(match.GetLeftSubstring());
                        break;

                    case RightPortion:
                        segments.Add(match.GetRightSubstring());
                        break;

                    case LastGroup:
                        segments.Add(match.LastGroupToStringImpl());
                        break;

                    case WholeString:
                        segments.Add(match.Text.AsMemory());
                        break;
                    }
                }
            }
        }