src/jdk/nashorn/internal/runtime/regexp/joni/ScannerSupport.java

Thu, 16 May 2013 19:52:39 +0200

author
hannesw
date
Thu, 16 May 2013 19:52:39 +0200
changeset 273
98798a6336de
parent 115
e42fd1640ff9
child 447
7503f30c1355
permissions
-rw-r--r--

8012359: Increase code coverage in Joni
Reviewed-by: jlaskey, lagergren

     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;
    22 import jdk.nashorn.internal.runtime.regexp.joni.encoding.IntHolder;
    23 import jdk.nashorn.internal.runtime.regexp.joni.exception.ErrorMessages;
    24 import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
    25 import jdk.nashorn.internal.runtime.regexp.joni.exception.SyntaxException;
    26 import jdk.nashorn.internal.runtime.regexp.joni.exception.ValueException;
    28 abstract class ScannerSupport extends IntHolder implements ErrorMessages {
    30     protected final char[] chars;       // pattern
    31     protected int p;                    // current scanner position
    32     protected int stop;                 // pattern end (mutable)
    33     private int lastFetched;            // last fetched value for unfetch support
    34     protected int c;                    // current code point
    36     private final int begin;            // pattern begin position for reset() support
    37     private final int end;              // pattern end position for reset() support
    38     protected int _p;                   // used by mark()/restore() to mark positions
    40     private final static int INT_SIGN_BIT = 1 << 31;
    42     protected ScannerSupport(char[] chars, int p, int end) {
    43         this.chars = chars;
    44         this.begin = p;
    45         this.end = end;
    47         reset();
    48     }
    50     protected int getBegin() {
    51         return begin;
    52     }
    54     protected int getEnd() {
    55         return end;
    56     }
    58     protected final int scanUnsignedNumber() {
    59         int last = c;
    60         int num = 0; // long ???
    61         while(left()) {
    62             fetch();
    63             if (Character.isDigit(c)) {
    64                 int onum = num;
    65                 num = num * 10 + EncodingHelper.digitVal(c);
    66                 if (((onum ^ num) & INT_SIGN_BIT) != 0) return -1;
    67             } else {
    68                 unfetch();
    69                 break;
    70             }
    71         }
    72         c = last;
    73         return num;
    74     }
    76     protected final int scanUnsignedHexadecimalNumber(int maxLength) {
    77         int last = c;
    78         int num = 0;
    79         while(left() && maxLength-- != 0) {
    80             fetch();
    81             if (EncodingHelper.isXDigit(c)) {
    82                 int onum = num;
    83                 int val = EncodingHelper.xdigitVal(c);
    84                 num = (num << 4) + val;
    85                 if (((onum ^ num) & INT_SIGN_BIT) != 0) return -1;
    86             } else {
    87                 unfetch();
    88                 break;
    89             }
    90         }
    91         c = last;
    92         return num;
    93     }
    95     protected final int scanUnsignedOctalNumber(int maxLength) {
    96         int last = c;
    97         int num = 0;
    98         while(left() && maxLength-- != 0) {
    99             fetch();
   100             if (Character.isDigit(c) && c < '8') {
   101                 int onum = num;
   102                 int val = EncodingHelper.odigitVal(c);
   103                 num = (num << 3) + val;
   104                 if (((onum ^ num) & INT_SIGN_BIT) != 0) return -1;
   105             } else {
   106                 unfetch();
   107                 break;
   108             }
   109         }
   110         c = last;
   111         return num;
   112     }
   114     protected final void reset() {
   115         p = begin;
   116         stop = end;
   117     }
   119     protected final void mark() {
   120         _p = p;
   121     }
   123     protected final void restore() {
   124         p = _p;
   125     }
   127     protected final void inc() {
   128         lastFetched = p;
   129         p++;
   130     }
   132     protected final void fetch() {
   133         lastFetched = p;
   134         c = chars[p++];
   135     }
   137     protected int fetchTo() {
   138         lastFetched = p;
   139         return chars[p++];
   140     }
   142     protected final void unfetch() {
   143         p = lastFetched;
   144     }
   146     protected final int peek() {
   147         return p < stop ? chars[p] : 0;
   148     }
   150     protected final boolean peekIs(int c) {
   151         return peek() == c;
   152     }
   154     protected final boolean left() {
   155         return p < stop;
   156     }
   158     protected void newSyntaxException(String message) {
   159         throw new SyntaxException(message);
   160     }
   162     protected void newValueException(String message) {
   163         throw new ValueException(message);
   164     }
   166     protected void newValueException(String message, String str) {
   167         throw new ValueException(message, str);
   168     }
   170     protected void newValueException(String message, int p, int end) {
   171         throw new ValueException(message, new String(chars, p, end - p));
   172     }
   174     protected void newInternalException(String message) {
   175         throw new InternalException(message);
   176     }
   178 }

mercurial