Move substring implementation to cpp.