escape static method

String escape(
  1. String value
)

Escapes all characters of the given string that are not allowed to occur unescaped in a multiline basic string.

Implementation

static String escape(String value) {
  var buffer = StringBuffer();
  var unescapedOrNewline = ChoiceParser(
    [unescapedParser, tomlNewline],
    failureJoiner: selectFarthestJoined,
  );
  var quotes = 0;
  var iterator = value.runes.iterator;
  while (iterator.moveNext()) {
    final rune = iterator.current;

    // If the current rune is a quotation mark and it is preceded by less
    // than two quotation marks, it does not have to be escaped, because only
    // three or more quotation marks can be confused for a closing delimiter.
    if (rune == TomlBasicString.delimiter.runes.first && quotes < 2) {
      buffer.writeCharCode(rune);
      quotes++;
    } else {
      quotes = 0;

      // If the current rune is a carriage return and the next rune is a
      // line feed, the carriage return does not have to be escaped.
      if (rune == TomlEscapedChar.carriageReturn &&
          _peekNext(iterator) == TomlEscapedChar.lineFeed) {
        buffer.writeCharCode(rune);
      } else {
        TomlEscapedChar.writeEscapedChar(rune, buffer, unescapedOrNewline);
      }
    }
  }
  return buffer.toString();
}