setLengthLeft function

Uint8List setLengthLeft(
  1. Uint8List msg,
  2. int length, {
  3. bool right = false,
})

Left Pads an Uint8List with leading zeros till it has length bytes. Or it truncates the beginning if it exceeds.

Implementation

Uint8List setLengthLeft(Uint8List msg, int length, {bool right = false}) {
  var buf = zeros(length);
  msg = toBuffer(msg);
  if (right) {
    if (msg.length < length) {
      buf.setAll(0, msg);
      return buf;
    }
    return msg.sublist(0, length);
  } else {
    if (msg.length < length) {
      buf.setAll(length - msg.length, msg);
      return buf;
    }
    return msg.sublist(msg.length - length);
  }
}