parser/html/javasrc/UTF16Buffer.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/parser/html/javasrc/UTF16Buffer.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,142 @@
     1.4 +/*
     1.5 + * Copyright (c) 2008-2010 Mozilla Foundation
     1.6 + *
     1.7 + * Permission is hereby granted, free of charge, to any person obtaining a 
     1.8 + * copy of this software and associated documentation files (the "Software"), 
     1.9 + * to deal in the Software without restriction, including without limitation 
    1.10 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
    1.11 + * and/or sell copies of the Software, and to permit persons to whom the 
    1.12 + * Software is furnished to do so, subject to the following conditions:
    1.13 + *
    1.14 + * The above copyright notice and this permission notice shall be included in 
    1.15 + * all copies or substantial portions of the Software.
    1.16 + *
    1.17 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
    1.18 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
    1.19 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
    1.20 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
    1.21 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
    1.22 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
    1.23 + * DEALINGS IN THE SOFTWARE.
    1.24 + */
    1.25 +
    1.26 +package nu.validator.htmlparser.impl;
    1.27 +
    1.28 +import nu.validator.htmlparser.annotation.NoLength;
    1.29 +
    1.30 +/**
    1.31 + * An UTF-16 buffer that knows the start and end indeces of its unconsumed
    1.32 + * content.
    1.33 + * 
    1.34 + * @version $Id$
    1.35 + * @author hsivonen
    1.36 + */
    1.37 +public final class UTF16Buffer {
    1.38 +
    1.39 +    /**
    1.40 +     * The backing store of the buffer. May be larger than the logical content
    1.41 +     * of this <code>UTF16Buffer</code>.
    1.42 +     */
    1.43 +    private final @NoLength char[] buffer;
    1.44 +
    1.45 +    /**
    1.46 +     * The index of the first unconsumed character in the backing buffer.
    1.47 +     */
    1.48 +    private int start;
    1.49 +
    1.50 +    /**
    1.51 +     * The index of the slot immediately after the last character in the backing
    1.52 +     * buffer that is part of the logical content of this
    1.53 +     * <code>UTF16Buffer</code>.
    1.54 +     */
    1.55 +    private int end;
    1.56 +
    1.57 +    //[NOCPP[
    1.58 +    
    1.59 +    /**
    1.60 +     * Constructor for wrapping an existing UTF-16 code unit array.
    1.61 +     * 
    1.62 +     * @param buffer
    1.63 +     *            the backing buffer
    1.64 +     * @param start
    1.65 +     *            the index of the first character to consume
    1.66 +     * @param end
    1.67 +     *            the index immediately after the last character to consume
    1.68 +     */
    1.69 +    public UTF16Buffer(@NoLength char[] buffer, int start, int end) {
    1.70 +        this.buffer = buffer;
    1.71 +        this.start = start;
    1.72 +        this.end = end;
    1.73 +    }
    1.74 +
    1.75 +    // ]NOCPP]
    1.76 +    
    1.77 +    /**
    1.78 +     * Returns the start index.
    1.79 +     * 
    1.80 +     * @return the start index
    1.81 +     */
    1.82 +    public int getStart() {
    1.83 +        return start;
    1.84 +    }
    1.85 +
    1.86 +    /**
    1.87 +     * Sets the start index.
    1.88 +     * 
    1.89 +     * @param start
    1.90 +     *            the start index
    1.91 +     */
    1.92 +    public void setStart(int start) {
    1.93 +        this.start = start;
    1.94 +    }
    1.95 +
    1.96 +    /**
    1.97 +     * Returns the backing buffer.
    1.98 +     * 
    1.99 +     * @return the backing buffer
   1.100 +     */
   1.101 +    public @NoLength char[] getBuffer() {
   1.102 +        return buffer;
   1.103 +    }
   1.104 +
   1.105 +    /**
   1.106 +     * Returns the end index.
   1.107 +     * 
   1.108 +     * @return the end index
   1.109 +     */
   1.110 +    public int getEnd() {
   1.111 +        return end;
   1.112 +    }
   1.113 +
   1.114 +    /**
   1.115 +     * Checks if the buffer has data left.
   1.116 +     * 
   1.117 +     * @return <code>true</code> if there's data left
   1.118 +     */
   1.119 +    public boolean hasMore() {
   1.120 +        return start < end;
   1.121 +    }
   1.122 +
   1.123 +    /**
   1.124 +     * Adjusts the start index to skip over the first character if it is a line
   1.125 +     * feed and the previous character was a carriage return.
   1.126 +     * 
   1.127 +     * @param lastWasCR
   1.128 +     *            whether the previous character was a carriage return
   1.129 +     */
   1.130 +    public void adjust(boolean lastWasCR) {
   1.131 +        if (lastWasCR && buffer[start] == '\n') {
   1.132 +            start++;
   1.133 +        }
   1.134 +    }
   1.135 +
   1.136 +    /**
   1.137 +     * Sets the end index.
   1.138 +     * 
   1.139 +     * @param end
   1.140 +     *            the end index
   1.141 +     */
   1.142 +    public void setEnd(int end) {
   1.143 +        this.end = end;
   1.144 +    }
   1.145 +}

mercurial