parse method
Parses input and returns the numeric value.
Throws FormatException when input cannot be parsed by this codec.
Implementation
@override
int parse(String input) {
var text = input.trim();
if (text.isEmpty) {
throw FormatException('Expected a Japanese cardinal number.', input);
}
final negative = text.startsWith('負');
if (negative) {
text = text.substring(1);
if (text.isEmpty) {
throw FormatException('Expected a Japanese cardinal number.', input);
}
}
if (text.runes.length == 1 &&
_digitValues[String.fromCharCode(text.runes.single)] == 0) {
return 0;
}
var total = 0;
var sectionText = StringBuffer();
var lastSectionUnit = 1000000000000000000;
for (final char in text.runes.map(String.fromCharCode)) {
final sectionUnit = _sectionUnitValues[char];
if (sectionUnit == null) {
sectionText.write(char);
continue;
}
if (sectionUnit >= lastSectionUnit) {
throw FormatException('Unexpected Japanese cardinal token.', input);
}
final rawSection = sectionText.toString();
if (rawSection.isEmpty && total > 0) {
throw FormatException('Unexpected Japanese cardinal token.', input);
}
final section = _parseSection(
rawSection,
input,
allowEmptyAsOne: true,
allowLeadingZero: total > 0,
);
if (section == 0) {
throw FormatException('Unexpected Japanese cardinal token.', input);
}
total += section * sectionUnit;
sectionText = StringBuffer();
lastSectionUnit = sectionUnit;
}
final lowerSection = _parseSection(
sectionText.toString(),
input,
allowEmptyAsOne: false,
allowLeadingZero: total > 0,
);
final value = total + lowerSection;
return negative ? -value : value;
}