⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 search.cs

📁 c#精彩编程百例(源代码)
💻 CS
字号:
//  Search.cs 
//  Copyright (c) 2000 Mike Krueger
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


using System.Drawing;

namespace SharpDevelop.Internal.Text.Search {
	
	public class Search
	{
		private int[] next;
		private string s;
		private string bigS;
		private int l = 0;
		private static bool bigonly = true;
		private static bool wholewords = false;
		
		public Search(string find)
		{
			s = find;
			l = s.Length;
			if(bigonly){
				bigS = s.ToUpper();
			}
			else{
				bigS = s;
			}
			Initnext(bigS);
		}
		
		private void Initnext(string s)
		{
			next = new int[l + 1];
			next[0] = -1;
			int i = 0;
			int j = -1;
			while (i < l){
				while ((j >= 0) && (s[i] != s[j]))
					j = next[j];
				++i;
				++j;
				next[i] = j;
			}
		}
		
		public bool BigOnly{
			get{
				return bigonly;
			}
			set{
				if (bigonly != value){
					bigonly = value;
					if(bigonly){
						bigS = s.ToUpper();
					}
					else{
						bigS = s;
					}
					Initnext(bigS);
				}
			}
		}
		
		public bool WholeWords{
			get{
				return wholewords;
			}
			set{
				wholewords = value;
			}
		}
		
		public string What{
			get{
				return s;
			}
			set{
				s = value;
				l = s.Length;
				if(bigonly){
					bigS = s.ToUpper();
				}
				else{
					bigS = s;
				}
				Initnext(bigS);
			}
		}
		
		private bool IsNothing(TextBuffer b, Point begin, int l){
			if (begin.X > 0 && System.Char.IsLetterOrDigit(b[begin.Y].Text[begin.X - 1]))
						return false;
			if (begin.X + l < b[begin.Y].Text.Length && System.Char.IsLetterOrDigit(b[begin.Y].Text[begin.X + l]))
				return false;
			return true;
		}
		
		public Point Find (TextBuffer b, Point begin, Point end)
		{
			int x = begin.X;
			int y = begin.Y;
			int j = 0;
//			System.Console.WriteLine(bigS + "  " + begin + " " + end);
			if (begin == end)
				return end;
			int last = b[y].Text.Length;
			string a;

			while (y < end.Y) {
//				System.Console.WriteLine("1");
				
				last = b[y].Text.Length;
//				System.Console.WriteLine("2");
				a = b[y].Text;
//				System.Console.WriteLine("3");
				if(bigonly)
					a = a.ToUpper();
				
//				System.Console.WriteLine("4");
				while (j < l && x < last) {
					while (j >= 0 && a[x] != bigS[j])
						j = next[j];
					++x;
					++j;
				}
//				System.Console.WriteLine("5");
			if (j == l){
				if(!WholeWords)
					return new Point(x - l, y);
				if(IsNothing(b, new Point(x - l, y), l))
					return new Point(x - l, y);
				else
					return Find(b, new Point(x - l + 1, y), end);
			}
				x = 0;
				j = 0;
				++y;
			}
			
//			System.Console.WriteLine("6 " + y);
			if (end.X > 0){
				a = b[y].Text;
				if (bigonly)
					a = a.ToUpper();
//				System.Console.WriteLine(end.X + " " + b[y].Text.Length + " a:" + a.Length);
//				System.Console.WriteLine("7 " + x + " " + j);
				while (j < l && x < end.X) {
//					System.Console.WriteLine("j: " + j + " X: " +x + " length(a): " + a.Length + " bisS:" + bigS.Length);
					while (j >= 0 && a[x] != bigS[j]) {
//						System.Console.WriteLine("j: " + j + " length: " + l + " next[j]: " + next[j]);
						j = next[j];
					}
//					System.Console.WriteLine(".done");
					++x;
					++j;
				}
			}
//			System.Console.WriteLine("8");
			if (j == l){
				if(!WholeWords)
					return new Point(x - l, y);
				if(IsNothing(b, new Point(x - l, y), l))
					return new Point(x - l, y);
				else
					return Find(b, new Point(x - l + 1, y), end);
			}
			return end;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -