ofString static method
- String stringRepresentation
Converts a binary String representation of a LogicValue into a LogicValue.
The stringRepresentation
should only contain bit values (e.g. no 0b
at the start). The order of the created LogicValue will be such that
the i
th character in stringRepresentation
corresponds to the
length - i - 1
th bit. That is, the last character of
stringRepresentation
will be the 0th bit of the returned LogicValue.
var stringRepresentation = '1x0';
var lv = LogicValue.ofString(stringRepresentation);
print(lv); // This prints `3b'1x0`
Implementation
static LogicValue ofString(String stringRepresentation) {
if (stringRepresentation.isEmpty) {
return const _FilledLogicValue(_LogicValueEnum.zero, 0);
}
if (stringRepresentation.contains(RegExp('[^01xz]'))) {
throw LogicValueConstructionException(
'Invalid characters found, must only contain 0, 1, x, and z.');
}
final valueString = _valueString(stringRepresentation);
final invalidString = _invalidString(stringRepresentation);
final width = stringRepresentation.length;
if (width <= INT_BITS) {
final value = _unsignedBinaryParse(valueString);
final invalid = _unsignedBinaryParse(invalidString);
return _smallLogicValueOrFilled(value, invalid, width);
} else {
final value = BigInt.parse(valueString, radix: 2);
final invalid = BigInt.parse(invalidString, radix: 2);
return _bigLogicValueOrFilled(value, invalid, width);
}
}