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
This commit is contained in:
Tim van der Meij 2019-07-06 13:52:22 +02:00 committed by GitHub
commit d66d273869
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,8 +15,8 @@
/* eslint no-var: error */
import {
assert, FormatError, info, isArrayBuffer, isBool, isNum, isSpace, isString,
OPS, shadow, stringToBytes, stringToPDFString, Util, warn
assert, bytesToString, FormatError, info, isArrayBuffer, isBool, isNum,
isSpace, isString, OPS, shadow, stringToBytes, stringToPDFString, Util, warn
} from '../shared/util';
import { Catalog, ObjectLoader, XRef } from './obj';
import { Dict, isDict, isName, isStream, Ref } from './primitives';
@ -337,20 +337,11 @@ const FINGERPRINT_FIRST_BYTES = 1024;
const EMPTY_FINGERPRINT = '\x00\x00\x00\x00\x00\x00\x00' +
'\x00\x00\x00\x00\x00\x00\x00\x00\x00';
function find(stream, needle, limit, backwards) {
const pos = stream.pos;
const end = stream.end;
if (pos + limit > end) {
limit = end - pos;
}
function find(stream, needle, limit, backwards = false) {
assert(limit > 0, 'The "limit" must be a positive integer.');
const strBuf = [];
for (let i = 0; i < limit; ++i) {
strBuf.push(String.fromCharCode(stream.getByte()));
}
const str = strBuf.join('');
const str = bytesToString(stream.peekBytes(limit));
stream.pos = pos;
const index = backwards ? str.lastIndexOf(needle) : str.indexOf(needle);
if (index === -1) {
return false;