from-string.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
  2. var B = require('../').Buffer
  3. var test = require('tape')
  4. test('detect utf16 surrogate pairs', function (t) {
  5. var text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D'
  6. var buf = new B(text)
  7. t.equal(text, buf.toString())
  8. t.end()
  9. })
  10. test('detect utf16 surrogate pairs over U+20000 until U+10FFFF', function (t) {
  11. var text = '\uD842\uDFB7' + '\uD93D\uDCAD' + '\uDBFF\uDFFF'
  12. var buf = new B(text)
  13. t.equal(text, buf.toString())
  14. t.end()
  15. })
  16. test('replace orphaned utf16 surrogate lead code point', function (t) {
  17. var text = '\uD83D\uDE38' + '\uD83D' + '\uD83D\uDC4D'
  18. var buf = new B(text)
  19. t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d ]))
  20. t.end()
  21. })
  22. test('replace orphaned utf16 surrogate trail code point', function (t) {
  23. var text = '\uD83D\uDE38' + '\uDCAD' + '\uD83D\uDC4D'
  24. var buf = new B(text)
  25. t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d ]))
  26. t.end()
  27. })
  28. test('do not write partial utf16 code units', function (t) {
  29. var f = new B([0, 0, 0, 0, 0])
  30. t.equal(f.length, 5)
  31. var size = f.write('あいうえお', 'utf16le')
  32. t.equal(size, 4)
  33. t.deepEqual(f, new B([0x42, 0x30, 0x44, 0x30, 0x00]))
  34. t.end()
  35. })
  36. test('handle partial utf16 code points when encoding to utf8 the way node does', function (t) {
  37. var text = '\uD83D\uDE38' + '\uD83D\uDC4D'
  38. var buf = new B(8)
  39. buf.fill(0)
  40. buf.write(text)
  41. t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xf0, 0x9f, 0x91, 0x8d ]))
  42. buf = new B(7)
  43. buf.fill(0)
  44. buf.write(text)
  45. t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00, 0x00 ]))
  46. buf = new B(6)
  47. buf.fill(0)
  48. buf.write(text)
  49. t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00 ]))
  50. buf = new B(5)
  51. buf.fill(0)
  52. buf.write(text)
  53. t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00 ]))
  54. buf = new B(4)
  55. buf.fill(0)
  56. buf.write(text)
  57. t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8 ]))
  58. buf = new B(3)
  59. buf.fill(0)
  60. buf.write(text)
  61. t.deepEqual(buf, new B([ 0x00, 0x00, 0x00 ]))
  62. buf = new B(2)
  63. buf.fill(0)
  64. buf.write(text)
  65. t.deepEqual(buf, new B([ 0x00, 0x00 ]))
  66. buf = new B(1)
  67. buf.fill(0)
  68. buf.write(text)
  69. t.deepEqual(buf, new B([ 0x00 ]))
  70. t.end()
  71. })
  72. test('handle invalid utf16 code points when encoding to utf8 the way node does', function (t) {
  73. var text = 'a' + '\uDE38\uD83D' + 'b'
  74. var buf = new B(8)
  75. buf.fill(0)
  76. buf.write(text)
  77. t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0x62 ]))
  78. buf = new B(7)
  79. buf.fill(0)
  80. buf.write(text)
  81. t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd ]))
  82. buf = new B(6)
  83. buf.fill(0)
  84. buf.write(text)
  85. t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0x00, 0x00 ]))
  86. buf = new B(5)
  87. buf.fill(0)
  88. buf.write(text)
  89. t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0x00 ]))
  90. buf = new B(4)
  91. buf.fill(0)
  92. buf.write(text)
  93. t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd ]))
  94. buf = new B(3)
  95. buf.fill(0)
  96. buf.write(text)
  97. t.deepEqual(buf, new B([ 0x61, 0x00, 0x00 ]))
  98. buf = new B(2)
  99. buf.fill(0)
  100. buf.write(text)
  101. t.deepEqual(buf, new B([ 0x61, 0x00 ]))
  102. buf = new B(1)
  103. buf.fill(0)
  104. buf.write(text)
  105. t.deepEqual(buf, new B([ 0x61 ]))
  106. t.end()
  107. })