node icon indicating copy to clipboard operation
node copied to clipboard

[Implement] Naive Buffer.from, and friends

Open jtenner opened this issue 4 years ago • 3 comments

Implements Buffer.from<T>(value: T) and a few other convenience functions.

There are lots of obvious problems with not having the function overloads, but there are different paths we can take.

For instance, because there are no optional parameters, we must assume that the default encoding for strings is UTF8.

// utf-16 has a work around in AssemblyScript
let buff = Buffer.from(String.UTF16.encode(value));

// using Buffer.fromString(value, "utf16le");

Things to note about this function:

  • String[] objects do something complicated: string -> f64 -> u8 to remain compatible with node.
> Buffer.from(["3", "257.3", "15", "Infinity", "NaN"])
<Buffer 03 01 0F 00 00>
  • String objects need to be converted to UTF8
  • Buffer objects share their view of the same ArrayBuffer
  • ArrayBuffer objects are simply attached to a new Buffer
  • ArrayBufferView objects need to convert their values to u8.

jtenner avatar Jul 19 '19 18:07 jtenner

@RedDwarfian and @dcodeIO thoughts on this pull request?

jtenner avatar Aug 22 '19 13:08 jtenner

It is not clear in the Naive Buffer.from code where non-string arrays such as Array<u8> are handled. I would suggest adding a comment to clarify that.

RedDwarfian avatar Aug 22 '19 15:08 RedDwarfian

We might like to pull the ASCII encoding pull request first. #27

Otherwise, this is ready for review.

jtenner avatar Mar 13 '20 03:03 jtenner