escapeChar static method

String escapeChar(
  1. int c
)

Takes a character and returns the escaped string for that character. For example escapeChar(String.fromCharCode(15)) -> "\x0E". @param {character} c The character to escape. @return {string} An escaped string representing {@code c}.

Implementation

static String escapeChar(int c)
{
  String rv = c.toString();
  int cc = c;
  if (cc > 31 && cc < 127) {
    rv = c.toString();
  }
  else {
    // tab is 9 but handled above
    if (cc < 256) {
      rv = "\\x";
      if (cc < 16 || cc > 256) {
        rv += "0";
      }
    }
    else {
      rv = "\\u";
      if (cc < 4096) {  // \u1000
        rv += "0";
      }
    }
    rv = rv + cc.toRadixString(16).toUpperCase();
  }

  return rv;
}