Skip to content

Commit

Permalink
Merge pull request #130 from vim-jp/fix-js-len
Browse files Browse the repository at this point in the history
Fix len() for js
  • Loading branch information
mattn authored Jul 20, 2019
2 parents a74da5d + 0f668c9 commit e190e30
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 4 deletions.
7 changes: 6 additions & 1 deletion js/vimlfunc.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ function viml_keys(obj) {

function viml_len(obj) {
if (typeof obj === 'string') {
return encodeURIComponent(obj).replace(/%../g, ' ').length;
var len = 0;
for (var i = 0; i < obj.length; i++) {
var c = obj.charCodeAt(i);
len += c < 128 ? 1 : ((c > 127) && (c < 2048)) ? 2 : 3;
}
return len;
}
return obj.length;
}
Expand Down
7 changes: 6 additions & 1 deletion js/vimlparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ function viml_keys(obj) {

function viml_len(obj) {
if (typeof obj === 'string') {
return encodeURIComponent(obj).replace(/%../g, ' ').length;
var len = 0;
for (var i = 0; i < obj.length; i++) {
var c = obj.charCodeAt(i);
len += c < 128 ? 1 : ((c > 127) && (c < 2048)) ? 2 : 3;
}
return len;
}
return obj.length;
}
Expand Down
6 changes: 5 additions & 1 deletion py/vimlfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ def viml_keys(obj):

def viml_len(obj):
if type(obj) is str:
return len(obj.encode('utf-8'))
if sys.version_info < (3, 0):
b = bytes(obj)
else:
b = bytes(obj, 'utf8')
return len(b)
return len(obj)

def viml_printf(*args):
Expand Down
6 changes: 5 additions & 1 deletion py/vimlparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ def viml_keys(obj):

def viml_len(obj):
if type(obj) is str:
return len(obj.encode('utf-8'))
if sys.version_info < (3, 0):
b = bytes(obj)
else:
b = bytes(obj, 'utf8')
return len(b)
return len(obj)

def viml_printf(*args):
Expand Down
1 change: 1 addition & 0 deletions test/test1.ok
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@
(let %= a 5)
(let ..= a 'foo')
(echo (concat (concat 'foo' 'bar') 'baz'))
(let = a '🐥')
1 change: 1 addition & 0 deletions test/test1.vim
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ let a /= 4
let a %= 5
let a ..= 'foo'
echo ('foo' .. 'bar')..'baz'
let a = '🐥'

0 comments on commit e190e30

Please sign in to comment.