Skip to content
Browse files

fix issue with encoding certain unicode codepoints

  • Loading branch information...
1 parent ad5d051 commit 38af479dd45c83081a80d5049b8ee517c9ca492f @chrisdickinson committed
Showing with 17 additions and 4 deletions.
  1. +15 −2 test/from.js
  2. +2 −2 typedarray/from.js
View
17 test/from.js
@@ -13,10 +13,10 @@ test('from array works', function(assert) {
})
test('from utf8 works as expected', function(assert) {
- var buf = binary.from('ƒello 淾淾淾 hello world 淾淾 yep ƒuu 淾 \ud83d\ude04', 'utf8')
+ var buf = binary.from('\ud835\udc00 ƒello 淾淾淾 hello world 淾淾 yep ƒuu 淾 \ud83d\ude04', 'utf8')
, expect
- expect = [198,146,101,108,108,111,32,230,183,190,230,183,190,230,183,190,32,104,101,108,108,111,32,119,111,114,108,100,32,230,183,190,230,183,190,32,121,101,112,32,198,146,117,117,32,230,183,190,32,0xF0,0x9F,0x98,0x84]
+ expect = [0xf0, 0x9d, 0x90, 0x80, 0x20, 198,146,101,108,108,111,32,230,183,190,230,183,190,230,183,190,32,104,101,108,108,111,32,119,111,114,108,100,32,230,183,190,230,183,190,32,121,101,112,32,198,146,117,117,32,230,183,190,32,0xF0,0x9F,0x98,0x84]
assert.equal(buf.length, expect.length)
for(var i = 0, len = buf.length; i < len; ++i) {
@@ -26,6 +26,19 @@ test('from utf8 works as expected', function(assert) {
assert.end()
})
+test('from utf8 works on odd unicode codepoints', function(assert) {
+ var string = ''
+
+ var expect = [0xe5, 0xa5, 0xbd]
+ , buf = binary.from(string)
+
+ for(var i = 0, len = buf.length; i < len; ++i) {
+ assert.equal(binary.readUInt8(buf, i), expect[i])
+ }
+
+ assert.end()
+})
+
test('from hex works as expected', function(assert) {
var buf = binary.from('68656c6c6f20776f726c64c692000a0809', 'hex')
, expect
View
4 typedarray/from.js
@@ -91,6 +91,7 @@ function codepoint_to_bytes(arr, code) {
}
if(pos) {
+ _byte |= +!!(code & 1) << (7 - pos)
arr[arr.length] = _byte
}
}
@@ -99,6 +100,7 @@ function pad(str) {
while(str.length < 8) {
str = '0' + str
}
+
return str
}
@@ -127,8 +129,6 @@ function fixed_cca(str, idx) {
return code
}
-
-
function from_base64(str) {
return new Uint8Array(base64.toByteArray(str))
}

0 comments on commit 38af479

Please sign in to comment.
Something went wrong with that request. Please try again.