diff --git a/uniseg/graphemes/stream.scm b/uniseg/graphemes/stream.scm index 0a6fd29..f8eadf9 100644 --- a/uniseg/graphemes/stream.scm +++ b/uniseg/graphemes/stream.scm @@ -122,28 +122,33 @@ (else (values 'any #t)))) - (define glyphs-reverse '()) - (define grapheme-width 0) + (define %glyphs-reverse '()) + (define %grapheme-width 0) (define hit-eof #f) (define (iterate-through-grapheme state) - (define glyph (get-char port)) - (if - (eof-object? glyph) + (define glyph (peek-char port)) + (if (eof-object? glyph) + (begin + (set! hit-eof #t) + state) (begin - (set! hit-eof #t) - state) - (begin - (set! glyphs-reverse (cons glyph glyphs-reverse)) (let* ((width property (char-width glyph)) (next-state boundary? (state-machine state property))) - (set! grapheme-width (+ grapheme-width width)) + (pk width next-state) (if boundary? state - (iterate-through-grapheme state)))))) + (let ((new-width (+ %grapheme-width width)) + (new-glyphs-reverse (cons glyph %glyphs-reverse))) + + ;; Officially induct this char into the cluster + (get-char port) + (set! %grapheme-width new-width) + (set! %glyphs-reverse new-glyphs-reverse) + (iterate-through-grapheme state))))))) (define final-state (iterate-through-grapheme 'any)) @@ -152,8 +157,8 @@ stream-null (stream-cons (make-grapheme - grapheme-width + %grapheme-width ;; Delay to avoid construction of unnecessary lists and strings! - (delay (reverse-list->string glyphs-reverse)) - (delay (reverse glyphs-reverse))) + (delay (reverse-list->string %glyphs-reverse)) + (delay (reverse %glyphs-reverse))) (input->grapheme-stream port))))