parse method

  1. @override
int parse(
  1. String input
)
override

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 (suffix.isNotEmpty) {
    if (!text.endsWith(suffix)) {
      throw FormatException('Expected year suffix "$suffix".', input);
    }
    text = text.substring(0, text.length - suffix.length);
  } else if (text.endsWith('年')) {
    text = text.substring(0, text.length - 1);
  }

  if (text.isEmpty) {
    throw FormatException('Expected a Chinese year number.', input);
  }

  final buffer = StringBuffer();
  for (final char in text.runes.map(String.fromCharCode)) {
    final value = _digitValues[char];
    if (value == null) {
      throw FormatException('Unexpected Chinese year token.', input);
    }
    buffer.write(value);
  }

  return int.parse(buffer.toString());
}