stringSubtraction method

String stringSubtraction(
  1. String s1,
  2. String s2
)

Implementation

String stringSubtraction(String s1, String s2) {
  assert(s1.length == s2.length);
  final List<String> result = List<String>.filled(s2.length, '0');

  const int c = 0;
  for (int i = s1.length - 1; i >= 0; i--) {
    int t = c + (dig(s1[i]) - dig(s2[i]));
    if (t < 0) {
      t += 10;
      if (i == 0) {
        throw Exception('Internal logic error');
      } else {
        s1 = replaceChar(s1, i - 1, cdig(dig(s1[i - 1]) - 1));
      }
    }
    result[i] = cdig(t);
  }
  assert(c == 0);
  return result.join();
}