Test strings on length before content

A substring can of course not be longer that the full string. Also fixes
problems with CMake-versions that doesn't handle out-of-range parameters
to the SUBSTRING sub-command.