D-Programming-Language/phobos — pull request #1517 by monarchdodra (2013-08-25 09:19:43+00:00)
Title: Fix bugs using `fitsInCodeUnit`

This fixes a few bugs/improves impelmentations in `std.string` using a new package-level function called `fitsInCodeUnit`: `fitsInCodeUnit` will return true if the passed character is a *valid* codepoint that fits in a single codeunit of the chosen encoding.

The idea is to make it easy to identify if decoding/encoding is necessary.

I used it to fix a bug in the `justify` family of functions, which used the erroneous `if (cast(dchar)(cast(C)fillChar) == fillChar)`: This meant the optimization was triggered for `char` for codpoints in the `0x80 .. 0x100` range.

The function is also useful when searching a string, such as in `indexOf`: If the char fits, then decoding is not necessary. This allowed "unlocking" an non-decoding optimization in `indexOf`.

This function, if pulled, could also be used in my `find` optimization: #1492, where it is basically hand encoded.