KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > util > text > TextMatch


1 package com.quadcap.util.text;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.InputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 import com.quadcap.util.Debug;
45
46 /**
47  *
48  * @author Stan Bailes
49  */

50 public class TextMatch {
51     public static int match(byte[] string, int slen,
52                 byte[] pattern, int plen) {
53     int[] qmap = new int[256];
54     for (int i = 0; i < 256; i++) {
55         qmap[i] = plen + 1;
56     }
57     for (int i = 0; i < plen; i++) {
58         qmap[pattern[i]] = plen - i;
59     }
60     for (int i = 0; i <= slen - plen; ) {
61         //Debug.println("i = " + i);
62
boolean match = true;
63         for (int j = 0; j < plen; j++) {
64         byte c = string[i + j];
65         //Debug.println(" c = " + ((char)c));
66
if (string[i + j] != pattern[j]) {
67             match = false;
68             break;
69         }
70         }
71         if (match) return i;
72
73         byte c = string[i + plen];
74         //Debug.println("qmap[" + ((char)c) + "] = " + qmap[c]);
75
i += qmap[string[i + plen]];
76     }
77     return -1;
78     }
79
80     public static int match(byte[] string, byte[] pattern) {
81     return match(string, string.length, pattern, pattern.length);
82     }
83
84     public static int match(InputStream JavaDoc is, byte[] pattern)
85     throws IOException JavaDoc
86     {
87     int[] qmap = new int[256];
88     for (int i = 0; i < 256; i++) {
89         qmap[i] = pattern.length + 1;
90     }
91     for (int i = 0; i < pattern.length; i++) {
92         qmap[pattern[i]] = pattern.length - i;
93     }
94     int cnt = 0;
95     while (true) {
96         //Debug.println("cnt = " + cnt);
97
is.mark(pattern.length);
98         boolean match = true;
99         int j, c;
100         for (j = 0; j < pattern.length; j++) {
101         if ((c = is.read()) < 0) return -1;
102         //Debug.println(" c = " + ((char)c));
103
if (c != pattern[j]) {
104             match = false;
105             break;
106         }
107         }
108         if (match) return cnt;
109         is.skip(pattern.length - j - 1);
110         if ((c = is.read()) < 0) return -1;
111         is.reset();
112         //Debug.println("qmap[" + ((char)c) + "] = " + qmap[c]);
113
int skip = qmap[c];
114         if (is.skip(skip) < skip) return -1;
115         cnt += skip;
116     }
117     }
118 }
119
Popular Tags