ascii.js 532 B

12345678910111213141516171819202122232425262728293031
  1. function byteLength (string) {
  2. return string.length
  3. }
  4. function toString (buffer) {
  5. const len = buffer.byteLength
  6. let result = ''
  7. for (let i = 0; i < len; i++) {
  8. result += String.fromCharCode(buffer[i])
  9. }
  10. return result
  11. }
  12. function write (buffer, string, offset = 0, length = byteLength(string)) {
  13. const len = Math.min(length, buffer.byteLength - offset)
  14. for (let i = 0; i < len; i++) {
  15. buffer[offset + i] = string.charCodeAt(i)
  16. }
  17. return len
  18. }
  19. module.exports = {
  20. byteLength,
  21. toString,
  22. write
  23. }