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 Region {
  23     static final int REGION_NOTPOS = -1;
  24 
  25     public final int numRegs;
  26     public final int[]beg;
  27     public final int[]end;
  28     public CaptureTreeNode historyRoot;
  29 
  30     public Region(int num) {
  31         this.numRegs = num;
  32         this.beg = new int[num];
  33         this.end = new int[num];
  34     }
  35 
  36     public Region(int begin, int end) {
  37         this.numRegs = 1;
  38         this.beg = new int[]{begin};
  39         this.end = new int[]{end};
  40     }
  41 
  42     public Region clone() {
  43         Region region = new Region(numRegs);
  44         System.arraycopy(beg, 0, region.beg, 0, beg.length);
  45         System.arraycopy(end, 0, region.end, 0, end.length);
  46         if (historyRoot != null) region.historyRoot = historyRoot.cloneTree();
  47         return region;
  48     }
  49 
  50     public String toString() {
  51         StringBuilder sb = new StringBuilder();
  52         sb.append("Region: \n");
  53         for (int i=0; i<beg.length; i++) sb.append(" " + i + ": (" + beg[i] + "-" + end[i] + ")");
  54         return sb.toString();
  55     }
  56 
  57     CaptureTreeNode getCaptureTree() {
  58         return historyRoot;
  59     }
  60 
  61     void clear() {
  62         for (int i=0; i<beg.length; i++) {
  63             beg[i] = end[i] = REGION_NOTPOS;
  64         }
  65     }
  66 }