Clean up str/bytes/unicode encoding in C.

Minor perf improvements included.