Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix len() for js #130

Merged
merged 3 commits into from
Jul 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = '🐥'