stringFormat function

String stringFormat(
  1. String value
)

Implementation

String stringFormat(String value) {
  var sb = StringBuffer();
  sb.write("\"");
  for (var i = 0; i < value.length; i++) {
    var char = value[i];
    switch (char) {
      case '"':
      case '\\':
      case '/':
        sb.write('\\$char');
        break;

      case '\t':
        sb.write("\\t");
        break;

      case '\b':
        sb.write("\\b");
        break;

      case '\n':
        sb.write("\\n");
        break;

      case '\r':
        sb.write("\\r");
        break;

      case '\f':
        sb.write("\\f");
        break;

      default:
        var codeUnit = char.codeUnitAt(0);
        if (codeUnit <= 0x1F) {
          sb.write(sprintf("\\u%04x", codeUnit));
        } else {
          sb.write(char);
        }
        break;
    }
  }

  sb.write("\"");
  return sb.toString();
}