Skip to content

Commit 8f4c016

Browse files
Merge pull request #19692 from Snuffleupagus/writer-improve
Improve the implementation in `src/core/writer.js` a little bit
2 parents a229914 + 50eb97a commit 8f4c016

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

src/core/writer.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,26 +168,28 @@ function writeInt(number, size, offset, buffer) {
168168
}
169169

170170
function writeString(string, offset, buffer) {
171-
for (let i = 0, len = string.length; i < len; i++) {
171+
const ii = string.length;
172+
for (let i = 0; i < ii; i++) {
172173
buffer[offset + i] = string.charCodeAt(i) & 0xff;
173174
}
175+
return offset + ii;
174176
}
175177

176178
function computeMD5(filesize, xrefInfo) {
177179
const time = Math.floor(Date.now() / 1000);
178180
const filename = xrefInfo.filename || "";
179-
const md5Buffer = [time.toString(), filename, filesize.toString()];
180-
let md5BufferLen = md5Buffer.reduce((a, str) => a + str.length, 0);
181-
for (const value of Object.values(xrefInfo.info)) {
182-
md5Buffer.push(value);
183-
md5BufferLen += value.length;
184-
}
181+
const md5Buffer = [
182+
time.toString(),
183+
filename,
184+
filesize.toString(),
185+
...Object.values(xrefInfo.info),
186+
];
187+
const md5BufferLen = md5Buffer.reduce((a, str) => a + str.length, 0);
185188

186189
const array = new Uint8Array(md5BufferLen);
187190
let offset = 0;
188191
for (const str of md5Buffer) {
189-
writeString(str, offset, array);
190-
offset += str.length;
192+
offset = writeString(str, offset, array);
191193
}
192194
return bytesToString(calculateMD5(array, 0, array.length));
193195
}
@@ -477,8 +479,7 @@ async function incrementalUpdate({
477479

478480
// New data
479481
for (const str of buffer) {
480-
writeString(str, offset, array);
481-
offset += str.length;
482+
offset = writeString(str, offset, array);
482483
}
483484

484485
return array;

0 commit comments

Comments
 (0)