Permalink
Browse files

Fix issue 257 (#310)

* add failing test for #257

* fix #257

* fix test
  • Loading branch information...
1 parent b0636ab commit 61c2211b1d6dee2b47778f96916c43f6c8630d72 @philippsimon philippsimon committed with Nov 1, 2016
Showing with 25 additions and 33 deletions.
  1. +19 −33 src/php/var/unserialize.js
  2. +6 −0 test/languages/php/var/test-unserialize.js
@@ -11,54 +11,40 @@ module.exports = function unserialize (data) {
// bugfixed by: dptr1988
// bugfixed by: Kevin van Zonneveld (http://kvz.io)
// bugfixed by: Brett Zamir (http://brett-zamir.me)
+ // bugfixed by: philippsimon (https://github.com/philippsimon/)
// revised by: d3x
// input by: Brett Zamir (http://brett-zamir.me)
// input by: Martin (http://www.erlenwiese.de/)
// input by: kilops
// input by: Jaroslaw Czarniak
+ // input by: lovasoa (https://github.com/lovasoa/)
// note 1: We feel the main purpose of this function should be
// note 1: to ease the transport of data between php & js
// note 1: Aiming for PHP-compatibility, we have to translate objects to arrays
// example 1: unserialize('a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}')
// returns 1: ['Kevin', 'van', 'Zonneveld']
// example 2: unserialize('a:2:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";}')
// returns 2: {firstName: 'Kevin', midName: 'van'}
+ // example 3: unserialize('a:3:{s:2:"ü";s:2:"ü";s:3:"四";s:3:"四";s:4:"𠜎";s:4:"𠜎";}')
+ // returns 3: {'ü': 'ü', '四': '四', '𠜎': '𠜎'}
var $global = (typeof window !== 'undefined' ? window : global)
- var utf8Overhead = function (chr) {
- // http://locutus.io/php/unserialize:571#comment_95906
- var code = chr.charCodeAt(0)
- var zeroCodes = [
- 338,
- 339,
- 352,
- 353,
- 376,
- 402,
- 8211,
- 8212,
- 8216,
- 8217,
- 8218,
- 8220,
- 8221,
- 8222,
- 8224,
- 8225,
- 8226,
- 8230,
- 8240,
- 8364,
- 8482
- ]
- if (code < 0x0080 || code >= 0x00A0 && code <= 0x00FF || zeroCodes.indexOf(code) !== -1) {
- return 0
- }
- if (code < 0x0800) {
- return 1
+ var utf8Overhead = function (str) {
+ var s = str.length
+ for (var i = str.length - 1; i >= 0; i--) {
+ var code = str.charCodeAt(i)
+ if (code > 0x7f && code <= 0x7ff) {
+ s++
+ } else if (code > 0x7ff && code <= 0xffff) {
+ s += 2
+ }
+ // trail surrogate
+ if (code >= 0xDC00 && code <= 0xDFFF) {
+ i--
+ }
}
- return 2
+ return s - 1
}
var error = function (type,
msg, filename, line) {
@@ -90,7 +76,7 @@ module.exports = function unserialize (data) {
}
return [buf.length, buf.join('')]
}
- var _unserialize = function (data, offset) {
+ function _unserialize (data, offset) {
var dtype
var dataoffset
var keyandchrs
@@ -19,4 +19,10 @@ describe('src/php/var/unserialize.js (tested in test/languages/php/var/test-unse
expect(result).to.deep.equal(expected)
done()
})
+ it('should pass example 3', function (done) {
+ var expected = {'ü': 'ü', '': '', '𠜎': '𠜎'}
+ var result = unserialize('a:3:{s:2:"ü";s:2:"ü";s:3:"四";s:3:"四";s:4:"𠜎";s:4:"𠜎";}')
+ expect(result).to.deep.equal(expected)
+ done()
+ })
})

0 comments on commit 61c2211

Please sign in to comment.