Skip to content

Commit

Permalink
fix issue with encoding certain unicode codepoints
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdickinson committed Nov 28, 2013
1 parent ad5d051 commit 38af479
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 15 additions & 2 deletions test/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions typedarray/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function codepoint_to_bytes(arr, code) {
}

if(pos) {
_byte |= +!!(code & 1) << (7 - pos)
arr[arr.length] = _byte
}
}
Expand All @@ -99,6 +100,7 @@ function pad(str) {
while(str.length < 8) {
str = '0' + str
}

return str
}

Expand Down Expand Up @@ -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.