|
1 /* |
|
2 * Copyright (c) 2008-2010 Mozilla Foundation |
|
3 * |
|
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: |
|
10 * |
|
11 * The above copyright notice and this permission notice shall be included in |
|
12 * all copies or substantial portions of the Software. |
|
13 * |
|
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. |
|
21 */ |
|
22 |
|
23 package nu.validator.htmlparser.impl; |
|
24 |
|
25 import nu.validator.htmlparser.annotation.NoLength; |
|
26 |
|
27 /** |
|
28 * An UTF-16 buffer that knows the start and end indeces of its unconsumed |
|
29 * content. |
|
30 * |
|
31 * @version $Id$ |
|
32 * @author hsivonen |
|
33 */ |
|
34 public final class UTF16Buffer { |
|
35 |
|
36 /** |
|
37 * The backing store of the buffer. May be larger than the logical content |
|
38 * of this <code>UTF16Buffer</code>. |
|
39 */ |
|
40 private final @NoLength char[] buffer; |
|
41 |
|
42 /** |
|
43 * The index of the first unconsumed character in the backing buffer. |
|
44 */ |
|
45 private int start; |
|
46 |
|
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>. |
|
51 */ |
|
52 private int end; |
|
53 |
|
54 //[NOCPP[ |
|
55 |
|
56 /** |
|
57 * Constructor for wrapping an existing UTF-16 code unit array. |
|
58 * |
|
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 |
|
65 */ |
|
66 public UTF16Buffer(@NoLength char[] buffer, int start, int end) { |
|
67 this.buffer = buffer; |
|
68 this.start = start; |
|
69 this.end = end; |
|
70 } |
|
71 |
|
72 // ]NOCPP] |
|
73 |
|
74 /** |
|
75 * Returns the start index. |
|
76 * |
|
77 * @return the start index |
|
78 */ |
|
79 public int getStart() { |
|
80 return start; |
|
81 } |
|
82 |
|
83 /** |
|
84 * Sets the start index. |
|
85 * |
|
86 * @param start |
|
87 * the start index |
|
88 */ |
|
89 public void setStart(int start) { |
|
90 this.start = start; |
|
91 } |
|
92 |
|
93 /** |
|
94 * Returns the backing buffer. |
|
95 * |
|
96 * @return the backing buffer |
|
97 */ |
|
98 public @NoLength char[] getBuffer() { |
|
99 return buffer; |
|
100 } |
|
101 |
|
102 /** |
|
103 * Returns the end index. |
|
104 * |
|
105 * @return the end index |
|
106 */ |
|
107 public int getEnd() { |
|
108 return end; |
|
109 } |
|
110 |
|
111 /** |
|
112 * Checks if the buffer has data left. |
|
113 * |
|
114 * @return <code>true</code> if there's data left |
|
115 */ |
|
116 public boolean hasMore() { |
|
117 return start < end; |
|
118 } |
|
119 |
|
120 /** |
|
121 * Adjusts the start index to skip over the first character if it is a line |
|
122 * feed and the previous character was a carriage return. |
|
123 * |
|
124 * @param lastWasCR |
|
125 * whether the previous character was a carriage return |
|
126 */ |
|
127 public void adjust(boolean lastWasCR) { |
|
128 if (lastWasCR && buffer[start] == '\n') { |
|
129 start++; |
|
130 } |
|
131 } |
|
132 |
|
133 /** |
|
134 * Sets the end index. |
|
135 * |
|
136 * @param end |
|
137 * the end index |
|
138 */ |
|
139 public void setEnd(int end) { |
|
140 this.end = end; |
|
141 } |
|
142 } |