decodeEscapes function

String decodeEscapes(
  1. String input, {
  2. bool replaceQuot = false,
})

Decodes Unicode escapes.

Implementation

String decodeEscapes(String input, {bool replaceQuot = false}) {
  String output = input;
  output = output.replaceAllMapped(RegExp(r'\\u([0-9a-fA-F]{4})'), (match) {
    final hexCode = match.group(1)!;
    final charCode = int.parse(hexCode, radix: 16);
    return String.fromCharCode(charCode);
  });

  output = output.replaceAllMapped(RegExp(r'\\x([0-9a-fA-F]{2})'), (match) {
    final hexCode = match.group(1)!;
    final charCode = int.parse(hexCode, radix: 16);
    return String.fromCharCode(charCode);
  });

  output = output.replaceAll(r'\\', r'\');
  output = output.replaceAll(r'\/', '/');
  output = output.replaceAll(r'\&', '&');
  if (replaceQuot) {
    output = output.replaceAll(r'\"', '"');
  }

  return output;
}