convert method
- String s
override
Converts input
and returns the result of the conversion.
Implementation
@override
Uint8List convert(String s) {
var result = new Uint8List(64);
var resultLength = 0;
var lineNumber = 0;
for (var line in s.split("\n")) {
lineNumber++;
// Remove prefix
final match = _prefixRegExp.matchAsPrefix(line);
if (match != null) {
line = line.substring(match.end);
}
int firstGroupLength;
// For each hexadecimal digit group
while (true) {
// Match hexadecimical characters
final match = _hexRegExp.matchAsPrefix(line);
if (match == null) {
break;
}
final group = match.group(1);
line = line.substring(match.end);
// Validate group length
if (group.length % 2 != 0) {
throw new ArgumentError(
"Error at line $lineNumber: invalid group of hexadecimal digits (non-even length): '$group'",
);
}
if (firstGroupLength == null) {
firstGroupLength = group.length;
} else if (group.length > firstGroupLength) {
// Longer than the first group.
// Conclude that it's not a hex group anymore.
break;
}
// Parse bytes
for (var i = 0; i < group.length; i += 2) {
final byteString = group.substring(i, i + 2);
int byte;
try {
byte = int.parse(byteString, radix: 16);
} catch (e) {
throw new ArgumentError.value(
"Error at line $lineNumber: '$byteString' is not hex",
);
}
// Ensure that the buffer has capacity left
if (resultLength == result.lengthInBytes) {
final oldResult = result;
result = new Uint8List(2 * resultLength);
result.setAll(0, oldResult);
}
// Set byte
result[resultLength] = byte;
resultLength++;
}
}
}
return new Uint8List.view(result.buffer, 0, resultLength);
}