package com.jsoniter;

import java.io.IOException;

import static java.lang.Character.*;

class IterImplString {

    final static int[] hexDigits = new int['f' + 1];

    static {
        for (int i = 0; i < hexDigits.length; i++) {
            hexDigits[i] = -1;
        }
        for (int i = '0'; i <= '9'; ++i) {
            hexDigits[i] = (i - '0');
        }
        for (int i = 'a'; i <= 'f'; ++i) {
            hexDigits[i] = ((i - 'a') + 10);
        }
        for (int i = 'A'; i <= 'F'; ++i) {
            hexDigits[i] = ((i - 'A') + 10);
        }
    }

    public static final String readString(JsonIterator iter) throws IOException {
        byte c = IterImpl.nextToken(iter);
        if (c == '"') {
            // try fast path first
            int j = 0;
            fast_loop:
            for (; ; ) {
                // copy ascii to buffer
                int i = iter.head;
                for (; i < iter.tail && j < iter.reusableChars.length; i++, j++) {
                    c = iter.buf[i];
                    if (c == '"') {
                        iter.head = i + 1;
                        return new String(iter.reusableChars, 0, j);
                    }
                    // If we encounter a backslash, which is a beginning of an escape sequence
                    // or a high bit was set - indicating an UTF-8 encoded multibyte character,
                    // there is no chance that we can decode the string without instantiating
                    // a temporary buffer, so quit this loop
                    if ((c ^ '\\') < 1) {
                        iter.head = i;
                        break fast_loop;
                    }
                    iter.reusableChars[j] = (char) c;
                }
                if (i == iter.tail) {
                    if (IterImpl.loadMore(iter)) {
                        i = iter.head;
                        continue;
                    } else {
                        throw iter.reportError("readString", "incomplete string");
                    }
                }
                iter.head = i;
                // resize to copy more
                if (j == iter.reusableChars.length) {
                    char[] newBuf = new char[iter.reusableChars.length * 2];
                    System.arraycopy(iter.reusableChars, 0, newBuf, 0, iter.reusableChars.length);
                    iter.reusableChars = newBuf;
                } else {
                    break;
                }
            }
            return IterImpl.readStringSlowPath(iter, j);
        }
        if (c == 'n') {
            IterImpl.skipFixedBytes(iter, 3);
            return null;
        }
        throw iter.reportError("readString", "expect n or \"");
    }

    public static int translateHex(final byte b) {
        int val = hexDigits[b];
        if (val == -1) {
            throw new IndexOutOfBoundsException(b + " is not valid hex digit");
        }
        return val;
    }

    // slice does not allow escape
    final static int findSliceEnd(JsonIterator iter) {
        for (int i = iter.head; i < iter.tail; i++) {
            byte c = iter.buf[i];
            if (c == '"') {
                return i + 1;
            } else if (c == '\\') {
                throw iter.reportError("findSliceEnd", "slice does not support escape char");
            }
        }
        return -1;
    }
}

@@ -1,8 +1,1677 @@
+/*%0Athis implementations contains significant code from https://github.com/ngs-doo/dsl-json/blob/master/LICENSE%0A%0ACopyright (c) 2015, Nova Generacija Softvera d.o.o.%0AAll rights reserved.%0A%0ARedistribution and use in source and binary forms, with or without%0Amodification, are permitted provided that the following conditions are met:%0A%0A    * Redistributions of source code must retain the above copyright notice,%0A      this list of conditions and the following disclaimer.%0A%0A    * Redistributions in binary form must reproduce the above copyright notice,%0A      this list of conditions and the following disclaimer in the documentation%0A      and/or other materials provided with the distribution.%0A%0A    * Neither the name of Nova Generacija Softvera d.o.o. nor the names of its%0A      contributors may be used to endorse or promote products derived from this%0A      software without specific prior written permission.%0A%0ATHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS %22AS IS%22 AND%0AANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED%0AWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE%0ADISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE%0AFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL%0ADAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR%0ASERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER%0ACAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,%0AOR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE%0AOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.%0A */%0A
 package 
@@ -1715,46 +1715,8 @@
 tion
-;%0A%0Aimport static java.lang.Character.*
 ;%0A%0Ac

add license