From c5f67300e9b36542fcb1b5df517bfa6b9ac811fe Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 10 Feb 2020 09:38:57 +0100 Subject: [PATCH] Rename the `isSpace` helper function to `isWhiteSpace` Trying to enable the ESLint rule `no-shadow`, against the `master` branch, would result in a fair number of errors in the `Glyph` class in `src/core/fonts.js`. Since the glyphs are exposed through the API, we can't very well change the `isSpace` property on `Glyph` instances. Thus the best approach seems, at least to me, to simply rename the `isSpace` helper function to `isWhiteSpace` which shouldn't cause any issues given that it's only used in the `src/core/` folder. --- src/core/core_utils.js | 4 ++-- src/core/document.js | 4 ++-- src/core/fonts.js | 8 ++++++-- src/core/parser.js | 10 +++++----- src/core/ps_parser.js | 4 ++-- src/core/stream.js | 6 +++--- src/core/type1_parser.js | 6 +++--- test/unit/core_utils_spec.js | 18 +++++++++--------- 8 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/core/core_utils.js b/src/core/core_utils.js index 1be5c978a..dd20bf006 100644 --- a/src/core/core_utils.js +++ b/src/core/core_utils.js @@ -161,7 +161,7 @@ function readUint32(data, offset) { } // Checks if ch is one of the following characters: SPACE, TAB, CR or LF. -function isSpace(ch) { +function isWhiteSpace(ch) { return ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a; } @@ -176,5 +176,5 @@ export { readInt8, readUint16, readUint32, - isSpace, + isWhiteSpace, }; diff --git a/src/core/document.js b/src/core/document.js index 0def19020..32ea4740a 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -42,7 +42,7 @@ import { } from "./primitives.js"; import { getInheritableProperty, - isSpace, + isWhiteSpace, MissingDataException, XRefEntryException, XRefParseException, @@ -598,7 +598,7 @@ class PDFDocument { let ch; do { ch = stream.getByte(); - } while (isSpace(ch)); + } while (isWhiteSpace(ch)); let str = ""; while (ch >= /* Space = */ 0x20 && ch <= /* '9' = */ 0x39) { str += String.fromCharCode(ch); diff --git a/src/core/fonts.js b/src/core/fonts.js index 09fafec1c..98e29f3b6 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -58,7 +58,11 @@ import { getUnicodeRangeFor, mapSpecialUnicodeValues, } from "./unicode.js"; -import { isSpace, MissingDataException, readUint32 } from "./core_utils.js"; +import { + isWhiteSpace, + MissingDataException, + readUint32, +} from "./core_utils.js"; import { FontRendererFactory } from "./font_renderer.js"; import { IdentityCMap } from "./cmap.js"; import { Stream } from "./stream.js"; @@ -3413,7 +3417,7 @@ var Type1Font = (function Type1FontClosure() { if (j >= signatureLength) { // `signature` found, skip over whitespace. i += j; - while (i < streamBytesLength && isSpace(streamBytes[i])) { + while (i < streamBytesLength && isWhiteSpace(streamBytes[i])) { i++; } found = true; diff --git a/src/core/parser.js b/src/core/parser.js index 1a64455e1..7f0871daf 100644 --- a/src/core/parser.js +++ b/src/core/parser.js @@ -43,7 +43,7 @@ import { Name, Ref, } from "./primitives.js"; -import { isSpace, MissingDataException } from "./core_utils.js"; +import { isWhiteSpace, MissingDataException } from "./core_utils.js"; import { CCITTFaxStream } from "./ccitt_stream.js"; import { Jbig2Stream } from "./jbig2_stream.js"; import { JpegStream } from "./jpeg_stream.js"; @@ -270,7 +270,7 @@ class Parser { // Ensure that we don't accidentally truncate the inline image, when the // data is immediately followed by the "EI" marker (fixes issue10388.pdf). - if (!isSpace(ch)) { + if (!isWhiteSpace(ch)) { endOffset--; } return stream.pos - endOffset - startPos; @@ -394,7 +394,7 @@ class Parser { ch = stream.peekByte(); // Handle corrupt PDF documents which contains whitespace "inside" of // the EOD marker (fixes issue10614.pdf). - while (isSpace(ch)) { + while (isWhiteSpace(ch)) { stream.skip(); ch = stream.peekByte(); } @@ -640,7 +640,7 @@ class Parser { // Ensure that the byte immediately following the truncated // endstream command is a space, to prevent false positives. const lastByte = stream.peekBytes(end + 1)[end]; - if (!isSpace(lastByte)) { + if (!isWhiteSpace(lastByte)) { break; } info( @@ -886,7 +886,7 @@ class Lexer { if ( divideBy === 10 && sign === 0 && - (isSpace(ch) || ch === /* EOF = */ -1) + (isWhiteSpace(ch) || ch === /* EOF = */ -1) ) { // This is consistent with Adobe Reader (fixes issue9252.pdf). warn("Lexer.getNumber - treating a single decimal point as zero."); diff --git a/src/core/ps_parser.js b/src/core/ps_parser.js index b3e873fcd..34d70e981 100644 --- a/src/core/ps_parser.js +++ b/src/core/ps_parser.js @@ -16,7 +16,7 @@ import { FormatError, shadow } from "../shared/util.js"; import { EOF } from "./primitives.js"; -import { isSpace } from "./core_utils.js"; +import { isWhiteSpace } from "./core_utils.js"; class PostScriptParser { constructor(lexer) { @@ -193,7 +193,7 @@ class PostScriptLexer { } } else if (ch === /* '%' = */ 0x25) { comment = true; - } else if (!isSpace(ch)) { + } else if (!isWhiteSpace(ch)) { break; } ch = this.nextChar(); diff --git a/src/core/stream.js b/src/core/stream.js index b0b213011..072c78cdc 100644 --- a/src/core/stream.js +++ b/src/core/stream.js @@ -21,7 +21,7 @@ import { FormatError, stringToBytes, unreachable } from "../shared/util.js"; import { isDict } from "./primitives.js"; -import { isSpace } from "./core_utils.js"; +import { isWhiteSpace } from "./core_utils.js"; var Stream = (function StreamClosure() { function Stream(arrayBuffer, start, length, dict) { @@ -1001,7 +1001,7 @@ var Ascii85Stream = (function Ascii85StreamClosure() { var str = this.str; var c = str.getByte(); - while (isSpace(c)) { + while (isWhiteSpace(c)) { c = str.getByte(); } @@ -1026,7 +1026,7 @@ var Ascii85Stream = (function Ascii85StreamClosure() { input[0] = c; for (i = 1; i < 5; ++i) { c = str.getByte(); - while (isSpace(c)) { + while (isWhiteSpace(c)) { c = str.getByte(); } diff --git a/src/core/type1_parser.js b/src/core/type1_parser.js index d87c7986d..a8d4dd920 100644 --- a/src/core/type1_parser.js +++ b/src/core/type1_parser.js @@ -14,7 +14,7 @@ */ import { getEncoding } from "./encodings.js"; -import { isSpace } from "./core_utils.js"; +import { isWhiteSpace } from "./core_utils.js"; import { Stream } from "./stream.js"; import { warn } from "../shared/util.js"; @@ -524,7 +524,7 @@ var Type1Parser = (function Type1ParserClosure() { } } else if (ch === /* '%' = */ 0x25) { comment = true; - } else if (!isSpace(ch)) { + } else if (!isWhiteSpace(ch)) { break; } ch = this.nextChar(); @@ -537,7 +537,7 @@ var Type1Parser = (function Type1ParserClosure() { do { token += String.fromCharCode(ch); ch = this.nextChar(); - } while (ch >= 0 && !isSpace(ch) && !isSpecial(ch)); + } while (ch >= 0 && !isWhiteSpace(ch) && !isSpecial(ch)); return token; }, diff --git a/test/unit/core_utils_spec.js b/test/unit/core_utils_spec.js index 9e0590847..3d13f12e8 100644 --- a/test/unit/core_utils_spec.js +++ b/test/unit/core_utils_spec.js @@ -16,7 +16,7 @@ import { Dict, Ref } from "../../src/core/primitives.js"; import { getInheritableProperty, - isSpace, + isWhiteSpace, log2, toRomanNumerals, } from "../../src/core/core_utils.js"; @@ -197,18 +197,18 @@ describe("core_utils", function() { }); }); - describe("isSpace", function() { + describe("isWhiteSpace", function() { it("handles space characters", function() { - expect(isSpace(0x20)).toEqual(true); - expect(isSpace(0x09)).toEqual(true); - expect(isSpace(0x0d)).toEqual(true); - expect(isSpace(0x0a)).toEqual(true); + expect(isWhiteSpace(0x20)).toEqual(true); + expect(isWhiteSpace(0x09)).toEqual(true); + expect(isWhiteSpace(0x0d)).toEqual(true); + expect(isWhiteSpace(0x0a)).toEqual(true); }); it("handles non-space characters", function() { - expect(isSpace(0x0b)).toEqual(false); - expect(isSpace(null)).toEqual(false); - expect(isSpace(undefined)).toEqual(false); + expect(isWhiteSpace(0x0b)).toEqual(false); + expect(isWhiteSpace(null)).toEqual(false); + expect(isWhiteSpace(undefined)).toEqual(false); }); }); });