With a macro:
(defmacro with-replace-all (regex new &rest body)
`(cl-ppcre:regex-replace-all ,regex ,@body ,new))
With a recursive function (this is the 'rewrite')
(defun replace-all (old-new-list text)
(if (not old-new-list)
text
(let ((old-new (car old-new-list)))
(replace-all (cdr old-new-list) (cl-ppcre:regex-replace-all (car old-new) text (cadr old-new))))))
The original function that used the macro: (removed the html for easy editing in blogger)
(defun color-format (str)
(with-replace-all "\\(" "html stuff.."
(with-replace-all "\\]" "html stuff.."
(with-replace-all "\\[" "html stuff.."
(with-replace-all "\\)" "html stuff.."
(with-replace-all "\(\".*?\"\)" "html stuff.."
(with-replace-all "," "html stuff.."
(with-replace-all "\\|\(gt[[_xA-Z0-9]+\)" "more html stuff.."
(with-replace-all "\([^\\]] gt[_xA-Z0-9]+\)" "more html stuff.."
(with-replace-all "\(gtV(\\d+?)x(\\d+?)x(\\w*)\)" "more html stuff.."
str))))))))))
Not very pretty, is it?
The new function that uses the rewritten function:
(defun new-color-format (str)
(replace-all '(("\(gtV(\\d+?)x(\\d+?)x(\\w*)\)" "html..")
("\([^\\]] gt[_xA-Z0-9]+\)" "html..")
("\\|\(gt[[_xA-Z0-9]+\)" "html..")
("," "html..")
("\(\".*?\"\)" "html..")
("\\)" "html..")
("\\[" "html..")
("\\]" "html..")
("\\(" "html.."))
str))
It looks a little better, there are less dangling parens at the end, I was pleased with myself.
Until I did this:
CL-USER> (time (color-format "[-1] gtTerminalInit(0);"))
Evaluation took:
0.001 seconds of real time
0.000000 seconds of total run time (0.000000 user, 0.000000 system)
0.00% CPU
219,648 processor cycles
4,016 bytes consed
CL-USER> (time (new-color-format "[-1] gtTerminalInit(0);"))
Evaluation took:
0.001 seconds of real time
0.000000 seconds of total run time (0.000000 user, 0.000000 system)
0.00% CPU
1,389,540 processor cycles
15,984 bytes consed
Recursion is nice, but it's 6 times as much work for the CPU?
Please help me out here if I made mistakes.