001 /*
002 * Copyright (c) 2008-2010 Mozilla Foundation
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a
005 * copy of this software and associated documentation files (the "Software"),
006 * to deal in the Software without restriction, including without limitation
007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
008 * and/or sell copies of the Software, and to permit persons to whom the
009 * Software is furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020 * DEALINGS IN THE SOFTWARE.
021 */
022
023 package nu.validator.htmlparser.impl;
024
025 import nu.validator.htmlparser.annotation.NoLength;
026
027 /**
028 * An UTF-16 buffer that knows the start and end indeces of its unconsumed
029 * content.
030 *
031 * @version $Id$
032 * @author hsivonen
033 */
034 public final class UTF16Buffer {
035
036 /**
037 * The backing store of the buffer. May be larger than the logical content
038 * of this <code>UTF16Buffer</code>.
039 */
040 private final @NoLength char[] buffer;
041
042 /**
043 * The index of the first unconsumed character in the backing buffer.
044 */
045 private int start;
046
047 /**
048 * The index of the slot immediately after the last character in the backing
049 * buffer that is part of the logical content of this
050 * <code>UTF16Buffer</code>.
051 */
052 private int end;
053
054 /**
055 * Constructor for wrapping an existing UTF-16 code unit array.
056 *
057 * @param buffer
058 * the backing buffer
059 * @param start
060 * the index of the first character to consume
061 * @param end
062 * the index immediately after the last character to consume
063 */
064 public UTF16Buffer(@NoLength char[] buffer, int start, int end) {
065 this.buffer = buffer;
066 this.start = start;
067 this.end = end;
068 }
069
070 /**
071 * Returns the start index.
072 *
073 * @return the start index
074 */
075 public int getStart() {
076 return start;
077 }
078
079 /**
080 * Sets the start index.
081 *
082 * @param start
083 * the start index
084 */
085 public void setStart(int start) {
086 this.start = start;
087 }
088
089 /**
090 * Returns the backing buffer.
091 *
092 * @return the backing buffer
093 */
094 public @NoLength char[] getBuffer() {
095 return buffer;
096 }
097
098 /**
099 * Returns the end index.
100 *
101 * @return the end index
102 */
103 public int getEnd() {
104 return end;
105 }
106
107 /**
108 * Checks if the buffer has data left.
109 *
110 * @return <code>true</code> if there's data left
111 */
112 public boolean hasMore() {
113 return start < end;
114 }
115
116 /**
117 * Adjusts the start index to skip over the first character if it is a line
118 * feed and the previous character was a carriage return.
119 *
120 * @param lastWasCR
121 * whether the previous character was a carriage return
122 */
123 public void adjust(boolean lastWasCR) {
124 if (lastWasCR && buffer[start] == '\n') {
125 start++;
126 }
127 }
128
129 /**
130 * Sets the end index.
131 *
132 * @param end
133 * the end index
134 */
135 public void setEnd(int end) {
136 this.end = end;
137 }
138 }