Apparently, replace-regexp-in-string is much faster than what I've implemented.