|
15 | 15 | /* eslint no-var: error */ |
16 | 16 |
|
17 | 17 | import { |
18 | | - assert, FormatError, info, isArrayBuffer, isBool, isNum, isSpace, isString, |
19 | | - OPS, shadow, stringToBytes, stringToPDFString, Util, warn |
| 18 | + assert, bytesToString, FormatError, info, isArrayBuffer, isBool, isNum, |
| 19 | + isSpace, isString, OPS, shadow, stringToBytes, stringToPDFString, Util, warn |
20 | 20 | } from '../shared/util'; |
21 | 21 | import { Catalog, ObjectLoader, XRef } from './obj'; |
22 | 22 | import { Dict, isDict, isName, isStream, Ref } from './primitives'; |
@@ -337,20 +337,11 @@ const FINGERPRINT_FIRST_BYTES = 1024; |
337 | 337 | const EMPTY_FINGERPRINT = '\x00\x00\x00\x00\x00\x00\x00' + |
338 | 338 | '\x00\x00\x00\x00\x00\x00\x00\x00\x00'; |
339 | 339 |
|
340 | | -function find(stream, needle, limit, backwards) { |
341 | | - const pos = stream.pos; |
342 | | - const end = stream.end; |
343 | | - if (pos + limit > end) { |
344 | | - limit = end - pos; |
345 | | - } |
| 340 | +function find(stream, needle, limit, backwards = false) { |
| 341 | + assert(limit > 0, 'The "limit" must be a positive integer.'); |
346 | 342 |
|
347 | | - const strBuf = []; |
348 | | - for (let i = 0; i < limit; ++i) { |
349 | | - strBuf.push(String.fromCharCode(stream.getByte())); |
350 | | - } |
351 | | - const str = strBuf.join(''); |
| 343 | + const str = bytesToString(stream.peekBytes(limit)); |
352 | 344 |
|
353 | | - stream.pos = pos; |
354 | 345 | const index = backwards ? str.lastIndexOf(needle) : str.indexOf(needle); |
355 | 346 | if (index === -1) { |
356 | 347 | return false; |
|
0 commit comments