1
2
3
4
5
6
7
8
9
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
19
20
21
22
23 public class RawSubStringPattern {
24 private final String needleString;
25
26 private final byte[] needle;
27
28
29
30
31
32
33
34
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
49
50
51
52
53
54
55
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
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
94
95
96
97 public String pattern() {
98 return needleString;
99 }
100
101
102 @Override
103 public String toString() {
104 return pattern();
105 }
106 }