improved safesubstring performance