[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[MIT-Scheme-devel] string-trim broken
From: |
Peter |
Subject: |
[MIT-Scheme-devel] string-trim broken |
Date: |
Mon, 16 Oct 2017 19:18:39 +0200 |
Hello fellow Schemers!
Right now, (string-trim "\"\"" (char-set-invert (char-set #\"))) does
not, in fact, trim the string, but try to call (substring str 2 0),
which results in failure.
The current implementation is:
(define (string-trimmer . options)
(receive (where to-trim copy?)
(string-trimmer-options options 'string-trimmer)
(let ((predicate (char-matcher->predicate to-trim 'string-trimmer))
(get-trimmed (if copy? substring string-slice)))
(lambda (string)
(let ((end (string-length string)))
(get-trimmed
string
(if (eq? where 'trailing)
0
(let loop ((index 0))
(if (and (fix:< index end)
(predicate (string-ref string index)))
(loop (fix:+ index 1))
index)))
(if (eq? where 'leading)
end
(let loop ((index end))
(if (and (fix:> index 0)
(predicate (string-ref string (fix:- index 1))))
(loop (fix:- index 1))
index)))))))))
I'd suggest something like the following in runtime/ustring.scm:
(define (string-trimmer . options)
(receive (where to-trim copy?)
(string-trimmer-options options 'string-trimmer)
(let ((predicate (char-matcher->predicate to-trim 'string-trimmer))
(get-trimmed (if copy? substring string-slice)))
(lambda (string)
(let* ((end (string-length string))
(the-start (if (eq? where 'trailing)
0
(let loop ((index 0))
(if (and (fix:< index end)
(predicate (string-ref string index)))
(loop (fix:+ index 1))
index))))
(the-end (if (eq? where 'leading)
end
(let loop ((index end))
(if (and (fix:> index 0)
(predicate (string-ref string (fix:-
index 1))))
(loop (fix:- index 1))
index)))))
(get-trimmed
string
(min the-start the-end)
the-end))))))
This moves the computation of the-start and the-end above the actual
call to substring, and uses the minimum of start and end (to prevent
start being further along than end).
Would anyone be kind enough to merge this or a variant of it that fixes
the same problem?
Thanks a lot, greetings,
Peter
- [MIT-Scheme-devel] string-trim broken,
Peter <=