michael@0: /*
michael@0: * Copyright (c) 2008-2010 Mozilla Foundation
michael@0: *
michael@0: * Permission is hereby granted, free of charge, to any person obtaining a
michael@0: * copy of this software and associated documentation files (the "Software"),
michael@0: * to deal in the Software without restriction, including without limitation
michael@0: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
michael@0: * and/or sell copies of the Software, and to permit persons to whom the
michael@0: * Software is furnished to do so, subject to the following conditions:
michael@0: *
michael@0: * The above copyright notice and this permission notice shall be included in
michael@0: * all copies or substantial portions of the Software.
michael@0: *
michael@0: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
michael@0: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
michael@0: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
michael@0: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
michael@0: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
michael@0: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
michael@0: * DEALINGS IN THE SOFTWARE.
michael@0: */
michael@0:
michael@0: package nu.validator.htmlparser.impl;
michael@0:
michael@0: import nu.validator.htmlparser.annotation.NoLength;
michael@0:
michael@0: /**
michael@0: * An UTF-16 buffer that knows the start and end indeces of its unconsumed
michael@0: * content.
michael@0: *
michael@0: * @version $Id$
michael@0: * @author hsivonen
michael@0: */
michael@0: public final class UTF16Buffer {
michael@0:
michael@0: /**
michael@0: * The backing store of the buffer. May be larger than the logical content
michael@0: * of this UTF16Buffer
.
michael@0: */
michael@0: private final @NoLength char[] buffer;
michael@0:
michael@0: /**
michael@0: * The index of the first unconsumed character in the backing buffer.
michael@0: */
michael@0: private int start;
michael@0:
michael@0: /**
michael@0: * The index of the slot immediately after the last character in the backing
michael@0: * buffer that is part of the logical content of this
michael@0: * UTF16Buffer
.
michael@0: */
michael@0: private int end;
michael@0:
michael@0: //[NOCPP[
michael@0:
michael@0: /**
michael@0: * Constructor for wrapping an existing UTF-16 code unit array.
michael@0: *
michael@0: * @param buffer
michael@0: * the backing buffer
michael@0: * @param start
michael@0: * the index of the first character to consume
michael@0: * @param end
michael@0: * the index immediately after the last character to consume
michael@0: */
michael@0: public UTF16Buffer(@NoLength char[] buffer, int start, int end) {
michael@0: this.buffer = buffer;
michael@0: this.start = start;
michael@0: this.end = end;
michael@0: }
michael@0:
michael@0: // ]NOCPP]
michael@0:
michael@0: /**
michael@0: * Returns the start index.
michael@0: *
michael@0: * @return the start index
michael@0: */
michael@0: public int getStart() {
michael@0: return start;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Sets the start index.
michael@0: *
michael@0: * @param start
michael@0: * the start index
michael@0: */
michael@0: public void setStart(int start) {
michael@0: this.start = start;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the backing buffer.
michael@0: *
michael@0: * @return the backing buffer
michael@0: */
michael@0: public @NoLength char[] getBuffer() {
michael@0: return buffer;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the end index.
michael@0: *
michael@0: * @return the end index
michael@0: */
michael@0: public int getEnd() {
michael@0: return end;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Checks if the buffer has data left.
michael@0: *
michael@0: * @return true
if there's data left
michael@0: */
michael@0: public boolean hasMore() {
michael@0: return start < end;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Adjusts the start index to skip over the first character if it is a line
michael@0: * feed and the previous character was a carriage return.
michael@0: *
michael@0: * @param lastWasCR
michael@0: * whether the previous character was a carriage return
michael@0: */
michael@0: public void adjust(boolean lastWasCR) {
michael@0: if (lastWasCR && buffer[start] == '\n') {
michael@0: start++;
michael@0: }
michael@0: }
michael@0:
michael@0: /**
michael@0: * Sets the end index.
michael@0: *
michael@0: * @param end
michael@0: * the end index
michael@0: */
michael@0: public void setEnd(int end) {
michael@0: this.end = end;
michael@0: }
michael@0: }