示例#1
0
        protected override void Search(ref NativeMethods.SearchParams searchParams)
        {
            var start = searchParams.TextStart;

            if (searchParams.MatchStart != IntPtr.Zero)
            {
                start = searchParams.MatchStart + searchParams.MatchLength * sizeof(char);
            }
            var end          = searchParams.TextStart + searchParams.TextLength * sizeof(char);
            var count        = Pointers.Offset64(start, end) / sizeof(char);
            var searchHitPtr = NativeMethods.Utf16_Search(
                start,
                count,
                _patternPtr.Pointer,
                _patternLength,
                _searchOptions);

            if (searchHitPtr == IntPtr.Zero)
            {
                searchParams.MatchStart  = IntPtr.Zero;
                searchParams.MatchLength = 0;
            }
            else
            {
                searchParams.MatchStart  = searchHitPtr;
                searchParams.MatchLength = _patternLength;
            }
        }
        private unsafe List <TextRange> FindWorker(
            TextFragment textFragment,
            Func <TextRange, TextRange?> postProcess,
            IOperationProgressTracker progressTracker,
            int maxResultSize)
        {
            List <TextRange> result = null;

            // Note: From C# spec: If E is zero, then no allocation is made, and
            // the pointer returned is implementation-defined.
            byte *searchBuffer = stackalloc byte[this.SearchBufferSize];
            var   searchParams = new NativeMethods.SearchParams {
                TextStart    = textFragment.StartPtr,
                TextLength   = textFragment.Length,
                SearchBuffer = new IntPtr(searchBuffer),
            };

            while (true)
            {
                // Perform next search
                Search(ref searchParams);
                if (searchParams.MatchStart == IntPtr.Zero)
                {
                    break;
                }

                // Convert match from *byte* pointers to a *text* range
                var matchFragment = textFragment.Sub(searchParams.MatchStart, searchParams.MatchLength);
                var matchRange    = new TextRange(matchFragment.Position, matchFragment.Length);

                // Post process match, maybe skipping it
                var postMatchRange = postProcess(matchRange);
                if (postMatchRange == null)
                {
                    continue;
                }
                matchRange = postMatchRange.Value;

                // Add to result collection
                if (result == null)
                {
                    result = new List <TextRange>();
                }
                result.Add(matchRange);

                // Check it is time to end processing early.
                maxResultSize--;
                progressTracker.AddResults(1);
                if (maxResultSize <= 0 || progressTracker.ShouldEndProcessing)
                {
                    CancelSearch(ref searchParams);
                    break;
                }
            }

            return(result ?? NoResult);
        }
    private unsafe List<TextRange> FindWorker(
      TextFragment textFragment,
      Func<TextRange, TextRange?> postProcess, 
      IOperationProgressTracker progressTracker,
      int maxResultSize) {
      List<TextRange> result = null;

      // Note: From C# spec: If E is zero, then no allocation is made, and
      // the pointer returned is implementation-defined.
      byte* searchBuffer = stackalloc byte[this.SearchBufferSize];
      var searchParams = new NativeMethods.SearchParams {
        TextStart = textFragment.StartPtr,
        TextLength = textFragment.Length,
        SearchBuffer = new IntPtr(searchBuffer),
      };

      while (true) {
        // Perform next search
        Search(ref searchParams);
        if (searchParams.MatchStart == IntPtr.Zero)
          break;

        // Convert match from *byte* pointers to a *text* range
        var matchFragment = textFragment.Sub(searchParams.MatchStart, searchParams.MatchLength);
        var matchRange = new TextRange(matchFragment.Position, matchFragment.Length);

        // Post process match, maybe skipping it
        var postMatchRange = postProcess(matchRange);
        if (postMatchRange == null)
          continue;
        matchRange = postMatchRange.Value;

        // Add to result collection
        if (result == null)
          result = new List<TextRange>();
        result.Add(matchRange);

        // Check it is time to end processing early.
        maxResultSize--;
        progressTracker.AddResults(1);
        if (maxResultSize <= 0 || progressTracker.ShouldEndProcessing) {
          CancelSearch(ref searchParams);
          break;
        }
      }

      return result ?? NoResult;
    }
示例#4
0
 protected override void CancelSearch(ref NativeMethods.SearchParams searchParams)
 {
     // Nothing to do.
 }
 /// <summary>
 /// If the search is abandonned before all hits have been found, this method
 /// is called to allow the implementation to cleanup intermediate data
 /// structures used by the implementation.
 /// </summary>
 protected abstract void CancelSearch(ref NativeMethods.SearchParams searchParams);
 protected override void CancelSearch(ref NativeMethods.SearchParams searchParams)
 {
     NativeMethods.AsciiSearchAlgorithm_CancelSearch(_handle, ref searchParams);
 }