Factorize and optimize `write' for strings and characters.