(bug 46867) trim bad utf-8 sequences before normalizing.

This adds code to detect and remove incomplete utf-8 sequences at the beginning
and end of strings, when normalizing them for internal use, e.g. in search keys.

This is a) sensible by itself and b) needed to work around the fact that
preg_replace will return an empty string when encountering an invalid utf-8
sequence anywhere in the input.

Change-Id: I702e01b3f021bb2e86fb309e0d51db2a10475ac2
Merge "(bug 46867) trim bad utf-8 sequences before normalizing."