New Scheme string routines
Charles Day
cedayiv at gmail.com
Thu Jun 5 12:11:54 EDT 2008
To support multi-byte account separators in the QIF importer I have had to
write several new Scheme string manipulation routines. They are pretty
useful variations on some of the standard routines. I have them working in
the QIF importer, but is there a better place to stick them so that they can
be used from other modules, such as reports? Perhaps in main.scm?
-Charles
P.S. For example, here are a three of the Scheme procedures I have written.
There are a few more, but this will give you a good idea. If you happen to
see any room for performance improvement, let me know.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; string-rcontains
;;
;; Like string-contains, but searches from the right.
;;
;; Example: (string-rcontains "foobarfoobarf" "bar") returns 9.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (string-rcontains s1 s2)
(let ((s2len (string-length s2)))
(let loop ((i (string-contains s1 s2))
(retval #f))
(if i
(loop (string-contains s1 s2 (+ i s2len)) i)
retval))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; substring-count
;;
;; Similar to string-count, but searches for a substring rather
;; than a single character.
;;
;; Example: (substring-count "foobarfoobarf" "bar") returns 2.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (substring-count s1 s2)
(let ((s2len (string-length s2)))
(let loop ((i (string-contains s1 s2))
(retval 0))
(if i
(loop (string-contains s1 s2 (+ i s2len)) (+ 1 retval))
retval))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; substring-split
;;
;; Similar to string-split, but the delimiter is a string
;; rather than a single character.
;;
;; Example: (substring-split "foobarfoobarf" "bar") returns
;; ("foo" "foo" "f")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (substring-split s1 s2)
(let ((i (string-contains s1 s2)))
(if i
(cons (substring s1 0 i)
(substring-split (substring s1 (+ i (string-length s2))) s2))
(list s1))))
More information about the gnucash-devel
mailing list