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.runtime.regexp.joni;
  21 
  22 final class OptMapInfo {
  23 
  24     final MinMaxLen mmd = new MinMaxLen();          /* info position */
  25     final OptAnchorInfo anchor = new OptAnchorInfo();
  26 
  27     int value;                                      /* weighted value */
  28     final byte map[] = new byte[Config.CHAR_TABLE_SIZE];
  29 
  30     void clear() {
  31         mmd.clear();
  32         anchor.clear();
  33         value = 0;
  34         for (int i=0; i<map.length; i++) map[i] = 0;
  35     }
  36 
  37     void copy(OptMapInfo other) {
  38         mmd.copy(other.mmd);
  39         anchor.copy(other.anchor);
  40         value = other.value;
  41         //for(int i=0; i<map.length; i++) map[i] = other.map[i];
  42         System.arraycopy(other.map, 0, map, 0, other.map.length);
  43     }
  44 
  45     void addChar(int c) {
  46         int c_ = c & 0xff;
  47         if (map[c_] == 0) {
  48             map[c_] = 1;
  49             value += positionValue(c_);
  50         }
  51     }
  52 
  53     void addCharAmb(char[] chars, int p, int end, int caseFoldFlag) {
  54         addChar(chars[p]);
  55 
  56         caseFoldFlag &= ~Config.INTERNAL_ENC_CASE_FOLD_MULTI_CHAR;
  57         char[]items = EncodingHelper.caseFoldCodesByString(caseFoldFlag, chars[p]);
  58 
  59         for (int i=0; i<items.length; i++) {
  60             addChar(items[i]);
  61         }
  62     }
  63 
  64     // select_opt_map_info
  65     private static final int z = 1<<15; /* 32768: something big value */
  66     void select(OptMapInfo alt) {
  67         if (alt.value == 0) return;
  68         if (value == 0) {
  69             copy(alt);
  70             return;
  71         }
  72 
  73         int v1 = z / value;
  74         int v2 = z /alt.value;
  75 
  76         if (mmd.compareDistanceValue(alt.mmd, v1, v2) > 0) copy(alt);
  77     }
  78 
  79     // alt_merge_opt_map_info
  80     void altMerge(OptMapInfo other) {
  81         /* if (! is_equal_mml(&to->mmd, &add->mmd)) return ; */
  82         if (value == 0) return;
  83         if (other.value == 0 || mmd.max < other.mmd.max) {
  84             clear();
  85             return;
  86         }
  87 
  88         mmd.altMerge(other.mmd);
  89 
  90         int val = 0;
  91         for (int i=0; i<Config.CHAR_TABLE_SIZE; i++) {
  92             if (other.map[i] != 0) map[i] = 1;
  93             if (map[i] != 0) val += positionValue(i);
  94         }
  95 
  96         value = val;
  97         anchor.altMerge(other.anchor);
  98     }
  99 
 100     static final short ByteValTable[] = {
 101         5,  1,  1,  1,  1,  1,  1,  1,  1, 10, 10,  1,  1, 10,  1,  1,
 102         1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
 103        12,  4,  7,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,
 104         6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  5,  5,  5,  5,  5,  5,
 105         5,  6,  6,  6,  6,  7,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,
 106         6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  5,  6,  5,  5,  5,
 107         5,  6,  6,  6,  6,  7,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,
 108         6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  5,  5,  5,  5,  1
 109      };
 110 
 111     // map_position_value
 112     static int positionValue(int i) {
 113         if (i < ByteValTable.length) {
 114             return ByteValTable[i];
 115         } else {
 116             return 4; /* Take it easy. */
 117         }
 118     }
 119 
 120 }