001: package com.quadcap.util.text;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.InputStream;
042: import java.io.IOException;
043:
044: import com.quadcap.util.Debug;
045:
046: /**
047: *
048: * @author Stan Bailes
049: */
050: public class TextMatch {
051: public static int match(byte[] string, int slen, byte[] pattern,
052: int plen) {
053: int[] qmap = new int[256];
054: for (int i = 0; i < 256; i++) {
055: qmap[i] = plen + 1;
056: }
057: for (int i = 0; i < plen; i++) {
058: qmap[pattern[i]] = plen - i;
059: }
060: for (int i = 0; i <= slen - plen;) {
061: //Debug.println("i = " + i);
062: boolean match = true;
063: for (int j = 0; j < plen; j++) {
064: byte c = string[i + j];
065: //Debug.println(" c = " + ((char)c));
066: if (string[i + j] != pattern[j]) {
067: match = false;
068: break;
069: }
070: }
071: if (match)
072: return i;
073:
074: byte c = string[i + plen];
075: //Debug.println("qmap[" + ((char)c) + "] = " + qmap[c]);
076: i += qmap[string[i + plen]];
077: }
078: return -1;
079: }
080:
081: public static int match(byte[] string, byte[] pattern) {
082: return match(string, string.length, pattern, pattern.length);
083: }
084:
085: public static int match(InputStream is, byte[] pattern)
086: throws IOException {
087: int[] qmap = new int[256];
088: for (int i = 0; i < 256; i++) {
089: qmap[i] = pattern.length + 1;
090: }
091: for (int i = 0; i < pattern.length; i++) {
092: qmap[pattern[i]] = pattern.length - i;
093: }
094: int cnt = 0;
095: while (true) {
096: //Debug.println("cnt = " + cnt);
097: is.mark(pattern.length);
098: boolean match = true;
099: int j, c;
100: for (j = 0; j < pattern.length; j++) {
101: if ((c = is.read()) < 0)
102: return -1;
103: //Debug.println(" c = " + ((char)c));
104: if (c != pattern[j]) {
105: match = false;
106: break;
107: }
108: }
109: if (match)
110: return cnt;
111: is.skip(pattern.length - j - 1);
112: if ((c = is.read()) < 0)
113: return -1;
114: is.reset();
115: //Debug.println("qmap[" + ((char)c) + "] = " + qmap[c]);
116: int skip = qmap[c];
117: if (is.skip(skip) < skip)
118: return -1;
119: cnt += skip;
120: }
121: }
122: }
|