parse method

LcovNode parse(
  1. String content, {
  2. String parent = '',
})

Implementation

LcovNode parse(String content, {String parent = ''}) {
  final lines = LineSplitter.split(content);

  String filePath = '';
  var hits = <int, int>{};
  int linesHit = -1;
  int linesFound = -1;

  final root = LcovNode(path: '');

  for (var line in lines) {
    line = line.trim();
    if (line.startsWith('SF:')) {
      if (filePath.isNotEmpty) throw 'extra SF record';
      filePath = line.substring(3);
    } else if (line.startsWith('DA:')) {
      final match = line.substring(3).split(',');
      hits[int.parse(match.first)] = int.parse(match[1]);
    } else if (line.startsWith('LH:')) {
      final match = line.substring(3).split(',');
      if (linesHit != -1) throw 'extra LH record';
      linesHit = int.parse(match.first);
    } else if (line.startsWith('LF:')) {
      final match = line.substring(3).split(',');
      if (linesFound != -1) throw 'extra LF record';
      linesFound = int.parse(match.first);
    } else if (line == 'end_of_record') {
      if (filePath == '') throw 'invalid SF path';
      final segments = pth.split(filePath);
      var currentNode = root;
      var lastSegment = segments.last;
      final nodes = <LcovNode>[root];
      if (segments.length > 1) {
        segments.length -= 1;
        for (final segment in segments) {
          currentNode = currentNode.children.putIfAbsent(segment, () => LcovNode(path: segment));
          nodes.add(currentNode);
        }
      }
      final record = LcovRecord(
        path: filePath,
        content: '', // file content
        linesFound: linesFound,
        linesHit: linesHit,
        lines: hits,
      );
      currentNode.children[lastSegment] = record;

      for (var node in nodes) {
        node
          ..linesFound += record.linesFound
          ..linesHit += record.linesHit;
      }

      // clear memory
      filePath = '';
      hits = <int, int>{};
      linesHit = -1;
      linesFound = -1;
    }
  }
  return root;
}