static bool eq_phpstring(PerlRegExpConverter self, object otherpattern, string otherreplacement) { return otherpattern != null && otherpattern.GetType() == typeof(PhpString) && ((PhpString)otherpattern).Equals((PhpString)self._pattern) && otherreplacement == self._replacement; }
static bool eq_default(PerlRegExpConverter self, object otherpattern, string otherreplacement) { return otherpattern != null && otherpattern.GetType() == self._pattern.GetType() && otherpattern.Equals(self._pattern) && otherreplacement == self._replacement; }
static bool eq_string(PerlRegExpConverter self, object otherpattern, string otherreplacement) { return otherpattern != null && otherreplacement != null && otherpattern.GetType() == typeof(string) && self._strpattern.Equals((string)otherpattern) && self._replacement.Equals(otherreplacement); }
static bool eq_null(PerlRegExpConverter self, object otherpattern, string otherreplacement) { return otherpattern == null && otherreplacement == self._replacement; }
private static PerlRegExpConverter/*!*/EnsureGet(object pattern, string replacement, uint hash) { PerlRegExpConverter item; lock (locks[hash % locks.Length]) { // double checked lock if ((item = Get(pattern, replacement, false)) == null) { // avoid growing of the table in non-web applications (console etc.) CleanupBuckets(); // new item item = new PerlRegExpConverter(pattern, replacement, Configuration.Application.Globalization.PageEncoding) { nextcache = PerlRegExpCache.buckets[hash], generation = PerlRegExpCache.generation }; buckets[hash] = item; // enlist the item } } return item; }
private static string ConvertData(object data, PerlRegExpConverter/*!*/ converter) { if (data == null) { return string.Empty; } else if (data.GetType() == typeof(PhpBytes)) { return converter.ConvertBytes(((PhpBytes)data).ReadonlyData); } else { string str = Core.Convert.ObjectToString(data); return converter.ConvertString(str, 0, str.Length); } }
/// <summary> /// Replaces <paramref name="limit"/> occurences of substrings. /// </summary> /// <param name="converter"> /// Converter used for replacement if <paramref name="callback"/> is <B>null</B>. /// </param> /// <param name="self">Instance of object that called the replace method (replace pattern may contain $this)</param> /// <param name="definedVariables">Array with local variables - can be used by replace pattern</param> /// <param name="callback">Callback to call for replacement strings.</param> /// <param name="str">String to search for matches.</param> /// <param name="limit">Max number of replacements performed.</param> /// <param name="sourceCodeDesc"><see cref="SourceCodeDescriptor"/> for possible lambda function creation.</param> /// <param name="count">Cumulated number of replacements.</param> /// <returns></returns> private static string ReplaceInternal(DObject self, Dictionary<string, object> definedVariables, PerlRegExpConverter converter, PhpCallback callback, string str, int limit, SourceCodeDescriptor sourceCodeDesc, ref int count) { Debug.Assert(limit >= -1); if (callback == null) { // replace without executing code or counting the number of replacements: if ((converter.PerlOptions & PerlRegexOptions.Evaluate) == 0 && count < 0) return converter.Regex.Replace(str, converter.DotNetReplaceExpression, limit); Evaluator evaluator = new Evaluator(converter.Regex, converter.DotNetReplaceExpression, sourceCodeDesc, self, definedVariables); MatchEvaluator match_evaluator; if ((converter.PerlOptions & PerlRegexOptions.Evaluate) != 0) match_evaluator = new MatchEvaluator(evaluator.ReplaceCodeExecute); else match_evaluator = new MatchEvaluator(evaluator.ReplaceCount); string result = converter.Regex.Replace(str, match_evaluator, limit); count += evaluator.Count; return result; } else { StringBuilder result = new StringBuilder((str != null) ? str.Length : 0); int last_index = 0; Match m = converter.Regex.Match(str); while (m.Success && (limit == -1 || limit-- > 0)) { // append everything from input string to current match result.Append(str, last_index, m.Index - last_index); // move index after current match last_index = m.Index + m.Length; PhpArray arr = new PhpArray(m.Groups.Count, 0); for (int i = 0; i < m.Groups.Count; i++) arr[i] = m.Groups[i].Value; // append user callback function result string replacement = Core.Convert.ObjectToString(callback.Invoke(arr)); result.Append(replacement); m = m.NextMatch(); count++; } // remaining string result.Append(str, last_index, str.Length - last_index); return result.ToString(); } }