Skip to content

Instantly share code, notes, and snippets.

@youpy
Created April 21, 2009 12:22
Show Gist options
  • Save youpy/99107 to your computer and use it in GitHub Desktop.
Save youpy/99107 to your computer and use it in GitHub Desktop.
/* appjet:version 0.1 */
print(H1(appjet.appName));
/* appjet:library */
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;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment