Skip to content

Commit 8a65b9a

Browse files
committed
LN_CORE: Vastly improved version of 6a63dea
Note that the new (list->utf8string l) is still about 40% slower than an (apply string-append l) approach but this version can handle arbitrary length strings rather than being limited to about ~8193 character length strings. Still 25x better than prev.
1 parent 0ab5ec3 commit 8a65b9a

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

modules/ln_core/utf8string.scm

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,17 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7575

7676
(define (utf8string->list s) (map integer->utf8char (utf8string->unicode s)))
7777

78-
(define (list->utf8string l) (apply string-append l))
78+
(define (list->utf8string lst)
79+
(define len 0)
80+
(for-each (lambda (l) (set! len (fx+ len (string-length l)))) lst)
81+
(let ((offset 0)
82+
(result (make-string len)))
83+
(for-each (lambda (l)
84+
(string-copy! result offset l)
85+
(set! offset (fx+ offset (string-length l)))
86+
) lst)
87+
result
88+
))
7989

8090
(define (utf8substring s ofs len)
8191
(list->utf8string (sublist (utf8string->list s) ofs len)))
@@ -288,9 +298,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288298
(integer->char (bitwise-ior #x80 (bitwise-and #x3F c)))))
289299
(else (log-error "unicode->utf8string: illegal format")))
290300
))
291-
(else
292-
(call-with-output-string (lambda (port)
293-
(for-each (lambda (element) (display (unicode->utf8string element) port)) src))))
301+
(else (list->utf8string (map unicode->utf8string src)))
294302
))
295303

296304
;;u8vectors that need unicode conversion

0 commit comments

Comments
 (0)