203 return $utf8; |
204 return $utf8; |
204 |
205 |
205 case 2: |
206 case 2: |
206 // return a UTF-16 character from a 2-byte UTF-8 char |
207 // return a UTF-16 character from a 2-byte UTF-8 char |
207 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
208 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
208 return chr(0x07 & (ord($utf8{0}) >> 2)) |
209 return chr(0x07 & (ord($utf8[0]) >> 2)) |
209 . chr((0xC0 & (ord($utf8{0}) << 6)) |
210 . chr((0xC0 & (ord($utf8[0]) << 6)) |
210 | (0x3F & ord($utf8{1}))); |
211 | (0x3F & ord($utf8[1]))); |
211 |
212 |
212 case 3: |
213 case 3: |
213 // return a UTF-16 character from a 3-byte UTF-8 char |
214 // return a UTF-16 character from a 3-byte UTF-8 char |
214 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
215 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
215 return chr((0xF0 & (ord($utf8{0}) << 4)) |
216 return chr((0xF0 & (ord($utf8[0]) << 4)) |
216 | (0x0F & (ord($utf8{1}) >> 2))) |
217 | (0x0F & (ord($utf8[1]) >> 2))) |
217 . chr((0xC0 & (ord($utf8{1}) << 6)) |
218 . chr((0xC0 & (ord($utf8[1]) << 6)) |
218 | (0x7F & ord($utf8{2}))); |
219 | (0x7F & ord($utf8[2]))); |
219 } |
220 } |
220 |
221 |
221 // ignoring UTF-32 for now, sorry |
222 // ignoring UTF-32 for now, sorry |
222 return ''; |
223 return ''; |
223 } |
224 } |
313 |
314 |
314 case $ord_var_c == 0x22: |
315 case $ord_var_c == 0x22: |
315 case $ord_var_c == 0x2F: |
316 case $ord_var_c == 0x2F: |
316 case $ord_var_c == 0x5C: |
317 case $ord_var_c == 0x5C: |
317 // double quote, slash, slosh |
318 // double quote, slash, slosh |
318 $ascii .= '\\'.$var{$c}; |
319 $ascii .= '\\'.$var[$c]; |
319 break; |
320 break; |
320 |
321 |
321 case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)): |
322 case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)): |
322 // characters U-00000000 - U-0000007F (same as ASCII) |
323 // characters U-00000000 - U-0000007F (same as ASCII) |
323 $ascii .= $var{$c}; |
324 $ascii .= $var[$c]; |
324 break; |
325 break; |
325 |
326 |
326 case (($ord_var_c & 0xE0) == 0xC0): |
327 case (($ord_var_c & 0xE0) == 0xC0): |
327 // characters U-00000080 - U-000007FF, mask 110XXXXX |
328 // characters U-00000080 - U-000007FF, mask 110XXXXX |
328 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
329 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
604 case $substr_chrs_c_2 == '\\\'': |
605 case $substr_chrs_c_2 == '\\\'': |
605 case $substr_chrs_c_2 == '\\\\': |
606 case $substr_chrs_c_2 == '\\\\': |
606 case $substr_chrs_c_2 == '\\/': |
607 case $substr_chrs_c_2 == '\\/': |
607 if (($delim == '"' && $substr_chrs_c_2 != '\\\'') || |
608 if (($delim == '"' && $substr_chrs_c_2 != '\\\'') || |
608 ($delim == "'" && $substr_chrs_c_2 != '\\"')) { |
609 ($delim == "'" && $substr_chrs_c_2 != '\\"')) { |
609 $utf8 .= $chrs{++$c}; |
610 $utf8 .= $chrs[++$c]; |
610 } |
611 } |
611 break; |
612 break; |
612 |
613 |
613 case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)): |
614 case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)): |
614 // single, escaped unicode character |
615 // single, escaped unicode character |
703 for ($c = 0; $c <= $strlen_chrs; ++$c) { |
704 for ($c = 0; $c <= $strlen_chrs; ++$c) { |
704 |
705 |
705 $top = end($stk); |
706 $top = end($stk); |
706 $substr_chrs_c_2 = substr($chrs, $c, 2); |
707 $substr_chrs_c_2 = substr($chrs, $c, 2); |
707 |
708 |
708 if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) { |
709 if (($c == $strlen_chrs) || (($chrs[$c] == ',') && ($top['what'] == SERVICES_JSON_SLICE))) { |
709 // found a comma that is not inside a string, array, etc., |
710 // found a comma that is not inside a string, array, etc., |
710 // OR we've reached the end of the character list |
711 // OR we've reached the end of the character list |
711 $slice = substr($chrs, $top['where'], ($c - $top['where'])); |
712 $slice = substr($chrs, $top['where'], ($c - $top['where'])); |
712 array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false)); |
713 array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false)); |
713 //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
714 //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
745 } |
746 } |
746 } |
747 } |
747 |
748 |
748 } |
749 } |
749 |
750 |
750 } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) { |
751 } elseif ((($chrs[$c] == '"') || ($chrs[$c] == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) { |
751 // found a quote, and we are not inside a string |
752 // found a quote, and we are not inside a string |
752 array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c})); |
753 array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs[$c])); |
753 //print("Found start of string at {$c}\n"); |
754 //print("Found start of string at {$c}\n"); |
754 |
755 |
755 } elseif (($chrs{$c} == $top['delim']) && |
756 } elseif (($chrs[$c] == $top['delim']) && |
756 ($top['what'] == SERVICES_JSON_IN_STR) && |
757 ($top['what'] == SERVICES_JSON_IN_STR) && |
757 ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) { |
758 ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) { |
758 // found a quote, we're in a string, and it's not escaped |
759 // found a quote, we're in a string, and it's not escaped |
759 // we know that it's not escaped becase there is _not_ an |
760 // we know that it's not escaped becase there is _not_ an |
760 // odd number of backslashes at the end of the string so far |
761 // odd number of backslashes at the end of the string so far |
761 array_pop($stk); |
762 array_pop($stk); |
762 //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n"); |
763 //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n"); |
763 |
764 |
764 } elseif (($chrs{$c} == '[') && |
765 } elseif (($chrs[$c] == '[') && |
765 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
766 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
766 // found a left-bracket, and we are in an array, object, or slice |
767 // found a left-bracket, and we are in an array, object, or slice |
767 array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false)); |
768 array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false)); |
768 //print("Found start of array at {$c}\n"); |
769 //print("Found start of array at {$c}\n"); |
769 |
770 |
770 } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) { |
771 } elseif (($chrs[$c] == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) { |
771 // found a right-bracket, and we're in an array |
772 // found a right-bracket, and we're in an array |
772 array_pop($stk); |
773 array_pop($stk); |
773 //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
774 //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
774 |
775 |
775 } elseif (($chrs{$c} == '{') && |
776 } elseif (($chrs[$c] == '{') && |
776 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
777 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
777 // found a left-brace, and we are in an array, object, or slice |
778 // found a left-brace, and we are in an array, object, or slice |
778 array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false)); |
779 array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false)); |
779 //print("Found start of object at {$c}\n"); |
780 //print("Found start of object at {$c}\n"); |
780 |
781 |
781 } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) { |
782 } elseif (($chrs[$c] == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) { |
782 // found a right-brace, and we're in an object |
783 // found a right-brace, and we're in an object |
783 array_pop($stk); |
784 array_pop($stk); |
784 //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
785 //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
785 |
786 |
786 } elseif (($substr_chrs_c_2 == '/*') && |
787 } elseif (($substr_chrs_c_2 == '/*') && |