View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.util;
13  
14  import org.eclipse.jgit.internal.JGitText;
15  import org.eclipse.jgit.lib.Constants;
16  
17  /**
18   * Searches text using only substring search.
19   * <p>
20   * Instances are thread-safe. Multiple concurrent threads may perform matches on
21   * different character sequences at the same time.
22   */
23  public class RawSubStringPattern {
24  	private final String needleString;
25  
26  	private final byte[] needle;
27  
28  	/**
29  	 * Construct a new substring pattern.
30  	 *
31  	 * @param patternText
32  	 *            text to locate. This should be a literal string, as no
33  	 *            meta-characters are supported by this implementation. The
34  	 *            string may not be the empty string.
35  	 */
36  	public RawSubStringPattern(String patternText) {
37  		if (patternText.length() == 0)
38  			throw new IllegalArgumentException(JGitText.get().cannotMatchOnEmptyString);
39  		needleString = patternText;
40  
41  		final byte[] b = Constants.encode(patternText);
42  		needle = new byte[b.length];
43  		for (int i = 0; i < b.length; i++)
44  			needle[i] = lc(b[i]);
45  	}
46  
47  	/**
48  	 * Match a character sequence against this pattern.
49  	 *
50  	 * @param rcs
51  	 *            the sequence to match. Must not be null but the length of the
52  	 *            sequence is permitted to be 0.
53  	 * @return offset within <code>rcs</code> of the first occurrence of this
54  	 *         pattern; -1 if this pattern does not appear at any position of
55  	 *         <code>rcs</code>.
56  	 */
57  	public int match(RawCharSequence rcs) {
58  		final int needleLen = needle.length;
59  		final byte first = needle[0];
60  
61  		final byte[] text = rcs.buffer;
62  		int matchPos = rcs.startPtr;
63  		final int maxPos = rcs.endPtr - needleLen;
64  
65  		OUTER: for (; matchPos <= maxPos; matchPos++) {
66  			if (neq(first, text[matchPos])) {
67  				while (++matchPos <= maxPos && neq(first, text[matchPos])) {
68  					/* skip */
69  				}
70  				if (matchPos > maxPos)
71  					return -1;
72  			}
73  
74  			int si = matchPos + 1;
75  			for (int j = 1; j < needleLen; j++, si++) {
76  				if (neq(needle[j], text[si]))
77  					continue OUTER;
78  			}
79  			return matchPos;
80  		}
81  		return -1;
82  	}
83  
84  	private static final boolean neq(byte a, byte b) {
85  		return a != b && a != lc(b);
86  	}
87  
88  	private static final byte lc(byte q) {
89  		return (byte) StringUtils.toLowerCase((char) (q & 0xff));
90  	}
91  
92  	/**
93  	 * Get the literal pattern string this instance searches for.
94  	 *
95  	 * @return the pattern string given to our constructor.
96  	 */
97  	public String pattern() {
98  		return needleString;
99  	}
100 
101 	/** {@inheritDoc} */
102 	@Override
103 	public String toString() {
104 		return pattern();
105 	}
106 }