public
Description: Travis Vachon's clone of the Cosmo Subversion tree
Homepage: http://chandlerproject.org
Clone URL: git://github.com/travis/cosmo.git
Drastically simplify utf8 encoding function based on blog post here:
http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.
html

Change passes unit tests, so seems to work well.

git-svn-id: svn+ssh://svn.osafoundation.org/svn/server/cosmo/trunk@7295 
5fd0b11a-f2f7-0310-948e-c8f80ebed640
travis (author)
Fri Aug 01 12:50:42 -0700 2008
commit  09f46b256cadf3cd1c031b0fc105cfd3dfe7fa9e
tree    ef16b35c54b8c7e34aed7af311934e18f71bf3cc
parent  82ed58da89fb016e4ee418262cd98947a1e97cbc
...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
 
 
 
 
 
 
 
 
 
 
...
91
92
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
95
96
97
98
99
100
101
102
103
0
@@ -91,50 +91,13 @@ cosmo.util.encoding.fromBase64 = function(string){
0
     } while (i < string.length);
0
 
0
     return cosmo.util.encoding._utf8_decode(out);
0
-}
0
-/* UTF-8 functions grabbed from http://www.webtoolkit.info/javascript-base64.html */
0
-cosmo.util.encoding._utf8_decode = function (utftext) {
0
- var string = "";
0
- var i = 0;
0
- var c = c1 = c2 = 0;
0
- while ( i < utftext.length ) {
0
- c = utftext.charCodeAt(i);
0
- if (c < 128) {
0
- string += String.fromCharCode(c);
0
- i++;
0
- }
0
- else if((c > 191) && (c < 224)) {
0
- c2 = utftext.charCodeAt(i+1);
0
- string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
0
- i += 2;
0
- }
0
- else {
0
- c2 = utftext.charCodeAt(i+1);
0
- c3 = utftext.charCodeAt(i+2);
0
- string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
0
- i += 3;
0
- }
0
- }
0
- return string;
0
-}
0
-
0
-cosmo.util.encoding._utf8_encode = function (string) {
0
- string = string.replace(/\r\n/g,"\n");
0
- var utftext = "";
0
- for (var n = 0; n < string.length; n++) {
0
- var c = string.charCodeAt(n);
0
- if (c < 128) {
0
- utftext += String.fromCharCode(c);
0
- }
0
- else if((c > 127) && (c < 2048)) {
0
- utftext += String.fromCharCode((c >> 6) | 192);
0
- utftext += String.fromCharCode((c & 63) | 128);
0
- }
0
- else {
0
- utftext += String.fromCharCode((c >> 12) | 224);
0
- utftext += String.fromCharCode(((c >> 6) & 63) | 128);
0
- utftext += String.fromCharCode((c & 63) | 128);
0
- }
0
- }
0
- return utftext;
0
-}
0
+};
0
+
0
+/* UTF-8 conversion functions per http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html */
0
+cosmo.util.encoding._utf8_encode = function(s) {
0
+ return unescape(encodeURIComponent(s));
0
+};
0
+
0
+cosmo.util.encoding._utf8_decode = function(s) {
0
+ return decodeURIComponent(escape(s));
0
+};

Comments

    No one has commented yet.