parseBuffer method

void parseBuffer(
  1. StringBuffer buf
)

Implementation

void parseBuffer(StringBuffer buf) {
  final lines = buf.toString().split('\n');

  final linesCount = lines.length;
  var section = '';
  var currentLine = '';

  lines.asMap().forEach((index, element) {
    var commentPos = element.indexOf(Config.DEFAULT_COMMENT);

    if (commentPos > -1) {
      element = element.substring(0, commentPos);
    }

    commentPos = element.indexOf(Config.DEFAULT_COMMENT_SEM);
    if (commentPos > -1) {
      element = element.substring(0, commentPos);
    }

    final line = element.trim();
    if (line.isEmpty) {
      return;
    }

    final lineNumber = index + 1;

    if (line.startsWith('[') && line.endsWith(']')) {
      if (currentLine.isNotEmpty) {
        write(section, lineNumber - 1, currentLine);
        currentLine = '';
      }
      section = line.substring(1, line.length - 1);
    } else {
      var shouldWrite = false;
      if (line.contains(Config.DEFAULT_MULTI_LINE_SEPARATOR)) {
        currentLine += line.substring(0, line.length - 1).trim();
      } else {
        currentLine += line;
        shouldWrite = true;
      }
      if (shouldWrite || lineNumber == linesCount) {
        write(section, lineNumber, currentLine);
        currentLine = '';
      }
    }
  });
}