refactor `scan_ident` with length checks