reverseChunks method

List<T> reverseChunks(
  1. int chunkSize
)

Returns a copy of this List with chunks of length chunkSize in reversed order.

  • Useful to reverse endianness of integers in a byte buffer.
  • If chunkSize == 1 will return a copy of this instance.

Implementation

List<T> reverseChunks(int chunkSize) {
  if (chunkSize == 1) {
    return copy();
  }

  return List.generate(length, (i) {
    var chunkI = i % chunkSize;
    var chunkOffset = i - chunkI;
    var chunkIReversed = (chunkSize - 1) - chunkI;
    var idx = chunkOffset + chunkIReversed;
    return this[idx];
  });
}