parse method
Parses raw .env content and returns a Map<String, String>.
Implementation
Map<String, String> parse(String raw) {
final result = <String, String>{};
// Normalise CRLF → LF before splitting.
final normalised = raw
.replaceAll('\r\n', '\n')
.replaceAll('\r', '\n');
for (final line in normalised.split('\n')) {
final trimmed = line.trim();
// Skip blank lines and comments.
if (trimmed.isEmpty || trimmed.startsWith('#')) continue;
final eqIndex = trimmed.indexOf('=');
// Lines without '=' are not valid key-value pairs.
if (eqIndex == -1) continue;
final key = trimmed.substring(0, eqIndex).trim();
final rawValue = trimmed.substring(eqIndex + 1).trim();
// Skip entries with empty keys.
if (key.isEmpty) continue;
// Detect whether the value is quoted before any transformation.
final isQuoted = rawValue.length >= 2 &&
((rawValue.startsWith('"') && rawValue.endsWith('"')) ||
(rawValue.startsWith("'") && rawValue.endsWith("'")));
final value = isQuoted
// Quoted: strip quotes and use content verbatim — no inline comment
// stripping (the quote boundary already delimits the value).
? rawValue.substring(1, rawValue.length - 1)
// Unquoted: strip trailing inline comment first, then trim.
: _stripInlineComment(rawValue);
result[key] = value;
}
return result;
}