rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Feb 2014 07:54:35 +0100
branchReducedStack
changeset 1466 39d26d3686d9
parent 1457 b9386cc3ff7b
child 1587 bf08bd96d408
permissions -rw-r--r--
Don't clear the stack mapping when doing a cast
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 /**
    21  *
    22  * @author Jaroslav Tulach <jtulach@netbeans.org>
    23  */
    24 class StringArray {
    25     private String[] arr;
    26 
    27     public StringArray() {
    28     }
    29 
    30     private StringArray(String[] arr) {
    31         this.arr = arr;
    32     }
    33     
    34     public void add(String s) {
    35         if (arr == null) {
    36             arr = new String[1];
    37         } else {
    38             String[] tmp = new String[arr.length + 1];
    39             for (int i = 0; i < arr.length; i++) {
    40                 tmp[i] = arr[i];
    41             }
    42             arr = tmp;
    43         }
    44         arr[arr.length - 1] = s;
    45     }
    46 
    47     StringArray addAndNew(String... values) {
    48         int j;
    49         String[] tmp;
    50         if (arr == null) {
    51             tmp = new String[values.length];
    52             j = 0;
    53         } else {
    54             tmp = new String[arr.length + values.length];
    55             for (int i = 0; i < arr.length; i++) {
    56                 tmp[i] = arr[i];
    57             }
    58             j = arr.length;
    59         }
    60         for (int i = 0; i < values.length;) {
    61             tmp[j++] = values[i++];
    62         }
    63         return new StringArray(tmp);
    64     }
    65     
    66     public String[] toArray() {
    67         return arr == null ? new String[0] : arr;
    68     }
    69     
    70     static StringArray asList(String... names) {
    71         return new StringArray(names);
    72     }
    73 
    74     void reverse() {
    75         for (int i = 0, j = arr.length; i < j; i++) {
    76             String s = arr[i];
    77             arr[i] = arr[--j];
    78             arr[j] = s;
    79         }
    80     }
    81 
    82     boolean contains(String n) {
    83         if (arr == null) {
    84             return false;
    85         }
    86         for (int i = 0; i < arr.length; i++) {
    87             if (n.equals(arr[i])) {
    88                 return true;
    89             }
    90         }
    91         return false;
    92     }
    93 
    94     void delete(int indx) {
    95         if (arr == null || indx < 0 || indx >= arr.length) {
    96             return;
    97         }
    98         String[] tmp = new String[arr.length - 1];
    99         for (int i = 0, j = 0; i < arr.length; i++) {
   100             if (i != indx) {
   101                 tmp[j++] = arr[i];
   102             }
   103         }
   104         arr = tmp;
   105     }
   106 
   107     int indexOf(String ic) {
   108         if (arr != null) for (int i = 0; i < arr.length; i++) {
   109             if (ic.equals(arr[i])) {
   110                 return i;
   111             }
   112         }
   113         return -1;
   114     }
   115 
   116     String getAndClear(int count, boolean clear) {
   117         String s = arr[count];
   118         if (clear) {
   119             arr[count] = null;
   120         }
   121         return s;
   122     }
   123 
   124     void clear() {
   125         arr = null;
   126     }
   127 }