Fix ticket #4922 by checking the return value of string::find().

Hi,

This patch ensures the return value of string::find is checked to avoid an infinite loop (on my Mac) or an assertion (on the bug reporter's machine) upon invalid input. Thanks to consider pulling.

Best regards,
  Simon