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      */
UTF16Buffer(@oLength char[] buffer, int start, int end)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      */
getStart()79     public int getStart() {
80         return start;
81     }
82 
83     /**
84      * Sets the start index.
85      *
86      * @param start
87      *            the start index
88      */
setStart(int start)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      */
getBuffer()98     public @NoLength char[] getBuffer() {
99         return buffer;
100     }
101 
102     /**
103      * Returns the end index.
104      *
105      * @return the end index
106      */
getEnd()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      */
hasMore()116     public boolean hasMore() {
117         return start < end;
118     }
119 
120     /**
121      * Returns <code>end - start</code>.
122      *
123      * @return <code>end - start</code>
124      */
getLength()125     public int getLength() {
126         return end - start;
127     }
128 
129     /**
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.
132      *
133      * @param lastWasCR
134      *            whether the previous character was a carriage return
135      */
adjust(boolean lastWasCR)136     public void adjust(boolean lastWasCR) {
137         if (lastWasCR && buffer[start] == '\n') {
138             start++;
139         }
140     }
141 
142     /**
143      * Sets the end index.
144      *
145      * @param end
146      *            the end index
147      */
setEnd(int end)148     public void setEnd(int end) {
149         this.end = end;
150     }
151 }
152