Change castFromString so it can cast to constant string references

Before this change, constant string references were initialized from temporary stack allocated strings and deallocated before they were used as a result. This may have passed tests intermittently. It was especially noticeable for me when using longer strings as they were more likely to be corrupted by the time they were used.

I'd like to write tests for this, but getting the build system setup to run tests is non-trivial as a result of hard coded paths. I did it once before but have since nuked the source tree. I'll try and get around to it soon, but it's a little painful for a small change like this so I may wait until I have a few more.