nashorn/src/jdk/nashorn/internal/runtime/regexp/joni/UnsetAddrList.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 import jdk.nashorn.internal.runtime.regexp.joni.ast.EncloseNode;
       
    23 import jdk.nashorn.internal.runtime.regexp.joni.ast.Node;
       
    24 import jdk.nashorn.internal.runtime.regexp.joni.exception.ErrorMessages;
       
    25 import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
       
    26 
       
    27 public final class UnsetAddrList {
       
    28     int num;
       
    29     Node[]targets;
       
    30     int[]offsets;
       
    31 
       
    32     public UnsetAddrList(int size) {
       
    33         targets = new Node[size];
       
    34         offsets = new int[size];
       
    35     }
       
    36 
       
    37     public void add(int offset, Node node) {
       
    38         if (num >= offsets.length) {
       
    39             Node []ttmp = new Node[targets.length << 1];
       
    40             System.arraycopy(targets, 0, ttmp, 0, num);
       
    41             targets = ttmp;
       
    42             int[]otmp = new int[offsets.length << 1];
       
    43             System.arraycopy(offsets, 0, otmp, 0, num);
       
    44             offsets = otmp;
       
    45         }
       
    46         targets[num] = node;
       
    47         offsets[num] = offset;
       
    48 
       
    49         num++;
       
    50     }
       
    51 
       
    52     public void fix(Regex regex) {
       
    53         for (int i=0; i<num; i++) {
       
    54             EncloseNode en = (EncloseNode)targets[i];
       
    55             if (!en.isAddrFixed()) new InternalException(ErrorMessages.ERR_PARSER_BUG);
       
    56             regex.code[offsets[i]] = en.callAddr; // is this safe ?
       
    57         }
       
    58     }
       
    59 
       
    60     public String toString() {
       
    61         StringBuilder value = new StringBuilder();
       
    62         if (num > 0) {
       
    63             for (int i=0; i<num; i++) {
       
    64                 value.append("offset + " + offsets[i] + " target: " + targets[i].getAddressName());
       
    65             }
       
    66         }
       
    67         return value.toString();
       
    68     }
       
    69 }