From 696efb9fb9de91ecf34a5393ffd080d5a1f32c77 Mon Sep 17 00:00:00 2001 From: Joe Ferner Date: Sat, 14 Nov 2026 10:32:40 -0500 Subject: [PATCH] refactor: remove identical if/else branches when reading length ints Fixes #40 Co-Authored-By: Claude Opus 5.5 --- bplistParser.ts | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/bplistParser.ts b/bplistParser.ts index 0734de0..20da086 100644 --- a/bplistParser.ts +++ b/bplistParser.ts @@ -257,11 +257,7 @@ export function parseBuffer(buffer: Buffer): [T] { const intInfo = int_type & 0x0F; const intLength = Math.pow(2, intInfo); dataoffset = 2 + intLength; - if (intLength < 3) { - length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); - } else { - length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); - } + length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); } if (length < maxObjectSize) { return buffer.slice(offset + dataoffset, offset + dataoffset + length); @@ -283,11 +279,7 @@ export function parseBuffer(buffer: Buffer): [T] { const intInfo = int_type & 0x0F; const intLength = Math.pow(2, intInfo); stroffset = 2 + intLength; - if (intLength < 3) { - length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); - } else { - length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); - } + length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); } // length is String length -> to get byte length multiply by 2, as 1 character takes 2 bytes in UTF-16 length *= (utf16Flag ? 2 : 1); @@ -314,11 +306,7 @@ export function parseBuffer(buffer: Buffer): [T] { const intInfo = int_type & 0x0F; const intLength = Math.pow(2, intInfo); arrayoffset = 2 + intLength; - if (intLength < 3) { - length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); - } else { - length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); - } + length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); } if (length * objectRefSize > maxObjectSize) { throw new Error("Too little heap space available!"); @@ -343,11 +331,7 @@ export function parseBuffer(buffer: Buffer): [T] { const intInfo = int_type & 0x0F; const intLength = Math.pow(2, intInfo); dictoffset = 2 + intLength; - if (intLength < 3) { - length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); - } else { - length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); - } + length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); } if (length * 2 * objectRefSize > maxObjectSize) { throw new Error("Too little heap space available!");