Bug 1843499 - Part 2: Rethrow with exception stack in for-of loop. r=iain
[gecko.git] / parser / html / javasrc / UTF16Buffer.java
blobec79185ec20efd99b6fd9f213ac4458c4074fc79
1 /*
2 * Copyright (c) 2008-2010 Mozilla Foundation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
23 package nu.validator.htmlparser.impl;
25 import nu.validator.htmlparser.annotation.NoLength;
27 /**
28 * An UTF-16 buffer that knows the start and end indeces of its unconsumed
29 * content.
31 * @version $Id$
32 * @author hsivonen
34 public final class UTF16Buffer {
36 /**
37 * The backing store of the buffer. May be larger than the logical content
38 * of this <code>UTF16Buffer</code>.
40 private final @NoLength char[] buffer;
42 /**
43 * The index of the first unconsumed character in the backing buffer.
45 private int start;
47 /**
48 * The index of the slot immediately after the last character in the backing
49 * buffer that is part of the logical content of this
50 * <code>UTF16Buffer</code>.
52 private int end;
54 //[NOCPP[
56 /**
57 * Constructor for wrapping an existing UTF-16 code unit array.
59 * @param buffer
60 * the backing buffer
61 * @param start
62 * the index of the first character to consume
63 * @param end
64 * the index immediately after the last character to consume
66 public UTF16Buffer(@NoLength char[] buffer, int start, int end) {
67 this.buffer = buffer;
68 this.start = start;
69 this.end = end;
72 // ]NOCPP]
74 /**
75 * Returns the start index.
77 * @return the start index
79 public int getStart() {
80 return start;
83 /**
84 * Sets the start index.
86 * @param start
87 * the start index
89 public void setStart(int start) {
90 this.start = start;
93 /**
94 * Returns the backing buffer.
96 * @return the backing buffer
98 public @NoLength char[] getBuffer() {
99 return buffer;
103 * Returns the end index.
105 * @return the end index
107 public int getEnd() {
108 return end;
112 * Checks if the buffer has data left.
114 * @return <code>true</code> if there's data left
116 public boolean hasMore() {
117 return start < end;
121 * Returns <code>end - start</code>.
123 * @return <code>end - start</code>
125 public int getLength() {
126 return end - start;
130 * Adjusts the start index to skip over the first character if it is a line
131 * feed and the previous character was a carriage return.
133 * @param lastWasCR
134 * whether the previous character was a carriage return
136 public void adjust(boolean lastWasCR) {
137 if (lastWasCR && buffer[start] == '\n') {
138 start++;
143 * Sets the end index.
145 * @param end
146 * the end index
148 public void setEnd(int end) {
149 this.end = end;