escape method

Iterable<int> escape({
  1. QuoteType quotes = QuoteType.single,
  2. bool ascii = false,
})

A generator that escapes this Runes.

See EscapeString.escape for parameter details.

Implementation

Iterable<int> escape({
  QuoteType quotes = QuoteType.single,
  bool ascii = false,
}) sync* {
  var quote = switch (quotes) {
    QuoteType.none => null,
    QuoteType.single => $singleQuote,
    QuoteType.double => $doubleQuote,
  };

  if (quote != null) {
    yield quote;
  }

  for (var rune in this) {
    switch (rune) {
      case $lf:
        yield $backslash;
        yield $n;
      case $cr:
        yield $backslash;
        yield $r;
      case $bs:
        yield $backslash;
        yield $b;
      case $tab:
        yield $backslash;
        yield $t;
      case $vt:
        yield $backslash;
        yield $v;
      case $ff:
        yield $backslash;
        yield $f;
      case $backslash:
      case $dollar:
        yield $backslash;
        yield rune;
      default:
        if (rune == quote) {
          yield $backslash;
        }

        var isControlCharacter =
            rune <= 0x1F || rune == 0x7F || (rune >= 0x80 && rune <= 0x9F);

        if (!isControlCharacter && (rune < 128 || !ascii)) {
          yield rune;
        } else {
          yield $backslash;
          yield $u;
          yield $openBrace;
          yield* rune.toRadixString(16).runes;
          yield $closeBrace;
        }
    }
  }

  if (quote != null) {
    yield quote;
  }
}