public void CompilePattern(SearchOptions options)
 {
     RegexOptions regexOptions = RegexOptions.Compiled;
     if (options.IgnoreCase) {
         regexOptions |= RegexOptions.IgnoreCase;
     }
     regex = new Regex(options.SearchPattern, regexOptions);
 }
 public void CompilePattern(SearchOptions options)
 {
     if (searchPattern != (options.IgnoreCase ? options.SearchPattern.ToUpper() : options.SearchPattern)) {
         searchPattern = options.IgnoreCase ? options.SearchPattern.ToUpper() : options.SearchPattern;
         overlap = new int[searchPattern.Length + 1];
         Preprocessing();
     }
 }
        public ISearchResult FindNext(ITextIterator textIterator, SearchOptions options, bool reverseSearch)
        {
            if (reverseSearch)
                throw new NotSupportedException ();

            int charCount = InternalFindNext(textIterator, options);
            return charCount != -1 ? new DefaultSearchResult (textIterator, charCount) : null;
        }
        public ISearchResult FindNext(ITextIterator textIterator, SearchOptions options, bool reverseSearch)
        {
            if (reverseSearch)
                throw new NotSupportedException ();

            int pos = textIterator.Position;

            int offset = InternalFindNext(textIterator, options);
            if (offset == -1) return null;

            if (textIterator.GetCharRelative (searchPattern.Length) == char.MinValue) {
                if (pos != offset)
                    return FindNext(textIterator, options, false);
                else
                    return null;
            }

            return new DefaultSearchResult (textIterator, searchPattern.Length);
        }
        public ISearchResult FindNext(ITextIterator textIterator, SearchOptions options, bool reverseSearch)
        {
            if (reverseSearch)
                throw new NotSupportedException ();

            if (!textIterator.MoveAhead(1)) return null;
            if (regex == null) return null;

            int pos = textIterator.Position;
            string document = textIterator.ReadToEnd ();
            textIterator.Position = pos;

            Match m = regex.Match (document, 0);
            if (m == null || m.Index <= 0 || m.Length <= 0) {
                return null;
            } else {
                if (textIterator.MoveAhead (m.Index)) {
                    return new DefaultSearchResult (textIterator, m.Length);
                } else {
                    return null;
                }
            }
        }
 int InternalFindNext(ITextIterator textIterator, SearchOptions options)
 {
     int j = 0;
     if (!textIterator.MoveAhead(1)) {
         return -1;
     }
     while (true) { // until pattern found or Iterator finished
         while (j >= 0 && searchPattern[j] != (options.IgnoreCase ? Char.ToUpper(textIterator.GetCharRelative(j)) : textIterator.GetCharRelative(j))) {
             if (!textIterator.MoveAhead(j - overlap[j])) {
                 return -1;
             }
             j = overlap[j];
         }
         if (++j >= searchPattern.Length) {
             if ((!options.SearchWholeWordOnly || SearchReplaceUtilities.IsWholeWordAt(textIterator, searchPattern.Length))) {
                 return textIterator.Position;
             }
             if (!textIterator.MoveAhead(j - overlap[j])) {
                 return -1;
             }
             j = overlap[j];
         }
     }
 }
		public bool SupportsReverseSearch (ITextIterator textIterator, SearchOptions options)
		{
			return textIterator.SupportsSearch (options, true);
		}
 public bool SupportsReverseSearch(ITextIterator textIterator, SearchOptions options)
 {
     return false;
 }
        public ISearchResult Find(SearchOptions options, bool reverse)
        {
            // insanity check
            Debug.Assert(searchStrategy      != null);
            Debug.Assert(documentIterator    != null);
            Debug.Assert(options             != null);

            while (!cancelled)
            {
                if (info != null && textIterator != null && documentIterator.CurrentFileName != null) {
                    if (info.FileName != documentIterator.CurrentFileName || lastWasReverse != reverse) {
                        // create new iterator, if document changed or search direction has changed.
                        info = documentIterator.Current;
                        textIterator = info.GetTextIterator ();
                        reverseSearchMap = null;
                        lastResultPos = -1;
                        if (reverse)
                            textIterator.MoveToEnd ();
                    }

                    ISearchResult result;
                    if (!reverse)
                        result = searchStrategy.FindNext (textIterator, options, false);
                    else {
                        if (searchStrategy.SupportsReverseSearch (textIterator, options)) {
                            result = searchStrategy.FindNext (textIterator, options, true);
                        }
                        else {
                            if (reverseSearchMap == null) {
                                reverseSearchMap = new SearchMap ();
                                reverseSearchMap.Build (searchStrategy, textIterator, options);
                            }
                            if (lastResultPos == -1)
                                lastResultPos = textIterator.Position;
                            result = reverseSearchMap.GetPreviousMatch (lastResultPos);
                            if (result != null)
                                textIterator.Position = result.Position;
                        }
                    }

                    if (result != null) {
                        matches++;
                        lastResultPos = result.Position;
                        lastWasReverse = reverse;
                        return result;
                    }
                }

                if (textIterator != null) textIterator.Close ();

                // not found or first start -> move forward to the next document
                bool more = !reverse ? documentIterator.MoveForward () : documentIterator.MoveBackward ();
                if (more) {
                    searchedFiles++;
                    info = documentIterator.Current;
                    textIterator = info.GetTextIterator ();
                    reverseSearchMap = null;
                    lastResultPos = -1;
                    if (reverse)
                        textIterator.MoveToEnd ();
                }
                else
                    cancelled = true;

                lastWasReverse = reverse;
            }

            cancelled = false;
            return null;
        }
		int InternalFindNext(ITextIterator textIterator, SearchOptions options)
		{
			int[] compareIndex = new int [searchPattern.Length];
			int[] startPositions = new int [searchPattern.Length];
			int maxPoss = 0;
			bool ignoreCase = options.IgnoreCase;
			bool searchWord = options.SearchWholeWordOnly;
			CultureInfo cinfo = CultureInfo.InvariantCulture;
			int patternLength = searchPattern.Length;
			bool wasWordStart = true;
			
			char first = searchPattern[0];

			while (textIterator.MoveAhead(1))
			{
				char c = textIterator.Current;
				if (ignoreCase) c = Char.ToUpper (c, cinfo);
				
				int freePos = -1;
				for (int n=0; n<maxPoss; n++) 
				{
					int pos = compareIndex[n];
					if (pos != 0) {
						if (searchPattern[pos] == c) {
							pos++;
							if (pos == patternLength) {
								if (searchWord) {
									int curp = textIterator.Position;
									bool endw = !textIterator.MoveAhead (1);
									endw = endw || SearchReplaceUtilities.IsWordSeparator (textIterator.Current);
									textIterator.Position = curp;
									if (endw) return startPositions[n];
								}
								else
									return startPositions[n];
							}
							else {
								compareIndex[n] = pos;
								continue;
							}
						}
						compareIndex[n] = 0;
						if (n == maxPoss-1)
							maxPoss = n;
					}
					
					if (freePos == -1)
						freePos = pos;
				}
				
				if (c == first && (!searchWord || wasWordStart)) {
					if (patternLength == 1)
						return textIterator.Position;
						
					if (freePos == -1) {			
						freePos = maxPoss;
						maxPoss++;
					}

					compareIndex [freePos] = 1;
					startPositions [freePos] = textIterator.Position;
				}
				wasWordStart = SearchReplaceUtilities.IsWordSeparator (c);
			}
			
			return -1;
		}
 public ISearchResult FindPrevious(SearchOptions options)
 {
     return Find (options, true);
 }
        public void Build(ISearchStrategy strategy, ITextIterator it, SearchOptions options)
        {
            int startPos = it.Position;
            it.Reset ();

            ISearchResult res = strategy.FindNext (it, options, false);
            while (res != null) {
                matches.Add (res);
                res = strategy.FindNext (it, options, false);
            }
            it.Position = startPos;
        }
 public void CompilePattern(SearchOptions options)
 {
     CompilePattern(options.SearchPattern, options.IgnoreCase);
 }
 public bool SearchNext(string text, SearchOptions options, bool reverse)
 {
     throw new NotSupportedException ();
 }
        public override bool SearchNext(string text, SearchOptions options, bool reverse)
        {
            // Make sure the backward search finds the first match when that match is just
            // at the left of the cursor. Position needs to be incremented in this case because it will be
            // at the last char of the match, and BackwardSearch don't return results that include
            // the initial search position.
            if (reverse && Position < BufferLength && initialBackwardsPosition) {
                Position++;
                initialBackwardsPosition = false;
            }

            TextIter it = Buffer.GetIterAtOffset (DocumentOffset);

            int limitOffset = EndOffset;
            if (reverse)
                limitOffset = (limitOffset + text.Length - 1) % BufferLength;
            else
                limitOffset = (limitOffset + 1) % BufferLength;

            // Use special search flags that work for both the old and new API
            // of gtksourceview (the enum values where changed in the API).
            // See bug #75770
            SourceSearchFlags flags = options.IgnoreCase ? (SourceSearchFlags)7 : (SourceSearchFlags)1;

            Gtk.TextIter matchStart, matchEnd;
            bool res;

            Gtk.TextIter limit;

            if (reverse) {
                if (DocumentOffset <= EndOffset)
                    limit = Buffer.StartIter;
                else
                    limit = Buffer.GetIterAtOffset (limitOffset);
            } else {
                if (DocumentOffset >= EndOffset)
                    limit = Buffer.EndIter;
                else
                    limit = Buffer.GetIterAtOffset (limitOffset);
            }

            // machEnd is the position of the last matched char + 1
            // When searching forward, the limit check is: matchEnd < limit
            // When searching backwards, the limit check is: matchEnd > limit

            res = Find (reverse, it, text, flags, out matchStart, out matchEnd, limit);

            if (!res) {
                // Not found in the first half of the document, try not the other half
                if (reverse && DocumentOffset <= EndOffset) {
                    it = Buffer.EndIter;
                    limit = Buffer.GetIterAtOffset (limitOffset);
                    res = Find (true, it, text, flags, out matchStart, out matchEnd, limit);
                } else if (!reverse && DocumentOffset >= EndOffset) {
                    it = Buffer.StartIter;
                    limit = Buffer.GetIterAtOffset (limitOffset);
                    res = Find (false, it, text, flags, out matchStart, out matchEnd, limit);
                }
            }

            if (!res) return false;

            DocumentOffset = matchStart.Offset;
            return true;
        }
		public void CompilePattern(SearchOptions options)
		{
			searchPattern = options.IgnoreCase ? options.SearchPattern.ToUpper() : options.SearchPattern;
		}
		public ISearchResult FindNext(ITextIterator textIterator, SearchOptions options, bool reverseSearch)
		{
			if (textIterator.SupportsSearch (options, reverseSearch)) {
				if (textIterator.SearchNext (searchPattern, options, reverseSearch)) {
					DefaultSearchResult sr = new DefaultSearchResult (textIterator, searchPattern.Length);
					if (!reverseSearch)
						textIterator.MoveAhead (searchPattern.Length);
					return sr;
				} else
					return null;
			}
			
			if (reverseSearch)
				throw new NotSupportedException ();
				
			int offset = InternalFindNext(textIterator, options);
			if (offset >= 0) {
				int pos = textIterator.Position;
				textIterator.Position = offset;
				DefaultSearchResult sr = new DefaultSearchResult (textIterator, searchPattern.Length);
				textIterator.Position = pos;
				return sr;
			} else
				return null;
		}
 public override bool SupportsSearch(SearchOptions options, bool reverse)
 {
     return !options.SearchWholeWordOnly;
 }
 public ISearchResult FindNext(SearchOptions options)
 {
     return Find (options, false);
 }
 public bool SupportsSearch(SearchOptions options, bool reverse)
 {
     return false;
 }
 int InternalFindNext(ITextIterator textIterator, SearchOptions options)
 {
     while (textIterator.MoveAhead(1))
     {
         int pos = textIterator.Position;
         int charCount = Match (textIterator, options.IgnoreCase, 0);
         textIterator.Position = pos;
         if (charCount != -1) {
             if (!options.SearchWholeWordOnly || SearchReplaceUtilities.IsWholeWordAt (textIterator, charCount))
                 return charCount;
         }
     }
     return -1;
 }