Skip to content

Commit

Permalink
Use a limit, in more places, when splitting strings
Browse files Browse the repository at this point in the history
This should be a *tiny* bit more efficient, since it avoids parsing substrings that we don't care about.

*Please note:* I cannot find an ESLint rule to enforce this automatically.
  • Loading branch information
Snuffleupagus committed Feb 2, 2024
1 parent af4d2fa commit 363dce6
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion extensions/chromium/extension-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ limitations under the License.
}
var scheme = url.slice(0, schemeIndex).toLowerCase();
if (schemes.includes(scheme)) {
url = url.split("#")[0];
url = url.split("#", 1)[0];
if (url.charAt(schemeIndex) === ":") {
url = encodeURIComponent(url);
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3955,7 +3955,7 @@ class PartialEvaluator {

isSerifFont(baseFontName) {
// Simulating descriptor flags attribute
const fontNameWoStyle = baseFontName.split("-")[0];
const fontNameWoStyle = baseFontName.split("-", 1)[0];
return (
fontNameWoStyle in getSerifFonts() || /serif/gi.test(fontNameWoStyle)
);
Expand Down Expand Up @@ -4185,7 +4185,7 @@ class PartialEvaluator {
const metrics = this.getBaseFontMetrics(baseFontName);

// Simulating descriptor flags attribute
const fontNameWoStyle = baseFontName.split("-")[0];
const fontNameWoStyle = baseFontName.split("-", 1)[0];
const flags =
(this.isSerifFont(fontNameWoStyle) ? FontFlags.Serif : 0) |
(metrics.monospace ? FontFlags.FixedPitch : 0) |
Expand Down
4 changes: 2 additions & 2 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ class Font {
// Fallback to checking the font name, in order to improve text-selection,
// since the /Flags-entry is often wrong (fixes issue13845.pdf).
if (!isSerifFont && !properties.isSimulatedFlags) {
const baseName = name.replaceAll(/[,_]/g, "-").split("-")[0],
const baseName = name.replaceAll(/[,_]/g, "-").split("-", 1)[0],
serifFonts = getSerifFonts();
for (const namePart of baseName.split("+")) {
if (serifFonts[namePart]) {
Expand Down Expand Up @@ -1286,7 +1286,7 @@ class Font {
}

amendFallbackToUnicode(properties);
this.loadedName = fontName.split("-")[0];
this.loadedName = fontName.split("-", 1)[0];
}

checkAndRepair(name, font, properties) {
Expand Down
2 changes: 1 addition & 1 deletion test/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ class Driver {
if (!("message" in e)) {
return JSON.stringify(e);
}
return e.message + ("stack" in e ? " at " + e.stack.split("\n")[0] : "");
return e.message + ("stack" in e ? " at " + e.stack.split("\n", 1)[0] : "");
}

_getLastPageNumber(task) {
Expand Down
2 changes: 1 addition & 1 deletion web/generic_scripting.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { getPdfFilenameFromUrl } from "pdfjs-lib";

async function docProperties(pdfDocument) {
const url = "",
baseUrl = url.split("#")[0];
baseUrl = url.split("#", 1)[0];
// eslint-disable-next-line prefer-const
let { info, metadata, contentDispositionFilename, contentLength } =
await pdfDocument.getMetadata();
Expand Down
2 changes: 1 addition & 1 deletion web/pdf_history.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class PDFHistory {

let newUrl;
if (this._updateUrl && destination?.hash) {
const baseUrl = document.location.href.split("#")[0];
const baseUrl = document.location.href.split("#", 1)[0];
// Prevent errors in Firefox.
if (!baseUrl.startsWith("file://")) {
newUrl = `${baseUrl}#${destination.hash}`;
Expand Down

0 comments on commit 363dce6

Please sign in to comment.