setString method

int setString(
  1. String string
)

Writes string into this buffer as ANSI-encoded bytes.

The caller must guarantee that the underlying memory is large enough to hold string.length + 1 bytes. Writing past the allocated region results in undefined behavior.

Returns the total number of bytes written, including the terminating NUL.

Example:

final buffer = adaptiveMalloc<BYTE>(10).cast<Utf8>();
final pstr = PSTR(buffer);
final bytesWritten = pstr.setString("Hello");
print(bytesWritten); // Outputs: 6 (5 characters + 1 NUL)
free(buffer);

Implementation

int setString(String string) {
  final length = string.length;
  final pstr = cast<BYTE>();
  for (var i = 0; i < length; i++) {
    pstr[i] = string.codeUnitAt(i) & 0xFF;
  }
  pstr[length] = 0;

  final bytesWritten = length + 1;
  return bytesWritten;
}