cstr_escape()

Escapes bytes from a string using C-style escape sequences.

The escaping rules in priority order are as follows:

  1. if the byte is the doublequote (0x22), it is escaped as \" (backslash doublequote)
  2. if the byte is the backslash (0x5C), it is escaped as \\ (double backslash)
  3. if the byte is one of the following control characters, it is escaped as follows:
    • \b (0x08, backspace)
    • \t (0x09, horizontal tab)
    • \n (0x0A, newline)
    • \v (0x0B, vertical tab)
    • \r (0x0D, carriage return)
  4. if the byte is less than or equal to 0x1F, or it is greater or equal to 0x7F (in other words, a control character not explicitly listed above), it is escaped as \xHH where HH is the hexadecimal value of the byte
  5. if none of the above matched, the byte is passed through as-is: for example a for 0x61

This function is not prefixed with the std. namespace.

Format

STRING
cstr_escape(STRING string)

Examples

1
2
3
# var.escaped is set to: city="london"
declare local var.escaped STRING;
set var.escaped = "city=%22" + cstr_escape(client.geo.city.ascii) + "%22";