From 6ca21be3b1f3f48d17b473b6fccf40c72c21416f Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sun, 16 Aug 2015 02:03:33 +1000 Subject: [PATCH] avoid unnecessary hoisting --- index.js | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 8d0a531..86c37bb 100644 --- a/index.js +++ b/index.js @@ -634,30 +634,20 @@ function decodeCodePointsArray (array) { function utf8Slice (buf, start, end) { end = Math.min(buf.length, end) - var firstByte - var secondByte - var thirdByte - var fourthByte - var bytesPerSequence - var tempCodePoint - var codePoint var res = [] - for (var i = start; i < end; i += bytesPerSequence) { - firstByte = buf[i] - codePoint = 0xFFFD - - if (firstByte > 0xEF) { - bytesPerSequence = 4 - } else if (firstByte > 0xDF) { - bytesPerSequence = 3 - } else if (firstByte > 0xBF) { - bytesPerSequence = 2 - } else { - bytesPerSequence = 1 - } + var i = start + while (i < end) { + var firstByte = buf[i] + var codePoint = 0xFFFD + var bytesPerSequence = (firstByte > 0xEF) ? 4 + : (firstByte > 0xDF) ? 3 + : (firstByte > 0xBF) ? 2 + : 1 if (i + bytesPerSequence <= end) { + var secondByte, thirdByte, fourthByte, tempCodePoint + switch (bytesPerSequence) { case 1: if (firstByte < 0x80) { @@ -707,6 +697,7 @@ function utf8Slice (buf, start, end) { } res.push(codePoint) + i += bytesPerSequence } return decodeCodePointsArray(res) -- 2.34.1