Skip to content
ZstrProcessEscape

ZstrProcessEscape

Description

Decode one escape sequence starting at *str. *str must point at the leading \\; on success the pointer is advanced past the consumed escape. Handles single-character escapes (\\n/\\r/\\t/\\b/\\f/\\v/\\a/\\\\/\\"/\\'/\\0), hex escapes (\\xNN), and \\u{...} Unicode escapes that fit in a single byte.

Parameters

Name Direction Description
str in,out Address of a Zstr cursor. Advanced past the consumed escape on success.

Success

Returns the decoded byte; *str is advanced past the escape.

Failure

Returns 0; logs the malformed escape; *str may have been partially advanced.

Usage example (Cross-references)

Usage examples (Cross-references)
    #endif // FEATURE_FLOAT
    
    char ZstrProcessEscape(Zstr *str) {
        if (!str || !*str)
            return 0;
        Zstr s = *str;
        if (*s != '\\') {
            LOG_ERROR("ZstrProcessEscape called on non-escape sequence");
            return 0;
        }
                if (*i == '\\') {
                    Zstr curr = i;
                    char c    = ZstrProcessEscape(&curr);
                    if (c == 0) {
                        StrDeinit(s);
                if (*i == '\\') {
                    Zstr curr = i;
                    char c    = ZstrProcessEscape(&curr);
                    if (c == 0) {
                        StrDeinit(s);
    static char decode_hex_escape(Zstr lit) {
        Zstr p = lit;
        return ZstrProcessEscape(&p);
    }
    static bool test_m11_hex_two_digits_not_truncated(void) {
        Zstr p = "\\x41"; // 'A'
        char c = ZstrProcessEscape(&p);
        return c == 'A';
    }
    static bool test_m11_hex_value_is_decoded_not_42(void) {
        Zstr p = "\\x41"; // must yield 65 ('A'), not 42 ('*')
        char c = ZstrProcessEscape(&p);
        return c == (char)65;
    }
    static bool test_m11_hex_ff(void) {
        Zstr p = "\\xff";
        char c = ZstrProcessEscape(&p);
        return (unsigned char)c == 0xFFu;
    }
    static bool test_m11_hex_valid_not_rejected(void) {
        Zstr p = "\\x41";
        return ZstrProcessEscape(&p) == 'A';
    }
        char        buf[] = {'\\', 'x', '0', '0', 'Z', '\0'};
        const char *p     = buf;
        char        c     = ZstrProcessEscape(&p);
        // real: decodes NUL byte and advances p to the last consumed hex digit
        // (buf[3]); mutant (<=0) errors out and leaves p == buf.
    static bool test_m11_hex_result_assigned_from_value(void) {
        Zstr p = "\\x41";
        return ZstrProcessEscape(&p) == (char)65;
    }
    static bool test_m11_hex_invalid_returns_zero(void) {
        Zstr p = "\\xZZ";
        return ZstrProcessEscape(&p) == 0;
    }
    static bool test_m11_simple_escape_decodes(void) {
        Zstr p = "\\n";
        char c = ZstrProcessEscape(&p);
        return c == '\n';
    }
    static bool test_m11_escape_n(void) {
        Zstr p = "\\n";
        return ZstrProcessEscape(&p) == '\n';
    }
    static bool test_m11_escape_r(void) {
    static bool test_m11_escape_r(void) {
        Zstr p = "\\r";
        return ZstrProcessEscape(&p) == '\r';
    }
    static bool test_m11_escape_t(void) {
    static bool test_m11_escape_t(void) {
        Zstr p = "\\t";
        return ZstrProcessEscape(&p) == '\t';
    }
    static bool test_m11_escape_b(void) {
    static bool test_m11_escape_b(void) {
        Zstr p = "\\b";
        return ZstrProcessEscape(&p) == '\b';
    }
    static bool test_m11_escape_f(void) {
    static bool test_m11_escape_f(void) {
        Zstr p = "\\f";
        return ZstrProcessEscape(&p) == '\f';
    }
    static bool test_m11_escape_v(void) {
    static bool test_m11_escape_v(void) {
        Zstr p = "\\v";
        return ZstrProcessEscape(&p) == '\v';
    }
    static bool test_m11_escape_a(void) {
    static bool test_m11_escape_a(void) {
        Zstr p = "\\a";
        return ZstrProcessEscape(&p) == '\a';
    }
    static bool test_m11_escape_backslash(void) {
    static bool test_m11_escape_backslash(void) {
        Zstr p = "\\\\";
        return ZstrProcessEscape(&p) == '\\';
    }
    static bool test_m11_escape_dquote(void) {
    static bool test_m11_escape_dquote(void) {
        Zstr p = "\\\"";
        return ZstrProcessEscape(&p) == '"';
    }
    static bool test_m11_escape_squote(void) {
    static bool test_m11_escape_squote(void) {
        Zstr p = "\\'";
        return ZstrProcessEscape(&p) == '\'';
    }
    // case '0' -> result = '\0'; cxx_assign_const(42) at 2216 makes it '*'.
    static bool test_m11_escape_nul(void) {
        Zstr p = "\\0";
        return ZstrProcessEscape(&p) == '\0';
    }
Last updated on