includes/json2.php
changeset 880 218b6d4de908
parent 334 c72b545f1304
child 890 b624c55ab771
--- a/includes/json2.php	Sat Mar 21 19:11:30 2009 -0400
+++ b/includes/json2.php	Sun Mar 22 00:44:09 2009 -0400
@@ -773,6 +773,9 @@
                             case '\'' :
                                 $result .= '\'';
                                 break;
+                            case 'u':
+                              $result .= self::decode_unicode_byte(substr($str, $i + 1, 4));
+                              break;
                             default:
                                 throw new Zend_Json_Exception("Illegal escape "
                                     .  "sequence '" . $chr . "'");
@@ -904,6 +907,41 @@
 
         return($this->_token);
     }
+    
+    /**
+     * Handle a Unicode byte; local to Enano.
+     * @param string 4 character byte sequence
+     * @return string
+     */
+    
+    protected function decode_unicode_byte($byte)
+    {
+      if ( strlen($byte) != 4 )
+        throw new Zend_Json_Exception("Invalid Unicode sequence \\u$byte");
+        
+      $value = hexdec($byte);
+
+      if ($value < 0x0080)
+      {
+        // 1 byte: 0xxxxxxx
+        $character = chr($value);
+      }
+      else if ($value < 0x0800)
+      {
+        // 2 bytes: 110xxxxx 10xxxxxx
+        $character =
+            chr((($value & 0x07c0) >> 6) | 0xc0)
+          . chr(($value & 0x3f) | 0x80);
+      }
+      else
+      {
+        // 3 bytes: 1110xxxx 10xxxxxx 10xxxxxx
+        $character =
+            chr((($value & 0xf000) >> 12) | 0xe0)
+          . chr((($value & 0x0fc0) >> 6) | 0x80)
+          . chr(($value & 0x3f) | 0x80);
+      }
+    }
 }
 
 /**