Esempio n. 1
0
        static PatternAndReplacement[] PreparePatternArray(Context ctx, PhpValue pattern, PhpValue replacement)
        {
            if (pattern.IsPhpArray(out var pattern_array))
            {
                var regexes = new PatternAndReplacement[pattern_array.Count];

                if (replacement.IsPhpArray(out var replacement_array))
                {
                    int  i = 0;
                    var  pattern_enumerator     = pattern_array.GetFastEnumerator();
                    var  replacement_enumerator = replacement_array.GetFastEnumerator();
                    bool replacement_valid      = true;

                    while (pattern_enumerator.MoveNext())
                    {
                        string replacement_string;

                        // replacements are in array, move to next item and take it if possible, in other case take empty string:
                        if (replacement_valid && replacement_enumerator.MoveNext())
                        {
                            replacement_string = replacement_enumerator.CurrentValue.ToStringOrThrow(ctx);
                        }
                        else
                        {
                            replacement_string = string.Empty;
                            replacement_valid  = false; // end of replacement_enumerator, do not call MoveNext again
                        }

                        //
                        if (TryParseRegexp(pattern_enumerator.CurrentValue.ToStringOrThrow(ctx), out var regex))
                        {
                            regexes[i++] = new PatternAndReplacement(regex, replacement_string);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }
                else
                {
                    // array pattern
                    // string replacement

                    var replacement_string = replacement.ToStringOrThrow(ctx);

                    int i = 0;
                    var pattern_enumerator = pattern_array.GetFastEnumerator();
                    while (pattern_enumerator.MoveNext())
                    {
                        if (TryParseRegexp(pattern_enumerator.CurrentValue.ToStringOrThrow(ctx), out var regex))
                        {
                            regexes[i++] = new PatternAndReplacement(regex, replacement_string);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }

                return(regexes);
            }
            else
            {
                if (replacement.IsPhpArray(out _))
                {
                    // string pattern and array replacement not allowed:
                    // Parameter mismatch, pattern is a string while replacement is an array
                    PhpException.InvalidArgument(nameof(replacement), LibResources.replacement_array_pattern_not);
                    return(null);
                }

                // string pattern
                // string replacement
                if (TryParseRegexp(pattern.ToStringOrThrow(ctx), out var regex))
                {
                    return(new PatternAndReplacement[]
                    {
                        new PatternAndReplacement(regex, replacement.ToStringOrThrow(ctx))
                    });
                }
                else
                {
                    return(null);
                }
            }
        }