substring patch, coutesy of Yandong Mao