toCharArray method

Int8List toCharArray({
  1. int? memoryWidth,
  2. bool zeroTerminated = false,
})

Implementation

Int8List toCharArray({int? memoryWidth, bool zeroTerminated = false}) {
  final List<int> chars;
  if (zeroTerminated) {
    chars = utf8.encode(this).takeWhile((value) => value != 0).toList();
  } else {
    chars = utf8.encode(this);
  }

  if (memoryWidth != null) {
    if (chars.length > memoryWidth) {
      throw ArgumentError.value(
        memoryWidth,
        'memoryWidth',
        'must be at least as long as the encoded string ${chars.length} bytes',
      );
    }

    return Int8List(memoryWidth)
      ..setRange(0, chars.length, chars)
      ..fillRange(chars.length, memoryWidth, 0);
  } else {
    return Int8List.fromList(chars);
  }
}