Пример #1
0
        HexPosition?FindCoreReverse(SearchState state, HexPosition start, HexPosition lowerBounds)
        {
            var patternLocal = pattern;
            var maskLocal    = mask;
            var pos          = start;

            state.SetPreviousPosition(pos);
            var patternLocal0             = patternLocal[patternLocal.Length - 1];
            var maskLocal0                = maskLocal[maskLocal.Length - 1];
            var patternLocal1             = patternLocal.Length <= 1 ? 0 : patternLocal[patternLocal.Length - 2];
            var maskLocal1                = maskLocal.Length <= 1 ? 0 : maskLocal[maskLocal.Length - 2];
            var maskLocalLengthIsAtLeast2 = maskLocal.Length >= 2;

loop:
            // This loop doesn't check the cancellation token because SearchState does that
            // every time it reads new memory from the buffer.
            if (pos < lowerBounds)
            {
                return(null);
            }
            int         skip;
            HexPosition?beforePos;

            if (maskLocalLengthIsAtLeast2)
            {
                skip      = 2;
                beforePos = maskLocal0 == 0xFF ?
                            state.PositionBefore2(patternLocal0, patternLocal1, maskLocal1, lowerBounds) :
                            state.PositionBeforeWithMask2(patternLocal0, patternLocal1, maskLocal0, maskLocal1, lowerBounds);
            }
            else if (maskLocal0 == 0xFF)
            {
                skip      = 1;
                beforePos = state.PositionBefore1(patternLocal0, lowerBounds);
            }
            else
            {
                skip      = 1;
                beforePos = state.PositionBeforeWithMask1(patternLocal0, maskLocal0, lowerBounds);
            }
            if (beforePos == null)
            {
                return(null);
            }
            pos = beforePos.Value;

            for (int i = patternLocal.Length - 1 - skip; i >= 0; i--)
            {
                var b = state.GetPreviousByte();
                var m = maskLocal[i];
                if ((b & m) != patternLocal[i])
                {
                    pos = pos + (skip - 1);
                    state.SetPreviousPosition(pos);
                    goto loop;
                }
            }
            return(pos - (patternLocal.LongLength - skip - 1));
        }