Created
February 1, 2009 17:17
-
-
Save youpy/55910 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| String.prototype.fix = function() { | |
| var newString = []; | |
| var x, y, z, c; | |
| for(var i = 0; i < this.length; i ++) { | |
| c = this.charCodeAt(i); | |
| if((c & 0xe0) == 0xe0) { | |
| if((x = c ^ 0xe0) > 0xf) { | |
| newString.push(String.fromCharCode(c)); | |
| } else { | |
| y = this._fixFollowingByte(this.charCodeAt(++i)); | |
| z = this._fixFollowingByte(this.charCodeAt(++i)); | |
| newString.push(String.fromCharCode((x << 12) + (y << 6) + z)); | |
| } | |
| } else if((c & 0xc0) == 0xc0) { | |
| if((x = c ^ 0xc0) > 0x1f) { | |
| newString.push(String.fromCharCode(c)); | |
| } else { | |
| y = this._fixFollowingByte(this.charCodeAt(++i)); | |
| newString.push(String.fromCharCode((x << 6) + y)); | |
| } | |
| } else { | |
| newString.push(String.fromCharCode(c)); | |
| } | |
| } | |
| return newString.join(''); | |
| }; | |
| String.prototype._fixFollowingByte = function(c) { | |
| if(c & 0x80) { | |
| return c ^ 0x80; | |
| } else { | |
| return c; | |
| } | |
| }; | |
| fixObj = function(obj) { | |
| var newObj = {}; | |
| for(var i in obj) { | |
| if(typeof obj[i] == 'string') { | |
| newObj[i] = obj[i].fix(); | |
| } else if(typeof obj[i] == 'object') { | |
| newObj[i] = fixObj(obj[i]); | |
| } else { | |
| newObj[i] = obj[i]; | |
| } | |
| } | |
| return newObj; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| utils.include('fixYqlJson.js'); | |
| function testFix() { | |
| assertEqual(2, "人ç\u0094\u009f".fix().length); | |
| assertEqual("人生", "人ç\u0094\u009f".fix()); | |
| assertEqual("人生狂", "人ç\u0094\u009fç\u008b\u0082".fix()); | |
| assertEqual(" abc", " abc".fix()); | |
| assertEqual("人生狂わす、ニクいやつ\n | LIQUIDROOM", "人ç\u0094\u009fç\u008b\u0082ã\u0082\u008fã\u0081\u0099ã\u0080\u0081ã\u0083\u008bã\u0082¯ã\u0081\u0084ã\u0082\u0084ã\u0081¤\n | LIQUIDROOM".fix()); | |
| assertEqual("人生", "人生".fix()); | |
| assertEqual("ー", "ー".fix()); | |
| assertEqual("オークション", "オークション".fix()); | |
| assertEqual("ページの先頭に戻る", "ã\u0083\u009aã\u0083¼ã\u0082¸ã\u0081®å\u0085\u0088é \u00adã\u0081«æ\u0088»ã\u0082\u008b".fix()); | |
| assertEqual("ページの先頭に戻る", "ãã¼ã¸ã®å é ã«æ»ã".fix()); | |
| } | |
| function testFixJSON() { | |
| var newObj = fixObj({ | |
| foo: "人ç\u0094\u009f", | |
| bar: 1 | |
| }); | |
| assertEqual("人生", newObj.foo); | |
| assertEqual(1, newObj.bar); | |
| } | |
| function testFixComplexJSON() { | |
| var newObj = fixObj({ | |
| foo: "aaa", | |
| bar: { | |
| baz: "人ç\u0094\u009f", | |
| xxx: 2 | |
| } | |
| }); | |
| assertEqual("aaa", newObj.foo); | |
| assertEqual("人生", newObj.bar.baz); | |
| assertEqual(2, newObj.bar.xxx); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment