asString method

  1. @override
String asString()
override

Implementation

@override
String asString() {
  var ret = '';

  if (!_has_init) {
    return ret;
  }

  if (_output != null) {
    return _output;
  }

  if (options['add_space'] && options['sign'] == '' && _arg >= 0) {
    options['sign'] = ' ';
  }

  if (_arg.isInfinite) {
    if (_arg.isNegative) {
      options['sign'] = '-';
    }

    ret = 'inf';
    options['padding_char'] = ' ';
  }

  if (_arg.isNaN) {
    ret = 'nan';
    options['padding_char'] = ' ';
  }

  if (options['precision'] == -1) {
    // ignore: todo
    options['precision'] = 6; // TODO: make this configurable
  } else if (fmt_type == 'g' && options['precision'] == 0) {
    options['precision'] = 1;
  }

  if (_arg is num) {
    if (_is_negative) {
      options['sign'] = '-';
    }

    if (fmt_type == 'e') {
      ret = asExponential(options['precision'], remove_trailing_zeros: false);
    } else if (fmt_type == 'f') {
      ret = asFixed(options['precision'], remove_trailing_zeros: false);
    } else {
      // type == g
      var _exp = _exponent;
      var sig_digs = options['precision'];
      // print("${_exp} ${sig_digs}");
      if (-4 <= _exp && _exp < options['precision']) {
        sig_digs -= _decimal;
        var precision = max<num>(options['precision'] - 1 - _exp, sig_digs);

        ret = asFixed(precision.toInt(),
            remove_trailing_zeros: !options['alternate_form']);
      } else {
        ret = asExponential(options['precision'] - 1,
            remove_trailing_zeros: !options['alternate_form']);
      }
    }
  }

  var min_chars = options['width'];
  var str_len = ret.length + options['sign'].length;
  var padding = '';

  if (min_chars > str_len) {
    if (options['padding_char'] == '0' && !options['left_align']) {
      padding = Formatter.get_padding(min_chars - str_len, '0');
    } else {
      padding = Formatter.get_padding(min_chars - str_len, ' ');
    }
  }

  if (options['left_align']) {
    ret = "${options['sign']}${ret}${padding}";
  } else if (options['padding_char'] == '0') {
    ret = "${options['sign']}${padding}${ret}";
  } else {
    ret = "${padding}${options['sign']}${ret}";
  }

  if (options['is_upper']) {
    ret = ret.toUpperCase();
  }

  return (_output = ret);
}