parseOne method

Map<String, String> parseOne(
  1. String line, {
  2. Map<String, String> env = const {},
})

Parses a single line into a key-value pair.

Implementation

Map<String, String> parseOne(String line,
    {Map<String, String> env = const {}}) {
  var stripped = strip(line);
  if (!_isValid(stripped)) return {};

  var idx = stripped.indexOf('=');
  var lhs = stripped.substring(0, idx);
  var k = swallow(lhs);
  if (k.isEmpty) return {};

  var rhs = stripped.substring(idx + 1, stripped.length).trim();
  var quotChar = surroundingQuote(rhs);
  var v = unquote(rhs);
  if (quotChar == _singleQuot) {
    v = v.replaceAll("\\'", "'");
    return {k: v};
  }
  if (quotChar == '"') {
    v = v.replaceAll('\\"', '"').replaceAll('\\n', '\n');
  }
  final interpolatedValue = interpolate(v, env).replaceAll("\\\$", "\$");
  return {k: interpolatedValue};
}