M-OZ BLOG

Scheme-users.jpTwitter

# 2008/10/20(Mon) : 文字列操作マクロ
この前 (10/14) のマクロの続き。
汎用的に使えるものを考えてみた。

(use srfi-13) ;; string-trim

(define-syntax $str-proc
 (syntax-rules (trim-both replace-all)
  ((_ str) str)
  ((_ trim-both str) (string-trim-both str))
  ((_ replace-all str r s) (regexp-replace-all r str s))))

(define-syntax $str
 (syntax-rules ()
  ((_ str) str)
  ((_ str (proc x ...)) ($str-proc proc str x ...))
  ((_ str proc) ($str-proc proc str))
  ((_ str p1 p2 ...) ($str ($str str p1) p2 ...))))

↓こんな感じに、文字列操作関数のネストをパラメータ的に記述できる。

(display
 ($str "1234ABCD5678"
  (replace-all #/[0-9+]/ "+")
  (replace-all #/[+]/ (string #\space))
  trim-both))
(newline)

これはなかなか実用的かも。

[TOP] [ALL]