]> zoso.dev Git - buffer.git/commitdiff
invert logic to decrease branching depth
authorDaniel Cousens <github@dcousens.com>
Tue, 11 Aug 2015 14:33:19 +0000 (00:33 +1000)
committerDaniel Cousens <github@dcousens.com>
Tue, 11 Aug 2015 14:33:19 +0000 (00:33 +1000)
index.js

index 77d0f470758d94bc262ead59b324c984c4ef3718..c1b04190277279142d42de51e42a961d2eafa31d 100644 (file)
--- a/index.js
+++ b/index.js
@@ -1413,40 +1413,42 @@ function utf8ToBytes (string, units) {
     // is surrogate component
     if (codePoint > 0xD7FF && codePoint < 0xE000) {
       // last char was a lead
-      if (leadSurrogate) {
-        // 2 leads in a row
-        if (codePoint < 0xDC00) {
-          if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
-          leadSurrogate = codePoint
-          continue
-        } else {
-          // valid surrogate pair
-          codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000
-          leadSurrogate = null
-        }
-      } else {
+      if (!leadSurrogate) {
         // no lead yet
-
         if (codePoint > 0xDBFF) {
           // unexpected trail
           if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
           continue
+
         } else if (i + 1 === length) {
           // unpaired lead
           if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
           continue
-        } else {
-          // valid lead
-          leadSurrogate = codePoint
-          continue
         }
+
+        // valid lead
+        leadSurrogate = codePoint
+
+        continue
+      }
+
+      // 2 leads in a row
+      if (codePoint < 0xDC00) {
+        if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
+        leadSurrogate = codePoint
+        continue
       }
+
+      // valid surrogate pair
+      codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000
+
     } else if (leadSurrogate) {
       // valid bmp char, but last char was a lead
       if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
-      leadSurrogate = null
     }
 
+    leadSurrogate = null
+
     // encode utf8
     if (codePoint < 0x80) {
       if ((units -= 1) < 0) break