Improve UTF <-> wchar_t <-> ANSI conversion implementation.

* Require only input iterators instead of random access continous memory like
  the old implementation.
* Replace invalid characters with specified replacement again instead of just
  cutting the string off.
* Greatly reduce code duplication, thus improving readability.
* As a side effect of the above, fix some bugs probably resulting from me being
  in a hurry when writing the code.