nashorn/src/jdk/nashorn/internal/runtime/regexp/joni/NameEntry.java
changeset 18311 312f05b0462d
parent 18310 6b514cf7a2c3
parent 17815 b72ae39e1329
child 18312 c940914e1849
equal deleted inserted replaced
18310:6b514cf7a2c3 18311:312f05b0462d
     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 public final class NameEntry {
       
    23     static final int INIT_NAME_BACKREFS_ALLOC_NUM = 8;
       
    24 
       
    25     public final char[] name;
       
    26     public final int nameP;
       
    27     public final int nameEnd;
       
    28 
       
    29     int backNum;
       
    30     int backRef1;
       
    31     int backRefs[];
       
    32 
       
    33     public NameEntry(char[] chars, int p, int end) {
       
    34         name = chars;
       
    35         nameP = p;
       
    36         nameEnd = end;
       
    37     }
       
    38 
       
    39     public int[] getBackRefs() {
       
    40         switch (backNum) {
       
    41         case 0:
       
    42             return new int[]{};
       
    43         case 1:
       
    44             return new int[]{backRef1};
       
    45         default:
       
    46             int[]result = new int[backNum];
       
    47             System.arraycopy(backRefs, 0, result, 0, backNum);
       
    48             return result;
       
    49         }
       
    50     }
       
    51 
       
    52     private void alloc() {
       
    53         backRefs = new int[INIT_NAME_BACKREFS_ALLOC_NUM];
       
    54     }
       
    55 
       
    56     private void ensureSize() {
       
    57         if (backNum > backRefs.length) {
       
    58             int[]tmp = new int[backRefs.length << 1];
       
    59             System.arraycopy(backRefs, 0, tmp, 0, backRefs.length);
       
    60             backRefs = tmp;
       
    61         }
       
    62     }
       
    63 
       
    64     public void addBackref(int backRef) {
       
    65         backNum++;
       
    66 
       
    67         switch (backNum) {
       
    68             case 1:
       
    69                 backRef1 = backRef;
       
    70                 break;
       
    71             case 2:
       
    72                 alloc();
       
    73                 backRefs[0] = backRef1;
       
    74                 backRefs[1] = backRef;
       
    75                 break;
       
    76             default:
       
    77                 ensureSize();
       
    78                 backRefs[backNum - 1] = backRef;
       
    79         }
       
    80     }
       
    81 
       
    82     public String toString() {
       
    83         StringBuilder buff = new StringBuilder(new String(name, nameP, nameEnd - nameP) + " ");
       
    84         if (backNum == 0) {
       
    85             buff.append("-");
       
    86         } else if (backNum == 1){
       
    87             buff.append(backRef1);
       
    88         } else {
       
    89             for (int i=0; i<backNum; i++){
       
    90                 if (i > 0) buff.append(", ");
       
    91                 buff.append(backRefs[i]);
       
    92             }
       
    93         }
       
    94         return buff.toString();
       
    95     }
       
    96 
       
    97 }