tomlTableStrings function

Map<String, String> tomlTableStrings(
  1. String text,
  2. String table
)

The string values of one table of a TOML document ([project], [package], [tool.poetry]): key = "value" / key = 'value' lines, comments and other value types ignored. Enough for a project file's name and version; not a TOML parser.

Implementation

Map<String, String> tomlTableStrings(String text, String table) {
  final values = <String, String>{};
  String? current;
  for (final raw in text.split('\n')) {
    final line = raw.trim();
    if (line.isEmpty || line.startsWith('#')) continue;
    final header = RegExp(r'^\[\[?\s*([^\]]+?)\s*\]\]?').firstMatch(line);
    if (header != null) {
      current = header.group(1);
      continue;
    }
    if (current != table) continue;
    final kv = RegExp(
      r'''^([A-Za-z0-9_.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')''',
    ).firstMatch(line);
    if (kv != null) values[kv.group(1)!] = kv.group(2) ?? kv.group(3)!;
  }
  return values;
}