| Line | |
|---|
| 1 | (in-package :jss) |
|---|
| 2 | |
|---|
| 3 | (defun tree-replace (replace-fn tree) |
|---|
| 4 | "create new tree replacing each element with the result of calling replace-fn on it" |
|---|
| 5 | (labels ((tr-internal (tree) |
|---|
| 6 | (cond ((atom tree) (funcall replace-fn tree)) |
|---|
| 7 | (t (let ((replacement (funcall replace-fn tree))) |
|---|
| 8 | (if (eq replacement tree) |
|---|
| 9 | (mapcar #'tr-internal tree) |
|---|
| 10 | replacement)))))) |
|---|
| 11 | (tr-internal tree))) |
|---|
| 12 | |
|---|
| 13 | (defun replace-all (string regex function &rest which) |
|---|
| 14 | (let ((matcher (#"matcher" (if (java-object-p regex) regex (#"compile" 'java.util.regex.pattern regex)) string)) |
|---|
| 15 | (sb (new 'stringbuffer))) |
|---|
| 16 | (with-constant-signature ((append "appendReplacement")) |
|---|
| 17 | (loop for found = (#"find" matcher) |
|---|
| 18 | while found |
|---|
| 19 | do |
|---|
| 20 | (#"appendReplacement" matcher sb (apply function |
|---|
| 21 | (loop for g in which collect |
|---|
| 22 | (#"group" matcher g))))) |
|---|
| 23 | ) |
|---|
| 24 | (#"appendTail" matcher sb) |
|---|
| 25 | (#"toString" sb))) |
|---|
Note: See
TracBrowser
for help on using the repository browser.