toPrintable function

String toPrintable(
  1. String string
)

Returns an unescaped printable string.

Example: print(toPrintable("Hello 'world' \n")); => Hello 'world' \n

Implementation

String toPrintable(String string) {
  if (string.isEmpty) {
    return string;
  }

  final sb = StringBuffer();
  final characters = Characters(string);
  for (final s in characters) {
    final runes = s.runes;
    if (runes.length == 1) {
      final c = runes.first;
      if (c >= _C0_START && c <= _C0_END) {
        switch (c) {
          case 9:
            sb.write('\\t');
            break;
          case 10:
            sb.write('\\n');
            break;
          case 13:
            sb.write('\\r');
            break;
          default:
            sb.write(toUnicode(c));
        }
      } else if (_isPrintable(c)) {
        sb.write(s);
      } else {
        sb.write(toUnicode(c));
      }
    } else {
      // Experimental: Assuming that all clusters can be printed
      sb.write(s);
    }
  }

  return sb.toString();
}