Hardware interview practice
Measure the final UART command token
Measure the last token in an explicitly sized UART command buffer after ignoring trailing ASCII space bytes.
Starting point
Question code
typedef enum { TOKEN_OK, TOKEN_EINVAL } token_rc_t;
token_rc_t last_token_len(const uint8_t *buf,
size_t len,
size_t *token_len);
// len <= 128; only 0x20 separates tokens.Reviewed example
Work through one case
Input
buffer bytes="run test ", explicit_length=11Expected output
last_token_length=4 ("test")Trailing ASCII spaces are skipped, then the scan stops at the separator before test without relying on a terminator.
What to cover
Requirements
- Treat only byte 0x20 as a separator; embedded zero and nonprinting bytes remain token data.
- Return zero for an empty or all-space input.
- Scan with unsigned indexes without decrementing below zero, calling strlen, allocating, or mutating the buffer.
- Reject a null output, length above 128, or null nonempty input without changing the caller's old length.
