* Consume the stream until we reach the specified byte or sequence of bytes. * * @param {number|List } val
(val)
| 166 | * @param {number|List<number>} val |
| 167 | */ |
| 168 | continueUntil(val) { |
| 169 | if (this.position > this.length) return; |
| 170 | |
| 171 | this.bitPos = 0; |
| 172 | |
| 173 | if (typeof val === "number") { |
| 174 | while (++this.position < this.length && this.bytes[this.position] !== val) { |
| 175 | continue; |
| 176 | } |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | // val is an array |
| 181 | |
| 182 | /** |
| 183 | * Builds the skip forward table from the value to be searched. |
| 184 | * |
| 185 | * @param {Uint8Array} val |
| 186 | * @param {Number} len |
| 187 | * @returns {Uint8Array} |
| 188 | */ |
| 189 | function preprocess(val, len) { |
| 190 | const skiptable = new Array(); |
| 191 | val.forEach((element, index) => { |
| 192 | skiptable[element] = len - index; |
| 193 | }); |
| 194 | return skiptable; |
| 195 | } |
| 196 | |
| 197 | const length = val.length; |
| 198 | const initial = val[length-1]; |
| 199 | this.position = length; |
| 200 | |
| 201 | // Get the skip table. |
| 202 | const skiptable = preprocess(val, length); |
| 203 | let found; |
| 204 | |
| 205 | while (this.position < this.length) { |
| 206 | // Until we hit the final element of val in the stream. |
| 207 | while ((this.position < this.length) && (this.bytes[this.position++] !== initial)); |
| 208 | |
| 209 | found = true; |
| 210 | |
| 211 | // Loop through the elements comparing them to val. |
| 212 | for (let x = length-1; x >= 0; x--) { |
| 213 | if (this.bytes[this.position - length + x] !== val[x]) { |
| 214 | found = false; |
| 215 | |
| 216 | // If element is not equal to val's element then jump forward by the correct amount. |
| 217 | this.position += skiptable[val[x]]; |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | if (found) { |
| 222 | this.position -= length; |
| 223 | break; |
| 224 | } |
| 225 | } |
no outgoing calls
no test coverage detected