OptAnchorInfo.java revision 953:221a84ef44c0
1/*
2 * Permission is hereby granted, free of charge, to any person obtaining a copy of
3 * this software and associated documentation files (the "Software"), to deal in
4 * the Software without restriction, including without limitation the rights to
5 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
6 * of the Software, and to permit persons to whom the Software is furnished to do
7 * so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in all
10 * copies or substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 * SOFTWARE.
19 */
20package jdk.nashorn.internal.runtime.regexp.joni;
21
22import jdk.nashorn.internal.runtime.regexp.joni.constants.AnchorType;
23
24final class OptAnchorInfo implements AnchorType {
25    int leftAnchor;
26    int rightAnchor;
27
28    void clear() {
29        leftAnchor = rightAnchor = 0;
30    }
31
32    void copy(final OptAnchorInfo other) {
33        leftAnchor = other.leftAnchor;
34        rightAnchor = other.rightAnchor;
35    }
36
37    void concat(final OptAnchorInfo left, final OptAnchorInfo right, final int leftLength, final int rightLength) {
38        leftAnchor = left.leftAnchor;
39        if (leftLength == 0) leftAnchor |= right.leftAnchor;
40
41        rightAnchor = right.rightAnchor;
42        if (rightLength == 0) rightAnchor |= left.rightAnchor;
43    }
44
45    boolean isSet(final int anchor) {
46        if ((leftAnchor & anchor) != 0) return true;
47        return (rightAnchor & anchor) != 0;
48    }
49
50    void add(final int anchor) {
51        if (isLeftAnchor(anchor)) {
52            leftAnchor |= anchor;
53        } else {
54            rightAnchor |= anchor;
55        }
56    }
57
58    void remove(final int anchor) {
59        if (isLeftAnchor(anchor)) {
60            leftAnchor &= ~anchor;
61        } else {
62            rightAnchor &= ~anchor;
63        }
64    }
65
66    void altMerge(final OptAnchorInfo other) {
67        leftAnchor &= other.leftAnchor;
68        rightAnchor &= other.rightAnchor;
69    }
70
71    static boolean isLeftAnchor(final int anchor) { // make a mask for it ?
72        return !(anchor == END_BUF || anchor == SEMI_END_BUF ||
73                 anchor == END_LINE || anchor == PREC_READ ||
74                 anchor == PREC_READ_NOT);
75    }
76
77    static String anchorToString(final int anchor) {
78        final StringBuffer s = new StringBuffer("[");
79
80        if ((anchor & AnchorType.BEGIN_BUF) !=0 ) s.append("begin-buf ");
81        if ((anchor & AnchorType.BEGIN_LINE) !=0 ) s.append("begin-line ");
82        if ((anchor & AnchorType.BEGIN_POSITION) !=0 ) s.append("begin-pos ");
83        if ((anchor & AnchorType.END_BUF) !=0 ) s.append("end-buf ");
84        if ((anchor & AnchorType.SEMI_END_BUF) !=0 ) s.append("semi-end-buf ");
85        if ((anchor & AnchorType.END_LINE) !=0 ) s.append("end-line ");
86        if ((anchor & AnchorType.ANYCHAR_STAR) !=0 ) s.append("anychar-star ");
87        if ((anchor & AnchorType.ANYCHAR_STAR_ML) !=0 ) s.append("anychar-star-pl ");
88        s.append("]");
89
90        return s.toString();
91    }
92}
93