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.ast.CClassNode;
23
24final class ApplyCaseFold {
25
26    // i_apply_case_fold
27    public static void apply(final int from, final int to, final Object o) {
28        final ApplyCaseFoldArg arg = (ApplyCaseFoldArg)o;
29
30        final ScanEnvironment env = arg.env;
31        final CClassNode cc = arg.cc;
32        final BitSet bs = cc.bs;
33
34        final boolean inCC = cc.isCodeInCC(from);
35
36        if (Config.CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS) {
37            if ((inCC && !cc.isNot()) || (!inCC && cc.isNot())) {
38                if (to >= BitSet.SINGLE_BYTE_SIZE) {
39                    cc.addCodeRange(env, to, to);
40                } else {
41                    /* /(?i:[^A-C])/.match("a") ==> fail. */
42                    bs.set(to);
43                }
44            }
45        } else {
46            if (inCC) {
47                if (to >= BitSet.SINGLE_BYTE_SIZE) {
48                    if (cc.isNot()) {
49                        cc.clearNotFlag();
50                    }
51                    cc.addCodeRange(env, to, to);
52                } else {
53                    if (cc.isNot()) {
54                        bs.clear(to);
55                    } else {
56                        bs.set(to);
57                    }
58                }
59            }
60        } // CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
61
62    }
63
64    static final ApplyCaseFold INSTANCE = new ApplyCaseFold();
65}
66