characterToDigit static method

int? characterToDigit(
  1. String input
)

Implementation

static int? characterToDigit(String input) {
  final regex = RegExp(r'\p{N}', unicode: true);
  int position = -1;
  // Since the unicode scalar could be [0-9],
  // we count from [0-9] to determine its proximity to 9.
  for (var i = 0; i < 10; i++) {
    final codeUnit = input.codeUnitAt(0) + i;
    final character = String.fromCharCode(codeUnit);
    if (regex.hasMatch(character)) {
      position++;
    } else {
      break;
    }
  }
  // we substract from 9 to get the index of the character,
  // which corresponds to an arabic number of [0-9].
  int number = (9 - position);
  return (number > 9) ? null : number;
}