Exemplo n.º 1
0
        static PhpValue PregReplaceInternal(Context ctx, PatternAndReplacement[] regexes, PhpValue subject, long limit, out long count, bool filter)
        {
            count = 0;

            // PHP's behaviour for undocumented limit range
            if (limit < -1)
            {
                limit = 0;
            }

            if (regexes == null)
            {
                // NOTE: PHP returns FALSE when pattern is string and replacement is array which is wrong according to manual
                return(PhpValue.Null);
            }

            // enumerate subjects first, then patterns and replacements
            if (subject.IsPhpArray(out var subject_array))
            {
                // returning PhpArray
                var arr = new PhpArray(subject_array.Count);
                var s   = subject_array.GetFastEnumerator();
                while (s.MoveNext())
                {
                    var oldcount = count;
                    var newvalue = PregReplaceInternal(ctx, regexes, s.CurrentValue.ToPhpString(ctx), limit, ref count);

                    if (filter && oldcount == count)
                    {
                        continue;
                    }

                    arr[s.CurrentKey] = newvalue.DeepCopy();
                }

                return(arr);
            }
            else
            {
                var newvalue = PregReplaceInternal(ctx, regexes, subject.ToPhpString(ctx), limit, ref count);

                if (filter && count == 0)
                {
                    return(PhpValue.Null);
                }

                return(newvalue);
            }
        }