Skip to content
This repository was archived by the owner on Dec 9, 2018. It is now read-only.

Commit 8a8cb26

Browse files
committed
use builtin emscripten UTF8 functions
1 parent b57c90e commit 8a8cb26

File tree

1 file changed

+9
-107
lines changed

1 file changed

+9
-107
lines changed

web/vim_lib.js

Lines changed: 9 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -72,38 +72,6 @@ var LibraryVIM = {
7272

7373
dropbox: null,
7474

75-
// from https://github.com/s-macke/jor1k/blob/master/js/lib/utf8.js
76-
// LICENSE: https://github.com/s-macke/jor1k/blob/master/LICENSE.md
77-
UnicodeToUTF8Stream: function(key) {
78-
key = key|0;
79-
if (key < 0x80) {
80-
return [key];
81-
}
82-
if (key <= 0x7FF) {
83-
return [
84-
(key >> 6) | 0xC0,
85-
(key & 0x3F) | 0x80
86-
];
87-
}
88-
if (key <= 0xFFFF) {
89-
return [
90-
(key >> 12) | 0xE0,
91-
((key >> 6) & 0x3F) | 0x80,
92-
(key & 0x3F) | 0x80
93-
];
94-
}
95-
if (key <= 0x10FFFF) {
96-
return [
97-
(key >> 18) | 0xF0,
98-
((key >> 12) & 0x3F) | 0x80,
99-
((key >> 6) & 0x3F) | 0x80,
100-
(key & 0x3F) | 0x80
101-
];
102-
}
103-
//message.Debug("Error in utf-8 encoding: Invalid key");
104-
return [];
105-
},
106-
10775
// functions that are not exposed to C
10876
handle_key: function(charCode, keyCode, e) {//VIMJS_FOLD_START
10977
// macros defined in keymap.h
@@ -124,12 +92,14 @@ var LibraryVIM = {
12492
}
12593

12694
if(!handled) {
127-
var chars = vimjs.UnicodeToUTF8Stream(charCode || keyCode);
128-
if (chars.length == 1) {
95+
var MAX_UTF8_BYTES = 6;
96+
var chars = new Uint8Array(MAX_UTF8_BYTES + 1); // null-terminated
97+
var charLen = stringToUTF8Array(String.fromCharCode(charCode), chars, 0, MAX_UTF8_BYTES);
98+
if (charLen == 1) {
12999
vimjs.gui_web_handle_key(chars[0], modifiers, 0, 0);
130100
} else {
131101
// no modifers for UTF-8, should be handled in chars already
132-
for (var i = 0; i < chars.length; i++) {
102+
for (var i = 0; i < charLen; i++) {
133103
vimjs.gui_web_handle_key(chars[i], 0, 0, 0);
134104
}
135105
}
@@ -820,81 +790,13 @@ var LibraryVIM = {
820790

821791
vimjs_draw_string__deps: ['vimjs_clear_block'],
822792
vimjs_draw_string: function(row, col, s_ptr, len, flags) {
823-
// from https://github.com/s-macke/jor1k/blob/master/js/lib/utf8.js
824-
// LICENSE: https://github.com/s-macke/jor1k/blob/master/LICENSE.md
825-
var UTF8StreamToUnicode = function() {
826-
this.stream = new Uint8Array(5);
827-
this.ofs = 0;
828-
829-
this.Put = function(key) {
830-
this.stream[this.ofs] = key;
831-
this.ofs++;
832-
switch(this.ofs) {
833-
case 1:
834-
if (this.stream[0] < 0x80) {
835-
this.ofs = 0;
836-
return this.stream[0];
837-
}
838-
break;
839-
840-
case 2:
841-
if ((this.stream[0]&0xE0) == 0xC0)
842-
if ((this.stream[1]&0xC0) == 0x80) {
843-
this.ofs = 0;
844-
return ((this.stream[0]&0x1F)<<6) |
845-
((this.stream[1]&0x3F)<<0);
846-
}
847-
break;
848-
849-
case 3:
850-
if ((this.stream[0]&0xF0) == 0xE0)
851-
if ((this.stream[1]&0xC0) == 0x80)
852-
if ((this.stream[2]&0xC0) == 0x80) {
853-
this.ofs = 0;
854-
return ((this.stream[0]&0xF ) << 12) |
855-
((this.stream[1]&0x3F) << 6) |
856-
((this.stream[2]&0x3F) << 0);
857-
}
858-
break;
859-
860-
case 4:
861-
if ((this.stream[0]&0xF8) == 0xF0)
862-
if ((this.stream[1]&0xC0) == 0x80)
863-
if ((this.stream[2]&0xC0) == 0x80)
864-
if ((this.stream[3]&0xC0) == 0x80) {
865-
this.ofs = 0;
866-
return ((this.stream[0]&0x7 ) << 18) |
867-
((this.stream[1]&0x3F) << 12) |
868-
((this.stream[2]&0x3F) << 6) |
869-
((this.stream[3]&0x3F) << 0);
870-
}
871-
this.ofs = 0;
872-
return -1; //obviously illegal character, so reset
873-
break;
874-
875-
default:
876-
this.ofs = 0;
877-
return -1;
878-
break;
879-
}
880-
return -1;
881-
}
882-
};
883-
884-
// Pointer_stringify includes UTF-8 decoding,
885-
// but is buggy somehow, so we decode here instead.
886-
// TODO: Make a test case for Pointer_stringify and report bug to Emscripten.
887-
// var s = Pointer_stringify(s_ptr, len);
888-
var utf8converter = new UTF8StreamToUnicode();
889-
var news = "";
793+
var byteArray = [];
890794
for (var i = 0; i < len; i++) {
891795
c = getValue(s_ptr + i, 'i8', true);
892-
c = utf8converter.Put(c);
893-
if (c != -1) {
894-
news += String.fromCharCode(c);
895-
}
796+
byteArray.push(c);
896797
}
897-
var s = news;
798+
byteArray.push(0);
799+
var s = UTF8ArrayToString(byteArray, 0);
898800
len = s.length;
899801

900802
// TODO: use macros for flag constants

0 commit comments

Comments
 (0)