substitute function

String substitute(
  1. String source,
  2. dynamic values,
  3. String encodeValue(
    1. dynamic value,
    2. String? type
    )
)

Implementation

String substitute(String source, values, String encodeValue(value, String? type)) {
  _ValueEncoder valueEncoder;
  if (values is List) valueEncoder = _createListValueEncoder(values, encodeValue);
  else if (values is Map) valueEncoder = _createMapValueEncoder(values, encodeValue);
  else if (values == null) valueEncoder = _nullValueEncoder;
  else throw new ArgumentError('Unexpected type.');

  final buf = new StringBuffer(),
    s = new _Scanner(source),
    cache = new HashMap();

  while (s.hasMore()) {
    var t = s.read()!;
    if (t.type == _TOKEN_IDENT) {
      final id = t.value,
        typeName = t.typeName,
        key = new Pair(id, typeName);
      buf.write(cache[key] ?? (cache[key] = valueEncoder(id, typeName)));
    } else {
      buf.write(t.value);
    }
  }

  return buf.toString();
}