escapeAndValidateString method

String escapeAndValidateString(
  1. String value
)

Escape the string for use in generated Dart code.

Implementation

String escapeAndValidateString(String value) {
  const Map<String, String> escapes = const {
    r"\": r"\\",
    '"': r'\"',
    "\b": r"\b",
    "\f": r"\f",
    "\n": r"\n",
    "\r": r"\r",
    "\t": r"\t",
    "\v": r"\v",
    "'": r"\'",
    r"$": r"\$"
  };

  String _escape(String s) => escapes[s] ?? s;

  var escaped = value.splitMapJoin("", onNonMatch: _escape);
  return escaped;
}