formatMsg function

String formatMsg(
  1. String fmt,
  2. List args
)

formatMsg —— corresponds to the subset of SysUtils.Format that db.pas uses: substitutes %s / %d / %f with args' values in order (no width/precision modifiers supported, since dbconst's messages don't use those).

Implementation

String formatMsg(String fmt, List<dynamic> args) {
  final buf = StringBuffer();
  var argIndex = 0;
  var i = 0;
  while (i < fmt.length) {
    final c = fmt[i];
    if (c == '%' && i + 1 < fmt.length) {
      final spec = fmt[i + 1];
      if (spec == 's' || spec == 'd' || spec == 'f') {
        final arg = argIndex < args.length ? args[argIndex] : "";
        argIndex++;
        buf.write("$arg");
        i += 2;
        continue;
      }
      if (spec == '%') {
        buf.write("%");
        i += 2;
        continue;
      }
    }
    buf.write(c);
    i++;
  }
  return buf.toString();
}