Unify whitespace trimming logic