Instead of
let surrogate_mask = surrogate_bit << 2 | surrogate_bit << 1 | surrogate_bit;
len &= !surrogate_mask;
consider len &= surrogate_bit.wrapping_sub(1);
This should still work better. Alternatively, invert the conditions and do len &= non_surrogate_bit.wrapping_neg();