Quadcap Embeddable Database

com/quadcap/util/text/TextMatch.java

Go to the documentation of this file.
00001 package com.quadcap.util.text; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.InputStream; 00042 import java.io.IOException; 00043 00044 import com.quadcap.util.Debug; 00045 00046 /** 00047 * 00048 * @author Stan Bailes 00049 */ 00050 public class TextMatch { 00051 public static int match(byte[] string, int slen, 00052 byte[] pattern, int plen) { 00053 int[] qmap = new int[256]; 00054 for (int i = 0; i < 256; i++) { 00055 qmap[i] = plen + 1; 00056 } 00057 for (int i = 0; i < plen; i++) { 00058 qmap[pattern[i]] = plen - i; 00059 } 00060 for (int i = 0; i <= slen - plen; ) { 00061 //Debug.println("i = " + i); 00062 boolean match = true; 00063 for (int j = 0; j < plen; j++) { 00064 byte c = string[i + j]; 00065 //Debug.println(" c = " + ((char)c)); 00066 if (string[i + j] != pattern[j]) { 00067 match = false; 00068 break; 00069 } 00070 } 00071 if (match) return i; 00072 00073 byte c = string[i + plen]; 00074 //Debug.println("qmap[" + ((char)c) + "] = " + qmap[c]); 00075 i += qmap[string[i + plen]]; 00076 } 00077 return -1; 00078 } 00079 00080 public static int match(byte[] string, byte[] pattern) { 00081 return match(string, string.length, pattern, pattern.length); 00082 } 00083 00084 public static int match(InputStream is, byte[] pattern) 00085 throws IOException 00086 { 00087 int[] qmap = new int[256]; 00088 for (int i = 0; i < 256; i++) { 00089 qmap[i] = pattern.length + 1; 00090 } 00091 for (int i = 0; i < pattern.length; i++) { 00092 qmap[pattern[i]] = pattern.length - i; 00093 } 00094 int cnt = 0; 00095 while (true) { 00096 //Debug.println("cnt = " + cnt); 00097 is.mark(pattern.length); 00098 boolean match = true; 00099 int j, c; 00100 for (j = 0; j < pattern.length; j++) { 00101 if ((c = is.read()) < 0) return -1; 00102 //Debug.println(" c = " + ((char)c)); 00103 if (c != pattern[j]) { 00104 match = false; 00105 break; 00106 } 00107 } 00108 if (match) return cnt; 00109 is.skip(pattern.length - j - 1); 00110 if ((c = is.read()) < 0) return -1; 00111 is.reset(); 00112 //Debug.println("qmap[" + ((char)c) + "] = " + qmap[c]); 00113 int skip = qmap[c]; 00114 if (is.skip(skip) < skip) return -1; 00115 cnt += skip; 00116 } 00117 } 00118 }