Pattern GetDuplicatedCode(MethodDefinition current, MethodDefinition target)
        {
            if (!CanCompareMethods(current, target))
            {
                return(null);
            }

            IList <Pattern> patterns = GetPatterns(current);

            if (patterns.Count == 0)
            {
                return(null);
            }

            InstructionMatcher.Current = current;
            InstructionMatcher.Target  = target;

            foreach (Pattern pattern in patterns)
            {
                if (pattern.IsCompilerGeneratedBlock || !pattern.IsExtractableToMethodBlock)
                {
                    continue;
                }

                if (InstructionMatcher.Match(pattern, target.Body.Instructions))
                {
                    WriteToOutput(current, target, pattern);
                    return(pattern);
                }
            }

            return(null);
        }
Example #2
0
        internal void ComputePrefixes(MethodDefinition method)
        {
            MethodDefinition target = InstructionMatcher.Target;

            InstructionMatcher.Target = method;
            try
            {
                int offset = 0;
                if ((prefixes == null) || (prefixes.Length < instructions.Length))
                {
                    prefixes = new int [instructions.Length];
                }

                for (int index = 1; index < instructions.Length; index++)
                {
                    while (offset > 0 &&
                           !InstructionMatcher.AreEquivalent(instructions [offset], instructions [index]))
                    {
                        offset = prefixes [offset - 1];
                    }

                    if (InstructionMatcher.AreEquivalent(instructions [offset], instructions [index]))
                    {
                        offset++;
                    }

                    prefixes [index] = offset;
                }
            }
            finally
            {
                InstructionMatcher.Target = target;
            }
        }