jmdavis/phobos — comment on commit 2d340a9ea3d8e738693f81c688f25440a49ff107
File: std/utf.d line 1207
By jmdavis:

It may or may not be the best choice with regards to memory usage, but the conversion doesn't get messed up. It allocates the new string with the same number of code units as the original, sets the `length` to `0`, and then uses appending to put the elements into the string. If you're converting from UTF-8 to UTF-16, it risks allocating more memory that it really needs, whereas when converting to UTF-16 from UTF-8, it risks having to reallocate at some point while appending. But the idea was to reduce the number of reallocations necessary by starting with a string in the ballpark of the correct length, since simply appending to an empty string would definitely end up reallocating several times unless the string being converted is quite short.