Skip to content

Commit eca44f9

Browse files
committed
Fix len() for js
Fixes #129
1 parent a74da5d commit eca44f9

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

js/vimlfunc.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ function viml_keys(obj) {
155155

156156
function viml_len(obj) {
157157
if (typeof obj === 'string') {
158-
return encodeURIComponent(obj).replace(/%../g, ' ').length;
158+
var len = 0;
159+
for (var i = 0; i < obj.length; i++) {
160+
var c = obj.charCodeAt(i);
161+
len += c < 128 ? 1 : ((c > 127) && (c < 2048)) ? 2 : 3;
162+
}
163+
return len;
159164
}
160165
return obj.length;
161166
}

js/vimlparser.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ function viml_keys(obj) {
155155

156156
function viml_len(obj) {
157157
if (typeof obj === 'string') {
158-
return encodeURIComponent(obj).replace(/%../g, ' ').length;
158+
var len = 0;
159+
for (var i = 0; i < obj.length; i++) {
160+
var c = obj.charCodeAt(i);
161+
len += c < 128 ? 1 : ((c > 127) && (c < 2048)) ? 2 : 3;
162+
}
163+
return len;
159164
}
160165
return obj.length;
161166
}

test/test1.ok

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@
5353
(let %= a 5)
5454
(let ..= a 'foo')
5555
(echo (concat (concat 'foo' 'bar') 'baz'))
56+
(let = a '🐥')

test/test1.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ let a /= 4
5858
let a %= 5
5959
let a ..= 'foo'
6060
echo ('foo' .. 'bar')..'baz'
61+
let a = '🐥'

0 commit comments

Comments
 (0)