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) {
40            leftAnchor |= right.leftAnchor;
41        }
42
43        rightAnchor = right.rightAnchor;
44        if (rightLength == 0) {
45            rightAnchor |= left.rightAnchor;
46        }
47    }
48
49    boolean isSet(final int anchor) {
50        if ((leftAnchor & anchor) != 0) {
51            return true;
52        }
53        return (rightAnchor & anchor) != 0;
54    }
55
56    void add(final int anchor) {
57        if (isLeftAnchor(anchor)) {
58            leftAnchor |= anchor;
59        } else {
60            rightAnchor |= anchor;
61        }
62    }
63
64    void remove(final int anchor) {
65        if (isLeftAnchor(anchor)) {
66            leftAnchor &= ~anchor;
67        } else {
68            rightAnchor &= ~anchor;
69        }
70    }
71
72    void altMerge(final OptAnchorInfo other) {
73        leftAnchor &= other.leftAnchor;
74        rightAnchor &= other.rightAnchor;
75    }
76
77    static boolean isLeftAnchor(final int anchor) { // make a mask for it ?
78        return !(anchor == END_BUF || anchor == SEMI_END_BUF ||
79                 anchor == END_LINE || anchor == PREC_READ ||
80                 anchor == PREC_READ_NOT);
81    }
82
83    static String anchorToString(final int anchor) {
84        final StringBuffer s = new StringBuffer("[");
85
86        if ((anchor & AnchorType.BEGIN_BUF) !=0 ) {
87            s.append("begin-buf ");
88        }
89        if ((anchor & AnchorType.BEGIN_LINE) !=0 ) {
90            s.append("begin-line ");
91        }
92        if ((anchor & AnchorType.BEGIN_POSITION) !=0 ) {
93            s.append("begin-pos ");
94        }
95        if ((anchor & AnchorType.END_BUF) !=0 ) {
96            s.append("end-buf ");
97        }
98        if ((anchor & AnchorType.SEMI_END_BUF) !=0 ) {
99            s.append("semi-end-buf ");
100        }
101        if ((anchor & AnchorType.END_LINE) !=0 ) {
102            s.append("end-line ");
103        }
104        if ((anchor & AnchorType.ANYCHAR_STAR) !=0 ) {
105            s.append("anychar-star ");
106        }
107        if ((anchor & AnchorType.ANYCHAR_STAR_ML) !=0 ) {
108            s.append("anychar-star-pl ");
109        }
110        s.append("]");
111
112        return s.toString();
113    }
114}
115