Skip to content

Commit d66d273

Browse files
Merge pull request #10947 from Snuffleupagus/document-find-peekBytes
Make the `find` helper function, in `src/core/document.js`, more efficient by using `peekBytes` rather reading the stream one byte at a time
2 parents a98ce9c + bdc31f8 commit d66d273

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

src/core/document.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
/* eslint no-var: error */
1616

1717
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
2020
} from '../shared/util';
2121
import { Catalog, ObjectLoader, XRef } from './obj';
2222
import { Dict, isDict, isName, isStream, Ref } from './primitives';
@@ -337,20 +337,11 @@ const FINGERPRINT_FIRST_BYTES = 1024;
337337
const EMPTY_FINGERPRINT = '\x00\x00\x00\x00\x00\x00\x00' +
338338
'\x00\x00\x00\x00\x00\x00\x00\x00\x00';
339339

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.');
346342

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));
352344

353-
stream.pos = pos;
354345
const index = backwards ? str.lastIndexOf(needle) : str.indexOf(needle);
355346
if (index === -1) {
356347
return false;

0 commit comments

Comments
 (0)