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  */
  20 package jdk.nashorn.internal.joni;
  21 
  22 import jdk.nashorn.internal.joni.ast.CClassNode;
  23 import jdk.nashorn.internal.joni.ast.ConsAltNode;
  24 import jdk.nashorn.internal.joni.ast.StringNode;
  25 
  26 final class ApplyCaseFold {
  27 
  28     // i_apply_case_fold
  29     public void apply(int from, int[]to, int length, Object o) {
  30         ApplyCaseFoldArg arg = (ApplyCaseFoldArg)o;
  31 
  32         ScanEnvironment env = arg.env;
  33         CClassNode cc = arg.cc;
  34         BitSet bs = cc.bs;
  35 
  36         if (length == 1) {
  37             boolean inCC = cc.isCodeInCC(from);
  38 
  39             if (Config.CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS) {
  40                 if ((inCC && !cc.isNot()) || (!inCC && cc.isNot())) {
  41                     if (to[0] >= BitSet.SINGLE_BYTE_SIZE) {
  42                         cc.addCodeRange(env, to[0], to[0]);
  43                     } else {
  44                         /* /(?i:[^A-C])/.match("a") ==> fail. */
  45                         bs.set(to[0]);
  46                     }
  47                 }
  48             } else {
  49                 if (inCC) {
  50                     if (to[0] >= BitSet.SINGLE_BYTE_SIZE) {
  51                         if (cc.isNot()) cc.clearNotFlag();
  52                         cc.addCodeRange(env, to[0], to[0]);
  53                     } else {
  54                         if (cc.isNot()) {
  55                             bs.clear(to[0]);
  56                         } else {
  57                             bs.set(to[0]);
  58                         }
  59                     }
  60                 }
  61             } // CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
  62 
  63         } else {
  64             if (cc.isCodeInCC(from) && (!Config.CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS || !cc.isNot())) {
  65                 StringNode node = null;
  66                 for (int i=0; i<length; i++) {
  67                     if (i == 0) {
  68                         node = new StringNode();
  69                         /* char-class expanded multi-char only
  70                         compare with string folded at match time. */
  71                         node.setAmbig();
  72                     }
  73                     node.catCode(to[i]);
  74                 }
  75 
  76                 ConsAltNode alt = ConsAltNode.newAltNode(node, null);
  77 
  78                 if (arg.tail == null) {
  79                     arg.altRoot = alt;
  80                 } else {
  81                     arg.tail.setCdr(alt);
  82                 }
  83                 arg.tail = alt;
  84             }
  85 
  86         }
  87 
  88     }
  89 
  90     static final ApplyCaseFold INSTANCE = new ApplyCaseFold();
  91 }