Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2008-2010 Mozilla Foundation |
michael@0 | 3 | * |
michael@0 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
michael@0 | 5 | * copy of this software and associated documentation files (the "Software"), |
michael@0 | 6 | * to deal in the Software without restriction, including without limitation |
michael@0 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
michael@0 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
michael@0 | 9 | * Software is furnished to do so, subject to the following conditions: |
michael@0 | 10 | * |
michael@0 | 11 | * The above copyright notice and this permission notice shall be included in |
michael@0 | 12 | * all copies or substantial portions of the Software. |
michael@0 | 13 | * |
michael@0 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
michael@0 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
michael@0 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
michael@0 | 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
michael@0 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
michael@0 | 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
michael@0 | 20 | * DEALINGS IN THE SOFTWARE. |
michael@0 | 21 | */ |
michael@0 | 22 | |
michael@0 | 23 | package nu.validator.htmlparser.impl; |
michael@0 | 24 | |
michael@0 | 25 | import nu.validator.htmlparser.annotation.NoLength; |
michael@0 | 26 | |
michael@0 | 27 | /** |
michael@0 | 28 | * An UTF-16 buffer that knows the start and end indeces of its unconsumed |
michael@0 | 29 | * content. |
michael@0 | 30 | * |
michael@0 | 31 | * @version $Id$ |
michael@0 | 32 | * @author hsivonen |
michael@0 | 33 | */ |
michael@0 | 34 | public final class UTF16Buffer { |
michael@0 | 35 | |
michael@0 | 36 | /** |
michael@0 | 37 | * The backing store of the buffer. May be larger than the logical content |
michael@0 | 38 | * of this <code>UTF16Buffer</code>. |
michael@0 | 39 | */ |
michael@0 | 40 | private final @NoLength char[] buffer; |
michael@0 | 41 | |
michael@0 | 42 | /** |
michael@0 | 43 | * The index of the first unconsumed character in the backing buffer. |
michael@0 | 44 | */ |
michael@0 | 45 | private int start; |
michael@0 | 46 | |
michael@0 | 47 | /** |
michael@0 | 48 | * The index of the slot immediately after the last character in the backing |
michael@0 | 49 | * buffer that is part of the logical content of this |
michael@0 | 50 | * <code>UTF16Buffer</code>. |
michael@0 | 51 | */ |
michael@0 | 52 | private int end; |
michael@0 | 53 | |
michael@0 | 54 | //[NOCPP[ |
michael@0 | 55 | |
michael@0 | 56 | /** |
michael@0 | 57 | * Constructor for wrapping an existing UTF-16 code unit array. |
michael@0 | 58 | * |
michael@0 | 59 | * @param buffer |
michael@0 | 60 | * the backing buffer |
michael@0 | 61 | * @param start |
michael@0 | 62 | * the index of the first character to consume |
michael@0 | 63 | * @param end |
michael@0 | 64 | * the index immediately after the last character to consume |
michael@0 | 65 | */ |
michael@0 | 66 | public UTF16Buffer(@NoLength char[] buffer, int start, int end) { |
michael@0 | 67 | this.buffer = buffer; |
michael@0 | 68 | this.start = start; |
michael@0 | 69 | this.end = end; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | // ]NOCPP] |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * Returns the start index. |
michael@0 | 76 | * |
michael@0 | 77 | * @return the start index |
michael@0 | 78 | */ |
michael@0 | 79 | public int getStart() { |
michael@0 | 80 | return start; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | /** |
michael@0 | 84 | * Sets the start index. |
michael@0 | 85 | * |
michael@0 | 86 | * @param start |
michael@0 | 87 | * the start index |
michael@0 | 88 | */ |
michael@0 | 89 | public void setStart(int start) { |
michael@0 | 90 | this.start = start; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | /** |
michael@0 | 94 | * Returns the backing buffer. |
michael@0 | 95 | * |
michael@0 | 96 | * @return the backing buffer |
michael@0 | 97 | */ |
michael@0 | 98 | public @NoLength char[] getBuffer() { |
michael@0 | 99 | return buffer; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * Returns the end index. |
michael@0 | 104 | * |
michael@0 | 105 | * @return the end index |
michael@0 | 106 | */ |
michael@0 | 107 | public int getEnd() { |
michael@0 | 108 | return end; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | /** |
michael@0 | 112 | * Checks if the buffer has data left. |
michael@0 | 113 | * |
michael@0 | 114 | * @return <code>true</code> if there's data left |
michael@0 | 115 | */ |
michael@0 | 116 | public boolean hasMore() { |
michael@0 | 117 | return start < end; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | /** |
michael@0 | 121 | * Adjusts the start index to skip over the first character if it is a line |
michael@0 | 122 | * feed and the previous character was a carriage return. |
michael@0 | 123 | * |
michael@0 | 124 | * @param lastWasCR |
michael@0 | 125 | * whether the previous character was a carriage return |
michael@0 | 126 | */ |
michael@0 | 127 | public void adjust(boolean lastWasCR) { |
michael@0 | 128 | if (lastWasCR && buffer[start] == '\n') { |
michael@0 | 129 | start++; |
michael@0 | 130 | } |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | /** |
michael@0 | 134 | * Sets the end index. |
michael@0 | 135 | * |
michael@0 | 136 | * @param end |
michael@0 | 137 | * the end index |
michael@0 | 138 | */ |
michael@0 | 139 | public void setEnd(int end) { |
michael@0 | 140 | this.end = end; |
michael@0 | 141 | } |
michael@0 | 142 | } |