2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.util
;
40 import java
.io
.UnsupportedEncodingException
;
43 * Searches text using only substring search.
45 * Instances are thread-safe. Multiple concurrent threads may perform matches on
46 * different character sequences at the same time.
48 public class RawSubStringPattern
{
49 private final String needleString
;
51 private final byte[] needle
;
54 * Construct a new substring pattern.
57 * text to locate. This should be a literal string, as no
58 * meta-characters are supported by this implementation. The
59 * string may not be the empty string.
61 public RawSubStringPattern(final String patternText
) {
62 if (patternText
.length() == 0)
63 throw new IllegalArgumentException("Cannot match on empty string.");
64 needleString
= patternText
;
68 b
= patternText
.getBytes("UTF-8");
69 } catch (UnsupportedEncodingException e
) {
70 throw new IllegalStateException("JVM lacks UTF-8 support.", e
);
73 needle
= new byte[b
.length
];
74 for (int i
= 0; i
< b
.length
; i
++)
79 * Match a character sequence against this pattern.
82 * the sequence to match. Must not be null but the length of the
83 * sequence is permitted to be 0.
84 * @return offset within <code>rcs</code> of the first occurrence of this
85 * pattern; -1 if this pattern does not appear at any position of
88 public int match(final RawCharSequence rcs
) {
89 final int needleLen
= needle
.length
;
90 final byte first
= needle
[0];
92 final byte[] text
= rcs
.buffer
;
93 int matchPos
= rcs
.startPtr
;
94 final int maxPos
= rcs
.endPtr
- needleLen
;
96 OUTER
: for (; matchPos
< maxPos
; matchPos
++) {
97 if (neq(first
, text
[matchPos
])) {
98 while (++matchPos
< maxPos
&& neq(first
, text
[matchPos
])) {
101 if (matchPos
== maxPos
)
106 for (int j
= 1; j
< needleLen
; j
++, si
++) {
107 if (neq(needle
[j
], text
[si
]))
115 private static final boolean neq(final byte a
, final byte b
) {
116 return a
!= b
&& a
!= lc(b
);
119 private static final byte lc(final byte q
) {
120 return (byte) Character
.toLowerCase((char) (q
& 0xff));
124 * Get the literal pattern string this instance searches for.
126 * @return the pattern string given to our constructor.
128 public String
pattern() {
133 public String
toString() {