-
-
Save ionurboz/dad33936f10089c5725b0cff3045c4d1 to your computer and use it in GitHub Desktop.
A way to manipulate UTF-8 strings in jQuery.
This file contains 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
;(function($, undefined) { | |
$.extend({ | |
toUTF8 : function(text) { | |
text = text.replace(/\r\n/g,"\n"); | |
var output = []; | |
for (var n = 0 ; n < text.length ; n++) { | |
var c = text.charCodeAt(n); | |
if (c < 128) { | |
output.push(String.fromCharCode(c)); | |
} else if ((c > 127) && (c < 2048)) { | |
output.push(String.fromCharCode((c >> 6) | 192)); | |
output.push(String.fromCharCode((c & 63) | 128)); | |
} else { | |
output.push(String.fromCharCode((c >> 12) | 224)); | |
output.push(String.fromCharCode(((c >> 6) & 63) | 128)); | |
output.push(String.fromCharCode((c & 63) | 128)); | |
} | |
} | |
return output.join(''); | |
}, | |
fromUTF8 : function(text) { | |
var output = [], | |
i = c = c1 = c2 = 0; | |
while (i < text.length) { | |
c = text.charCodeAt(i); | |
if (c < 128) { | |
output.push(String.fromCharCode(c)); | |
i++; | |
} else if ((c > 191) && (c < 224)) { | |
c2 = text.charCodeAt(i + 1); | |
output.push(String.fromCharCode(((c & 31) << 6) | (c2 & 63))); | |
i += 2; | |
} else { | |
c2 = text.charCodeAt(i + 1); | |
c3 = text.charCodeAt(i + 2); | |
output.push(String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63))); | |
i += 3; | |
} | |
} | |
return output.join(''); | |
} | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment